| Title: | Course Materials for Statistics in Linguistics at the University of Konstanz |
|---|---|
| Description: | Companion package for the Statistics and Advanced Statistics courses taught by Massimiliano Canzi at the Department of Linguistics, University of Konstanz, written for their students and not meant for general use. Installing it brings in the packages of the Statistics course; install_course() adds those of Advanced Statistics, and check_setup() tests a computer for either course. lectures() lists the lectures with their data sets and slides, get_tutorial() copies the weekly exercise sheets into a project folder, and an R Markdown template starts the weekly assignments. The helper functions written in the Advanced Statistics slides are included: residual autocorrelation within trajectories, partial dependence, cross-validation by speaker and a 'ggplot2' theme for papers. The data sets of the courses are in the 'simling' package. |
| Authors: | Massimiliano Canzi [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-6124-0321>) |
| Maintainer: | Massimiliano Canzi <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0.9000 |
| Built: | 2026-11-17 17:15:04 UTC |
| Source: | https://github.com/mcanzi/konstats |
Reports your version of R and checks that every package the course uses is installed. It also checks that your version of simling has every data set the slides of the course use. Run it before the first session, and again whenever course code fails on your computer.
check_setup(course = "statistics")check_setup(course = "statistics")
course |
Which course: |
A tibble with one row per package and the columns package,
installed (TRUE or FALSE) and version, invisibly. The function
is called for the report it prints.
check_setup("statistics")check_setup("statistics")
Lists the packages used in the slides and tutorials of each course. The
packages of the Statistics course are installed together with konstats;
the extra packages of Advanced Statistics are installed with
install_course().
course_packages(course = NULL)course_packages(course = NULL)
course |
Which course: |
A tibble with one row per package and course, and the columns:
"statistics" or "advanced".
Name of the package.
What the package is used for in the course.
course_packages() course_packages("advanced")course_packages() course_packages("advanced")
Splits the speakers into k folds, fits a random forest from ranger on
all folds but one, and predicts the tokens of the speakers left out, so
that every token is predicted by a forest that never saw its speaker.
This estimates the accuracy for new speakers, which the out-of-bag
accuracy overestimates when tokens of the same speaker are alike.
cv_speakers( formula, data, speaker = "speaker", k = 5, seed = 2, num_trees = 500 )cv_speakers( formula, data, speaker = "speaker", k = 5, seed = 2, num_trees = 500 )
formula |
Formula of the forest, such as |
data |
A data frame with one row per token. |
speaker |
Name of the column with the speakers, as a string. |
k |
Number of folds. |
seed |
Seed for assigning speakers to folds and for breaking ties
between the votes of the trees. The forest of fold |
num_trees |
Number of trees in each forest. |
When the response is a factor with one value per speaker, such as the L1, each fold gets the same number of speakers from each group. With the default seed, the folds and forests are the ones in the slides of Advanced Statistics, session 6.
A tibble with one row per token and the columns speaker,
fold, observed and predicted. The accuracy for new speakers is
mean(cv$predicted == cv$observed).
if (requireNamespace("ranger", quietly = TRUE)) { library(simling) cv <- cv_speakers(l1 ~ vot + vowel_ratio + rate, accent, num_trees = 100) mean(cv$predicted == cv$observed) }if (requireNamespace("ranger", quietly = TRUE)) { library(simling) cv <- cv_speakers(l1 ~ vot + vowel_ratio + rate, accent, num_trees = 100) mean(cv$predicted == cv$observed) }
Copies the exercise sheet of a tutorial into a folder, where you can open
it and write your answers in the empty chunks. If the sheet has a weekly
assignment, it comes at the end, and you hand in the sheet itself (the
.Rmd file).
get_tutorial( number, dir, course = "statistics", overwrite = FALSE, open = interactive() )get_tutorial( number, dir, course = "statistics", overwrite = FALSE, open = interactive() )
number |
Number of the tutorial, the same as its lecture, such as
|
dir |
Folder to copy the sheets into, such as |
course |
Which course: |
overwrite |
If |
open |
If |
The paths of the sheets in dir, invisibly.
dir <- tempfile() dir.create(dir) get_tutorial(4, dir, open = FALSE) get_tutorial("all", dir) list.files(dir)dir <- tempfile() dir.create(dir) get_tutorial(4, dir, open = FALSE) get_tutorial("all", dir) list.files(dir)
Installs the packages a course uses that are not installed yet, from
CRAN. The packages of the Statistics course come with konstats, so
install_course("advanced") is the one you need: run it once before the
first session of Advanced Statistics.
install_course(course = "advanced", update = FALSE)install_course(course = "advanced", update = FALSE)
course |
Which course: |
update |
If |
The names of the packages it installed, invisibly.
## Not run: install_course("advanced") ## End(Not run)## Not run: install_course("advanced") ## End(Not run)
Lists the lectures of Statistics and the sessions of Advanced Statistics, with the chapters of Winter (2019) they follow, the simling data sets they use and the address of their slides.
lectures(course = NULL)lectures(course = NULL)
course |
Which course: |
A tibble with one row per lecture and the columns:
"statistics" or "advanced".
Number of the lecture.
Topic of the lecture.
Chapters of Winter (2019), Statistics for Linguists: An
Introduction Using R, that the lecture follows. NA for Advanced
Statistics.
simling data sets used in the slides, separated by
commas. penguins comes from palmerpenguins.
TRUE if the lecture has an exercise sheet, see
tutorials().
Address of the slides. NA for sessions still in
preparation.
lectures() lectures("advanced") # lectures that use a given data set lecs <- lectures() lecs[grepl("megastudy", lecs$datasets), c("course", "number", "topic")]lectures() lectures("advanced") # lectures that use a given data set lecs <- lectures() lecs[grepl("megastudy", lecs$datasets), c("course", "number", "topic")]
Sets one predictor to the same value in every row of the data, predicts,
averages the predictions, and repeats for each value (Friedman, 2001).
Plotted against the values, the result shows how the predictions change
with the predictor, averaged over the other predictors. Used in Advanced
Statistics for random forests from ranger, but it works with any model
that has a predict() method.
partial_dependence(model, data, var, values, class = NULL, ...)partial_dependence(model, data, var, values, class = NULL, ...)
model |
A fitted model. For a classification forest from ranger,
fit it with |
data |
The data to predict for, usually the data the model was fitted to. |
var |
Name of the predictor, as a string. |
values |
Values of the predictor to set, such as
|
class |
For models that predict a probability for each class: the
class whose probability to average, such as |
... |
Passed on to |
A tibble with one row per value and the columns value and
prediction, the mean prediction (or probability of class).
Friedman, J. H. (2001). Greedy function approximation: A gradient boosting machine. Annals of Statistics, 29(5), 1189–1232.
library(simling) m <- lm(rt ~ log_freq * length, data = megastudy) partial_dependence(m, megastudy, "length", 3:12) if (requireNamespace("ranger", quietly = TRUE)) { rf <- ranger::ranger(deleted ~ following + morph + rate + age, data = td_deletion, num.trees = 200, probability = TRUE, seed = 1) partial_dependence(rf, td_deletion, "age", seq(20, 80, by = 10), class = "deleted") } # a logistic regression: average the probabilities, not the log-odds m <- glm(deleted ~ age + rate, data = td_deletion, family = binomial) partial_dependence(m, td_deletion, "age", c(20, 50, 80), type = "response")library(simling) m <- lm(rt ~ log_freq * length, data = megastudy) partial_dependence(m, megastudy, "length", 3:12) if (requireNamespace("ranger", quietly = TRUE)) { rf <- ranger::ranger(deleted ~ following + morph + rate + age, data = td_deletion, num.trees = 200, probability = TRUE, seed = 1) partial_dependence(rf, td_deletion, "age", seq(20, 80, by = 10), class = "deleted") } # a logistic regression: average the probabilities, not the log-odds m <- glm(deleted ~ age + rate, data = td_deletion, family = binomial) partial_dependence(m, td_deletion, "age", c(20, 50, 80), type = "response")
Computes the autocorrelation of model residuals within each trajectory,
time series or other group, for lags from 0 to max_lag. At lag 1 it is
the correlation between each residual and the residual one measurement
earlier in the same group; lag 2 skips one measurement, and so on.
Residuals are never paired across groups. Used in Advanced Statistics to
check GAMMs for autocorrelated residuals; itsadug::acf_resid() does the
same with base R graphics.
resid_acf(res, group, max_lag = 8)resid_acf(res, group, max_lag = 8)
res |
Residuals of a model, such as |
group |
The trajectory or series each residual belongs to, such as
|
max_lag |
The largest lag, in measurements. |
The residuals must be in the order of the data, sorted by time within
each group, and res and group must have the same length: if the model
dropped rows with missing values, drop them from group too.
A tibble with one row per lag and the columns lag and acf.
The autocorrelation at lag 0 is always 1. It is NA at lags as long as
the groups, where there are too few pairs of residuals left.
library(simling) # a straight line through the PRICE trajectories: the residuals of # neighbouring measurements are strongly correlated m <- lm(f2 ~ time, data = price_f2) resid_acf(residuals(m), price_f2$traj)library(simling) # a straight line through the PRICE trajectories: the residuals of # neighbouring measurements are strongly correlated m <- lm(f2 ~ time, data = price_f2) resid_acf(residuals(m), price_f2$traj)
Opens the slides of a lecture in your web browser.
slides(lecture, course = "statistics", open = interactive())slides(lecture, course = "statistics", open = interactive())
lecture |
Number of the lecture, or part of its topic, such as
|
course |
Which course: |
open |
If |
The address of the slides, invisibly.
slides(4, open = FALSE) slides("random forests in practice", course = "advanced", open = FALSE)slides(4, open = FALSE) slides("random forests in practice", course = "advanced", open = FALSE)
A plain theme for figures in papers and theses: white background, light
grid without minor lines, bold left-aligned facet labels, a title that
starts at the left edge of the figure and the legend at the bottom. Built in Advanced Statistics,
session 11. ggplot2::theme_set(theme_paper()) makes it the default.
theme_paper(base_size = 11, base_family = "")theme_paper(base_size = 11, base_family = "")
base_size |
Base font size, in points. |
base_family |
Base font family. |
A ggplot2 theme.
library(ggplot2) library(simling) ggplot(vowel_duration, aes(x = voicing, y = duration)) + geom_boxplot() + labs(x = "Following consonant", y = "Vowel duration (ms)") + theme_paper()library(ggplot2) library(simling) ggplot(vowel_duration, aes(x = voicing, y = duration)) + geom_boxplot() + labs(x = "Following consonant", y = "Vowel duration (ms)") + theme_paper()
Lists the exercise sheets that come with konstats. Each sheet has the
exercises of one tutorial and an empty code chunk for each answer: an R
Markdown file (.Rmd) in Statistics, a Quarto document (.qmd) in
Advanced Statistics. RStudio opens and renders both. Some sheets end with a weekly assignment, which you
complete at home. Tutorial 1 of Statistics is a guided tutorial, and its
sheet has all the code. Copy one
into your project folder with get_tutorial(), or all of them with
get_tutorial("all", dir).
tutorials(course = NULL)tutorials(course = NULL)
course |
Which course: |
A tibble with one row per sheet and the columns course,
number (the number of the lecture it belongs to), topic and file.
tutorials()tutorials()