library("readxl")
library("tidyverse")
library("effsize") # for effect sizes
data_vowels <- read.csv("Vowels_Apache.csv", sep = "\t")3.6 t-test
Recommended reading
Agresti & Kateri (2022: Chapter 5.3)
Heumann et al. (2022: Chapter 10.3.3)
Preparation
You can find the full R script associated with this unit here.
Load packages and data:
Overview and hypotheses
Since the \(\chi^2\) measure exclusively works with categorical variables, a separate test statistic is required if one of them is a continuous variable. The \(t\)-statistic is often used for research questions involving differences between sample means.
For instance, we will compare the mean first formant (F1) frequencies of vowels (in Hz) produced by male and female speakers of Apache. We put forward the following hypotheses about the population means:
\(H_0:\) \(\mu_{\text{men}} = \mu_{\text{women}}\)
\(H_1:\) \(\mu_{\text{men}} \neq \mu_{\text{women}}\)
These are so-called two-sided hypotheses: We are only interested in whether or not the means are different, regardless of one mean being higher or lower than the other. By contrast, a one-sided alternative hypothesis predicts an effect in a specific direction (e.g., \(\mu_{\text{men}} > \mu_{\text{women}}\) or \(\mu_{\text{men}} < \mu_{\text{women}}\)).
The \(t\)-test
The way \(t\) is calculated depends on the sources of \(X\) and \(Y\): Do they come from two independent samples, or are the observations paired (e.g., two measurements from the same speakers)?
First, we consider two independent samples from two populations:
Sample \(X\) with the observations \((x_1, x_2, \dots, x_{n_x})\), sample size \(n_x\), sample mean \(\hat{\mu}_{x}\) and sample variance \(\hat{\sigma}^2_x\).
Sample \(Y\) with the observations \((y_1, y_2, \dots, y_{n_y})\), sample size \(n_y\), sample mean \(\hat{\mu}_{y}\) and sample variance \(\hat{\sigma}^2_y\).
Descriptive overview
We select the variables of interest and proceed to calculate the mean F1 frequencies for each level of SEX, requiring a grouped data frame.
Code
# Filter data so as to show only those observations that are relevant
data_vowels %>%
# Filter columns
select(HZ_F1, SEX) %>%
# Define grouping variable
group_by(SEX) %>%
# Compute mean and standard deviation for each sex
summarise(mean = mean(HZ_F1),
sd = sd(HZ_F1)) -> data_vowels_stats
knitr::kable(data_vowels_stats)| SEX | mean | sd |
|---|---|---|
| F | 528.8548 | 110.80099 |
| M | 484.2740 | 87.90112 |
Code
# Plot means and standard deviations
data_vowels_stats %>%
ggplot(aes(x = SEX, y = mean)) +
geom_col() +
geom_errorbar(aes(x = SEX,
ymin = mean-sd,
ymax = mean+sd), width = .2) +
theme_classic()Code
# Plot quartiles
data_vowels %>%
ggplot(aes(x = SEX, y = HZ_F1)) +
geom_boxplot() +
theme_classic()Two-sample \(t\)-test
Suppose that the observations in samples \(X\) and \(Y\) each follow a normal distribution.
The rationale of the \(t\)-test is then to check how many standard errors the two sample means \(\hat{\mu}_{x}\) and \(\hat{\mu}_{y}\) are apart. In short:
\[ t = \frac{\hat{\mu}_{x}-\hat{\mu}_{y}}{se} \tag{1}\]
The difficulty lies in selecting an appropriate estimate of the standard error. In the literature, the following scenarios are distinguished (Heumann, Schomaker, and Shalabh 2022: 232–233):
- The population variances are known: In this case, the standard normal distribution can be used instead of the \(t\) distribution. However, this is hardly ever the case; instead, we tend to rely on sample estimates (cf. next point).
- The population variances are unknown, but equal: As long as the variances (and hence the standard deviations) of the two group samples are not too different, we can estimate the standard error via the pooled variance \(\hat{\sigma}_{\text{pooled}}^2\):
\[ \hat{\sigma}_{\text{pooled}}^2 = \frac{(n_x - 1) \hat{\sigma}^2_x + (n_y - 1) \hat{\sigma}_y^2}{n_x + n_y - 2} \tag{2}\]
Then the standard error corresponds to
\[ se = \sqrt{\frac{\hat{\sigma}_{\text{pooled}}^2}{n_x} + \frac{\hat{\sigma}_{\text{pooled}}^2}{n_y}} \tag{3}\]
# Two-sample test with equal variances
apache_test1 <- t.test(data_vowels$HZ_F1 ~ data_vowels$SEX,
var.equal = TRUE)
print(apache_test1)
Two Sample t-test
data: data_vowels$HZ_F1 by data_vowels$SEX
t = 2.4416, df = 118, p-value = 0.01611
alternative hypothesis: true difference in means between group F and group M is not equal to 0
95 percent confidence interval:
8.423043 80.738624
sample estimates:
mean in group F mean in group M
528.8548 484.2740
- The population variances are unknown and unequal: According to Agresti & Kateri (2022: 171), it is better to employ a different \(se\) estimate “[i]f the data show evidence of greatly different standard deviations (with, say, one being at least 50% larger than the other one).” In short, we replace the pooled variance by the two separate sample variances \(\hat{\sigma}_x^2\) and \(\hat{\sigma}_y^2\) (cf. Equation 4). This variant is known as Welch’s \(t\)-test. Its degrees of freedom are approximated from the data, which is why they are usually not a whole number (e.g., \(df = 112.19\)). Since it performs well even if the variances are equal, it is the default in R’s
t.test().
\[ se = \sqrt{\frac{\hat{\sigma}_x^2}{n_x} + \frac{\hat{\sigma}_y^2}{n_y}} \tag{4}\]
# Welch Two Sample t-test
apache_test2 <- t.test(data_vowels$HZ_F1 ~ data_vowels$SEX,
var.equal = FALSE)
print(apache_test2)
Welch Two Sample t-test
data: data_vowels$HZ_F1 by data_vowels$SEX
t = 2.4416, df = 112.19, p-value = 0.01619
alternative hypothesis: true difference in means between group F and group M is not equal to 0
95 percent confidence interval:
8.403651 80.758016
sample estimates:
mean in group F mean in group M
528.8548 484.2740
Paired \(t\)-test
If there are two observations for each subject (e.g., before and after an experiment), the samples are called paired (or dependent). Here, \(d_i = x_i - y_i\) denotes the difference between the two measurements for the \(i\)-th pair. The corresponding \(t\)-statistic is obtained by relating the mean difference \(\bar{d} = \frac{1}{n}\sum_{i=1}^n{d_i}\) to its standard error \(\frac{\hat{\sigma}_d}{\sqrt{n}}\):
\[ t(x, y) = t(d) = \frac{\bar{d}}{\hat{\sigma}_d} \sqrt{n}. \tag{5}\]
The standard deviation of the differences is estimated by
\[ \hat{\sigma}_d = \sqrt{\frac{\sum_{i=1}^n({d_i} - \bar{d})^2}{n-1}}. \tag{6}\]
In R, a paired \(t\)-test is performed by supplying the two vectors of paired measurements (in the same order) and setting paired = TRUE:
t.test(x, y, paired = TRUE)Assumptions
Traditionally, the \(t\)-test is based on the following assumptions:
- The observations are independent of each other.
- The data in each group are (approximately) normally distributed.
- The population variances of both groups are equal (variance homogeneity).
The first assumption is particularly important for linguistic data: If, for example, several vowel tokens were recorded from each speaker, the observations from the same speaker are not independent. In such cases, mixed-effects models are more appropriate (see 4.3 Mixed-effects regression).
For reasonably large samples, the \(t\)-test is fairly robust to deviations from normality (thanks to the Central Limit Theorem, see 3.4 Hypothesis testing). Welch’s \(t\)-test additionally handles unequal variances.
If the violations are substantial, it is advisable to use a non-parametric test such as the Wilcoxon-Mann-Whitney (WMW) U-test wilcox.test() instead. In essence, this test assesses the probability that a randomly chosen value from sample \(X\) is greater than a randomly chosen value from sample \(Y\) (under \(H_0\), this probability is 0.5).
Yes, there are formal tests that allow us to test for (non-)normality and (un-)equal variances. However, a visual inspection of some diagnostic plots (e.g., histograms or qq-plots) is usually more than enough!
# Normality is assumed within each group, so we check each SEX separately
# Shapiro-Wilk test of normality for each group
# H0: the data come from a normally distributed population
tapply(data_vowels$HZ_F1, data_vowels$SEX, shapiro.test)$F
Shapiro-Wilk normality test
data: X[[i]]
W = 0.98699, p-value = 0.7723
$M
Shapiro-Wilk normality test
data: X[[i]]
W = 0.97239, p-value = 0.1907
# Check histograms
ggplot(data_vowels, aes(x = HZ_F1)) +
geom_histogram(bins = 30) +
facet_wrap(~SEX) +
theme_classic()# Check quantile-quantile plots
ggplot(data_vowels, aes(sample = HZ_F1)) +
stat_qq() +
stat_qq_line(col = "red") +
facet_wrap(~SEX) +
theme_classic()# F test to compare two variances
var.test(data_vowels$HZ_F1 ~ data_vowels$SEX) # H0: the two population variances are equal
F test to compare two variances
data: data_vowels$HZ_F1 by data_vowels$SEX
F = 1.5889, num df = 59, denom df = 59, p-value = 0.07789
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.949093 2.660040
sample estimates:
ratio of variances
1.588907
Effect size
Cohen’s d is a possible effect size measure for continuous data and is obtained by dividing the difference of both sample means by the pooled standard deviation.
cohen.d(data_vowels$HZ_F1, data_vowels$SEX) # see also ?cohen.d for more details
Cohen's d
d estimate: 0.4457697 (small)
95 percent confidence interval:
lower upper
0.07976048 0.81177897
The documentation of the function (type ?cohen.d into the console for details) lists the following interpretation guidelines:
- \(|d| < 0.2\): negligible effect
- \(|d| < 0.5\): small effect
- \(|d| < 0.8\): moderate effect
- \(|d| \geq 0.8\): large effect
Reporting the results
According to Welch’s two-sample \(t\)-test, there is a significant difference between the mean F1 frequencies of male and female speakers of Apache (\(t(112.19) = 2.44\), \(p = 0.02\)), yet the effect is rather small (Cohen’s \(d = 0.45\)). This provides sufficient evidence to reject the null hypothesis at \(\alpha = 0.05\).
Exercises
Exercise 1 How does the \(t\)-test differ from the chi-squared test in terms of the type of data and research questions it addresses?
Exercise 2 You are investigating whether high-frequency words are processed faster than low-frequency words in a lexical decision task. State the null and alternative hypotheses for:
- A two-sided test comparing reaction times
- A one-sided test (assuming high-frequency words are processed faster)
Exercise 3 Load the psycholing_data into R:
psycholing_data <- read_xlsx("psycholing_data.xlsx")
# Inspect dataset
str(psycholing_data)
head(psycholing_data)The Dispersion column indicates how evenly words are distributed across text categories in a corpus. We will perform a (rather crude) division into words with high dispersion (‘even’) and low dispersion (‘uneven’).
# Create dispersion bins
psycholing_data <- psycholing_data %>%
mutate(
Dispersion_bin = case_when(
is.na(Dispersion) ~ NA_character_, # keep missing values as missing
Dispersion <= quantile(Dispersion, 0.5, na.rm = TRUE) ~ "even",
TRUE ~ "uneven"
),
# Convert to factor with proper ordering
Dispersion_bin = factor(Dispersion_bin,
levels = c("even", "uneven"))
)The variety of contexts in which a word can occur is likely influenced by the word’s number of meanings. Investigate the relationship between Dispersion_bin and Number_meanings by
- formulating two-sided (non-directional) hypotheses;
- computing and visualising the means and standard deviations of
Number_meaningsfor each dispersion bin; - conducting an appropriate statistical test and assessing the extent to which its assumptions are violated;
- reporting the results;
- giving a brief linguistic interpretation.