library(readxl)
library(tidyverse)
library(effectsize) # for effect sizes
psycholing_data <- read_xlsx("psycholing_data.xlsx")3.7 ANOVA
Recommended reading
Kirk (1982: Chapter 5)
Mendenhall et al. (1986: Chapter 13)
Preparation
Why ANOVA?
The \(t\)-test (see 3.6 t-test) only compares the means of two groups. For comparisons between three or more groups, analysis of variance (ANOVA) is the method of choice.
But couldn’t we simply run several \(t\)-tests, one for each pair of groups? With four groups, this would amount to six \(t\)-tests. The problem is that every single test comes with a 5% risk of a Type I error (see 3.4 Hypothesis testing). Across six tests, the probability of obtaining at least one false positive therefore rises to approximately \(1 - 0.95^6 \approx 26\%\). ANOVA avoids this problem by testing all group means simultaneously in a single test.
Data and hypotheses
The following dataset comprises various semantic and distributional measures for 407 transitive verbs. We will group the verbs into four frequency bins based on the quartiles of their frequency distribution: ‘low’, ‘moderate’, ‘high’ and ‘very high’.
Code
# Create frequency bins
psycholing_data <- psycholing_data %>%
mutate(
Frequency_bin = case_when(
is.na(Frequency) ~ NA_character_, # keep missing values as missing
Frequency <= quantile(Frequency, 0.25, na.rm = TRUE) ~ "low",
Frequency <= quantile(Frequency, 0.50, na.rm = TRUE) ~ "moderate",
Frequency <= quantile(Frequency, 0.75, na.rm = TRUE) ~ "high",
TRUE ~ "very_high"
),
# Convert to factor with proper ordering
Frequency_bin = factor(Frequency_bin,
levels = c("low", "moderate", "high", "very_high"))
)
# Show some examples
psycholing_data %>%
select(Verb, Frequency_bin) %>%
head(n = 10)# A tibble: 10 × 2
Verb Frequency_bin
<chr> <fct>
1 accelerate moderate
2 accept very_high
3 accompany low
4 accumulate moderate
5 acquire high
6 adore low
7 advance low
8 advise high
9 affect high
10 alter high
The column Number_meanings represents the number of distinct word senses listed in the WordNet database. We will examine whether the mean number of meanings \(\mu\) differs across the four frequency bins:
\(H_0: \mu_{\text{low}} = \mu_{\text{moderate}} = \mu_{\text{high}} = \mu_{\text{very high}}\)
\(H_1:\) At least one group mean differs from the others.
From a descriptive point of view, there is indeed some evidence that the number of meanings and frequency are linked:
Code
psycholing_data %>%
group_by(Frequency_bin) %>%
summarise(
n = n(),
mean_meanings = mean(Number_meanings, na.rm = TRUE),
sd_meanings = sd(Number_meanings, na.rm = TRUE),
median_meanings = median(Number_meanings, na.rm = TRUE),
.groups = 'drop'
)# A tibble: 4 × 5
Frequency_bin n mean_meanings sd_meanings median_meanings
<fct> <int> <dbl> <dbl> <dbl>
1 low 123 5.20 4.17 4
2 moderate 81 7.23 5.12 6
3 high 103 10.2 7.57 8
4 very_high 100 16.6 14.0 12
Code
ggplot(psycholing_data, aes(x = Frequency_bin, y = Number_meanings)) +
geom_boxplot() +
geom_jitter(width = 0.2, alpha = 0.5) +
stat_summary(
fun = mean,
geom = "point",
shape = 23, # filled diamond
size = 3,
fill = "white",
color = "black",
stroke = 0.8
) +
labs(
title = "Polysemy of Transitive Verbs by Frequency",
subtitle = "Bins based on frequency quartiles; diamonds indicate means",
x = "Frequency Bin",
y = "Number of meanings"
) +
theme_minimal()The logic of ANOVA
Despite its name, ANOVA tests a hypothesis about means. It does so by comparing two sources of variability in the data:
- the variability between the groups, i.e., how far the group means lie from the overall mean, and
- the variability within the groups, i.e., how far the individual observations lie from their own group mean.
If the group means are truly equal (\(H_0\)), the differences between the group means should be no larger than what we would expect from the random variability within the groups.
Let \(y_{ij}\) denote the \(j\)-th observation in group \(i\), \(n_i\) the size of group \(i\), \(k\) the number of groups and \(n = n_1 + n_2 + \dots + n_k\) the total sample size. Furthermore, let \(\bar{y}\) denote the overall (grand) mean of all observations and \(\bar{T}_i\) the sample mean of the \(i\)-th group.
The total sum of squares captures the overall variability of the data, i.e., the squared deviations of all observations from the grand mean:
\[ \text{Total SS} = \sum_{i=1}^k \sum_{j=1}^{n_i} (y_{ij} - \bar{y})^2 \tag{1}\]
It can be split up into two components. The sum of squares for treatments (SST) captures the variability between the groups:
\[ \text{SST} = \sum_{i=1}^k n_i(\bar{T}_i - \bar{y})^2 \tag{2}\]
The sum of squares for error (SSE) captures the variability within the groups:
\[ \text{SSE} = \sum_{i=1}^k\sum_{j=1}^{n_i} (y_{ij} - \bar{T}_i)^2 \tag{3}\]
Crucially, \(\text{Total SS} = \text{SST} + \text{SSE}\). Dividing each sum of squares by its degrees of freedom yields the mean square for treatments (MST) and the mean square for error (MSE):
\[ \text{MST} = \frac{\text{SST}}{k-1}, \qquad \text{MSE} = \frac{\text{SSE}}{n-k} \tag{4}\]
The test statistic is their ratio:
\[ F = \frac{\text{MST}}{\text{MSE}} \tag{5}\]
Under \(H_0\), \(F\) follows an \(F\)-distribution with \(k-1\) and \(n-k\) degrees of freedom and should be close to 1. The larger the differences between the group means relative to the variability within the groups, the larger \(F\) becomes, and the smaller the \(p\)-value.
ANOVA in R
R’s aov() function performs the \(F\)-test. Its syntax resembles that of t.test(): The dependent variable is placed to the left of the tilde ~ and the grouping variable to its right.
anova_model <- aov(Number_meanings ~ Frequency_bin, data = psycholing_data)
summary(anova_model) Df Sum Sq Mean Sq F value Pr(>F)
Frequency_bin 3 7730 2576.5 35.17 <2e-16 ***
Residuals 403 29525 73.3
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The output contains the degrees of freedom (Df), the sums of squares (Sum Sq) and mean squares (Mean Sq) for the grouping variable (Frequency_bin, i.e., SST and MST) and the residuals (Residuals, i.e., SSE and MSE), followed by the \(F\)-value and the \(p\)-value.
ANOVA is in fact a special case of the linear regression model with a categorical predictor (see 4.1 Linear regression). Fitting lm(Number_meanings ~ Frequency_bin, data = psycholing_data) yields exactly the same \(F\)-test, which can be found at the bottom of the summary() output.
Effect size
As with the \(t\)-test, the \(p\)-value does not tell us how strong the effect is. A common effect size measure for ANOVA is \(\eta^2\) (‘eta squared’), which indicates the proportion of the total variability that is accounted for by the grouping variable:
\[ \eta^2 = \frac{\text{SST}}{\text{Total SS}} \tag{6}\]
We can compute it with eta_squared() from the effectsize package:
eta_squared(anova_model)For one-way between subjects designs, partial eta squared is equivalent
to eta squared. Returning eta squared.
# Effect Size for ANOVA
Parameter | Eta2 | 95% CI
-----------------------------------
Frequency_bin | 0.21 | [0.15, 1.00]
- One-sided CIs: upper bound fixed at [1.00].
Note that eta_squared() reports a one-sided confidence interval by default, which is why its upper bound is always 1.
According to a one-way ANOVA, there is a statistically significant effect of frequency on the number of meanings (\(F(3, 403) = 35.17\), \(p < 0.001\), \(\eta^2 = 0.21\)).
According to commonly used rules of thumb, \(\eta^2\) values of around 0.01, 0.06 and 0.14 indicate small, medium and large effects, respectively. As with all such guidelines, these thresholds should be taken with a grain of salt.
Assumptions
ANOVA makes the same assumptions as the classic two-sample \(t\)-test (see 3.6 t-test):
- The observations are independent of each other.
- The data within each group are (approximately) normally distributed.
- The variances of all groups are equal (variance homogeneity).
Variance homogeneity can be checked with Bartlett’s test (H0: all group variances are equal). If it is violated, Welch’s ANOVA, which does not assume equal variances, is a robust alternative:
# Test for equal variances
bartlett.test(Number_meanings ~ Frequency_bin, data = psycholing_data)
# Welch's ANOVA
oneway.test(Number_meanings ~ Frequency_bin, data = psycholing_data, var.equal = FALSE)If the data are strongly non-normal, the Kruskal-Wallis test offers a non-parametric alternative, analogous to the Wilcoxon-Mann-Whitney test for two groups:
kruskal.test(Number_meanings ~ Frequency_bin, data = psycholing_data)Note that Number_meanings consists of counts, which are typically right-skewed. For count data, Poisson regression is often a more appropriate model (see 4.4 Poisson regression).
Post-hoc tests
A significant ANOVA result tells us that at least one group mean differs from the others, but it does not specify which groups differ. Post-hoc tests address this by examining all pairwise comparisons between the groups, while correcting for multiple testing.
A widely used option is Tukey’s Honest Significant Difference (HSD) test:
TukeyHSD(anova_model) Tukey multiple comparisons of means
95% family-wise confidence level
Fit: aov(formula = Number_meanings ~ Frequency_bin, data = psycholing_data)
$Frequency_bin
diff lwr upr p adj
moderate-low 2.039446 -1.1202279 5.199120 0.3435898
high-low 4.969927 2.0207193 7.919134 0.0001026
very_high-low 11.374878 8.4016918 14.348064 0.0000000
high-moderate 2.930481 -0.3487354 6.209697 0.0984694
very_high-moderate 9.335432 6.0346335 12.636231 0.0000000
very_high-high 6.404951 3.3050232 9.504880 0.0000010
For each pair of groups, the output shows the difference between the means (diff), its 95% confidence interval (lwr, upr) and the adjusted \(p\)-value (p adj). A difference is significant at \(\alpha = 0.05\) if its confidence interval does not include 0.
Exercises
Exercise 1 Interpret the results of Tukey’s HSD test above: Which frequency groups differ significantly from each other with regard to their mean number of meanings? How do these results relate to the overall ANOVA result?
Exercise 2 Check whether the assumptions of the ANOVA are met for the data above. Based on your findings, would you consider Welch’s ANOVA or the Kruskal-Wallis test more appropriate? Perform the test of your choice and compare its result to that of the classic ANOVA.