- Explain why comparing several means with many t tests inflates the chance of a false positive, and compute the inflation.
- Describe one-way ANOVA as a comparison of variation between groups with variation within groups.
- Partition total variation into \(\text{SST} = \text{SSB} + \text{SSW}\), and compute mean squares, the F statistic, and a complete ANOVA table.
- Use the F distribution to find p-values, and interpret a significant or non-significant F test correctly.
- Check the conditions for ANOVA and recognize when they fail.
- Measure effect size with \(\eta^2\) (and Cohen's \(f\)).
- Follow a significant F with Tukey or Bonferroni multiple comparisons and report which groups differ.
- Read a two-way ANOVA table and interpret main effects and interaction.
Chapter 11 compared two means. Real questions often involve three or more groups: four fertilizers, five teaching methods, three dosage levels. This chapter develops analysis of variance (ANOVA), the standard method for asking, in a single test, whether several population means are all equal. Despite the name, ANOVA is a test about means. It gets its name from how it works: by comparing two kinds of variation.
Why not just run many t tests?
With four groups you could compare every pair with a two-sample t test: A versus B, A versus C, and so on, six tests in all. The trouble is that each test at \(\alpha = 0.05\) has a 5 percent chance of a false alarm when the null is true, and the chances accumulate. If the tests were independent, the probability of at least one false positive among \(m\) tests, all at level \(\alpha\), is
\[P(\text{at least one Type I error}) = 1 - (1 - \alpha)^m.\]With \(k\) groups there are \(m = \binom{k}{2} = k(k-1)/2\) pairs, so the number of tests grows quickly:
| Groups \(k\) | 2 | 3 | 4 | 5 | 6 | 8 | 10 |
|---|---|---|---|---|---|---|---|
| Pairs \(m\) | 1 | 3 | 6 | 10 | 15 | 28 | 45 |
| \(1 - 0.95^m\) | 0.050 | 0.143 | 0.265 | 0.401 | 0.537 | 0.762 | 0.901 |
With just five groups, all truly identical, the chance of finding at least one "significant" pair is about 40 percent. (The pairwise tests are not exactly independent, so the true rate is a little lower, but the message stands: the simulation below shows it.) The probability of at least one false positive among a family of tests is called the family-wise error rate. ANOVA solves the problem for the first question, "are any of the means different?", by using one test with a single \(\alpha\). If it rejects, we can follow up with pairwise comparisons that are corrected to keep the family-wise error rate at \(\alpha\) (Section 13.7).
The idea: between-group and within-group variation
Suppose we have \(k\) groups, with \(n_i\) observations in group \(i\) and \(N = \sum n_i\) observations overall. Write \(x_{ij}\) for the \(j\)th observation in group \(i\), \(\bar{x}_i\) for the mean of group \(i\), and \(\bar{x}\) for the grand mean of all \(N\) observations.
\(H_0\!: \mu_1 = \mu_2 = \dots = \mu_k\) (all population means are equal).
\(H_a\!:\) at least one population mean differs from the others.
The alternative is not "all means differ": a single group standing apart is enough. And ANOVA does not say which means differ; that comes afterwards.
Two pictures of the same total amount of scatter show the logic.
- Group means far apart, points tightly clustered around their own means. Most of the variation in the data is between groups. The group labels explain the data well. This looks like real differences.
- Group means close together, points widely scattered around them. Most of the variation is within groups, and the differences between the sample means are the kind you would see by chance even if all population means were equal.
ANOVA quantifies this by comparing the variability of the group means (scaled appropriately) with the variability of the observations around their own group means. The ratio of the two is the F statistic. If \(H_0\) is true, the ratio should be near 1; if the means truly differ, the between-group variation is inflated and the ratio is large.
Sums of squares, mean squares, and the F statistic
The identity is exact, and it is the Pythagorean theorem in disguise (the between and within parts of each deviation are perpendicular). SSB measures how far the group means are from the grand mean, weighted by group size. SSW measures how far each observation is from its own group's mean, and equals \(\sum (n_i - 1)s_i^2\).
Sums of squares depend on how many terms were added, so each is divided by its degrees of freedom to give a mean square:
| Source | Sum of squares | df | Mean square | F |
|---|---|---|---|---|
| Between groups | SSB | \(k - 1\) | \(\text{MSB} = \text{SSB}/(k-1)\) | \(F = \text{MSB}/\text{MSW}\) |
| Within groups (error) | SSW | \(N - k\) | \(\text{MSW} = \text{SSW}/(N-k)\) | |
| Total | SST | \(N - 1\) |
Both mean squares estimate the common variance \(\sigma^2\) when \(H_0\) is true. MSW always does (it pools the within-group variances, exactly like the pooled variance of Chapter 11), while MSB estimates \(\sigma^2\) only if the means are equal; otherwise it estimates something larger. Hence \[E(\text{MSW}) = \sigma^2, \qquad E(\text{MSB}) = \sigma^2 + \frac{\sum n_i(\mu_i - \mu)^2}{k - 1},\] and the F ratio is near 1 under \(H_0\) and larger when means differ.
If the conditions hold and \(H_0\) is true, \(F = \text{MSB}/\text{MSW}\) follows the F distribution with \(\text{df}_1 = k - 1\) (numerator) and \(\text{df}_2 = N - k\) (denominator) degrees of freedom. The p-value is the right-tail area \(P(F_{k-1,\,N-k} \ge F_{\text{obs}})\). We look only at the right tail: a small F (means suspiciously close) is not evidence against \(H_0\).
The F distribution is right-skewed, takes only positive values, and is described by two degrees of freedom (numerator first). Its mean is about 1 (precisely \(\text{df}_2/(\text{df}_2 - 2)\)) when the denominator degrees of freedom are large. Notice the link to earlier chapters: the square of a t statistic with \(\nu\) degrees of freedom has an F distribution with 1 and \(\nu\) degrees of freedom, so with two groups, ANOVA gives exactly the same p-value as the pooled two-sample t test, with \(F = t^2\). ANOVA is the generalization of that test to \(k\) groups.
A complete example
Example 13.1: Four fertilizers (synthetic data)
A grower randomly assigns 24 identical plots to four fertilizer formulas (6 plots each) and measures the yield of each plot (kilograms). The data are synthetic:
| Formula | Yields | \(n\) | Mean | SD |
|---|---|---|---|---|
| A (control) | 20.1, 22.4, 19.8, 21.5, 23.0, 20.6 | 6 | 21.23 | 1.29 |
| B | 24.3, 25.1, 22.9, 26.4, 24.8, 25.6 | 6 | 24.85 | 1.19 |
| C | 22.0, 23.5, 21.2, 24.1, 22.8, 23.3 | 6 | 22.82 | 1.06 |
| D | 26.9, 28.2, 25.5, 27.3, 29.0, 26.4 | 6 | 27.22 | 1.25 |
Hypotheses. \(H_0\!: \mu_A = \mu_B = \mu_C = \mu_D\) versus \(H_a\!:\) at least one mean differs. Conditions. Randomly assigned plots (independent); the sample SDs (1.06 to 1.29) are very similar, so the equal-variance condition is fine, and there is no sign of strong skewness or outliers in samples this small.
Sums of squares. The grand mean is \(\bar{x} = 24.029\).
\(\text{SSB} = 6[(21.233 - 24.029)^2 + (24.850 - 24.029)^2 + (22.817 - 24.029)^2 + (27.217 - 24.029)^2] = 120.72\).
\(\text{SSW} = 5(1.288^2 + 1.195^2 + 1.061^2 + 1.254^2) = 28.93\). \(\text{SST} = 120.72 + 28.93 = 149.65\).
| Source | SS | df | MS | F | p-value |
|---|---|---|---|---|---|
| Between formulas | 120.72 | 3 | 40.24 | 27.82 | \(2.4 \times 10^{-7}\) |
| Within (error) | 28.93 | 20 | 1.446 | ||
| Total | 149.65 | 23 |
Conclusion. \(F = 40.24/1.446 = 27.82\) with 3 and 20 degrees of freedom, well beyond the 5 percent critical value 3.098. There is overwhelming evidence that the mean yield is not the same for all four formulas. Because the plots were randomly assigned, the differences can be attributed to the formulas. The test does not say which formulas differ; Section 13.7 continues this example.
Example 13.2: When the means are not convincingly different (synthetic data)
Three groups of 5 have observations (12, 15, 11, 14, 13), (13, 16, 12, 15, 17) and (14, 11, 15, 13, 12). The group means are 13.0, 14.6 and 13.0 with SDs 1.58, 2.07 and 1.58; the grand mean is 13.53.
\(\text{SSB} = 5[(13 - 13.53)^2 + (14.6 - 13.53)^2 + (13 - 13.53)^2] = 8.53\); \(\text{SSW} = 4(1.58^2 + 2.07^2 + 1.58^2) = 37.2\); \(\text{MSB} = 8.53/2 = 4.27\); \(\text{MSW} = 37.2/12 = 3.10\).
\(F = 4.27/3.10 = 1.376\) with 2 and 12 degrees of freedom, and \(p = 0.290\). The differences among the sample means (13.0, 14.6, 13.0) are just what chance produces when the variation within groups is this large, so we fail to reject \(H_0\). Compare with Example 13.1, where the group means were separated by several times the within-group SD.
Conditions for ANOVA
The F distribution describes the ratio only when these conditions are reasonable:
- Independence. Observations are independent within and between groups (random sampling or randomized assignment; no repeated measurements on the same subject, which need repeated-measures ANOVA).
- Normality within each group. The populations are approximately normal. As with the t procedures of Chapter 9, ANOVA is quite robust to mild skewness, especially when the group sizes are equal and not tiny. Check with side-by-side boxplots or dotplots and normal probability plots of residuals; beware of strong skewness and outliers in small groups.
- Equal variances (homoscedasticity). The populations have a common standard deviation \(\sigma\). A practical rule: if the largest sample SD is less than about twice the smallest, the test is reliable. With unequal group sizes the assumption matters more (the larger variance in a small group is the dangerous case). Formal checks include Levene's test; if the variances are clearly unequal, use Welch's ANOVA, which does not assume equal variances.
Draw side-by-side boxplots or dotplots before computing anything. They reveal outliers, skewness, unequal spreads, and whether a significant F reflects one odd group or a clear pattern. If the conditions fail badly, alternatives include a transformation (such as the logarithm), Welch's ANOVA, the permutation approach of Chapter 11 applied to F, or the rank-based Kruskal-Wallis test (Chapter 15).
Effect size: how much do the groups explain?
As with every test, a small p-value says the differences are unlikely to be chance but not how large they are. The proportion of the total variation explained by the grouping is the natural measure:
\[\eta^2 = \frac{\text{SSB}}{\text{SST}}.\]It is the R-squared of ANOVA: 0 means the groups explain nothing; 1 means every observation equals its group mean. Cohen's rough benchmarks are 0.01 (small), 0.06 (medium), and 0.14 (large). A related index is \(f = \sqrt{\eta^2/(1 - \eta^2)}\), with benchmarks 0.10, 0.25 and 0.40. \(\eta^2\) tends to overestimate the population value in small samples, and the less biased \(\omega^2 = \dfrac{\text{SSB} - (k-1)\text{MSW}}{\text{SST} + \text{MSW}}\) is preferred in careful work.
For Example 13.1, \(\eta^2 = 120.72/149.65 = 0.807\) (formula explains 81 percent of the variation in yield; \(\omega^2 = 0.770\), \(f = 2.04\)), a huge effect. For Example 13.2, \(\eta^2 = 8.53/45.73 = 0.187\): the sample groups differ by a moderate to large amount, but with only 5 per group we cannot distinguish that from chance.
After a significant F: multiple comparisons
A significant F says that not all means are equal. To learn which pairs differ, compare all pairs of means, but adjust for the fact that you are making \(m = k(k-1)/2\) comparisons. All the methods below use the pooled variance MSW (with \(N - k\) degrees of freedom) and produce intervals for \(\mu_i - \mu_j\).
| Method | Margin of error for \(\bar{x}_i - \bar{x}_j\) | Comment |
|---|---|---|
| Unadjusted (Fisher's LSD) | \(t^*_{N-k}\sqrt{\text{MSW}(1/n_i + 1/n_j)}\) with \(t^*\) at level \(\alpha/2\) per tail | No protection: family-wise error rate is inflated. Reasonable only after a significant F and with few groups. |
| Bonferroni | Same, but \(t^*\) with upper-tail area \(\alpha/(2m)\) | Simple and always valid (conservative). Equivalent to testing each pair at level \(\alpha/m\). |
| Tukey's HSD | \(q^*_{k,\,N-k}\sqrt{\dfrac{\text{MSW}}{2}\left(\dfrac{1}{n_i} + \dfrac{1}{n_j}\right)}\) with \(q^*\) from the studentized range distribution | Designed for all pairwise comparisons; controls the family-wise error rate exactly with equal group sizes and is more powerful than Bonferroni when \(m\) is large. |
For equal group sizes \(n\), Tukey's honestly significant difference is \(\text{HSD} = q^*\sqrt{\text{MSW}/n}\): any pair of sample means differing by more than HSD is declared different.
Example 13.3: Which fertilizers differ? (continuing Example 13.1)
Here \(k = 4\), \(N - k = 20\), \(n = 6\) per group, \(\text{MSW} = 1.446\), and \(m = 6\) pairs. The critical value of the studentized range for \(k = 4\) and 20 degrees of freedom at the 5 percent level is \(q^* = 3.958\), so
\[\text{HSD} = 3.958\sqrt{1.446/6} = 1.943.\]The unadjusted margin would be \(2.086\sqrt{2(1.446)/6} = 1.448\), and the Bonferroni margin uses \(t^* = 2.927\) (upper-tail area \(0.05/12\)): \(2.927 \times 0.6943 = 2.032\).
| Pair | Difference | Tukey p-value | Tukey 95% interval | Verdict |
|---|---|---|---|---|
| B - A | 3.617 | < 0.001 | (1.67, 5.56) | differ |
| C - A | 1.583 | 0.136 | (-0.36, 3.53) | not shown to differ |
| D - A | 5.983 | < 0.001 | (4.04, 7.93) | differ |
| B - C | 2.033 | 0.038 | (0.09, 3.98) | differ |
| D - B | 2.367 | 0.014 | (0.42, 4.31) | differ |
| D - C | 4.400 | < 0.001 | (2.46, 6.34) | differ |
Five of the six pairs differ. The only pair that cannot be separated is A versus C (difference 1.58, less than HSD 1.94). Notice that the unadjusted method (margin 1.448) would have flagged A versus C as different too, an example of the inflation in Section 13.1. Formula D has the highest mean yield and beats every other formula; the point estimates order the formulas D > B > C > A.
If you decided before seeing the data on a small number of specific comparisons (for example, control versus the average of the treatments), you may test just those with a proper contrast, needing less adjustment than an all-pairs search. Comparisons suggested by looking at the data must be treated as if you had made all \(m\) of them, as Tukey or Bonferroni do. Whichever method you use, say which and why.
Two-way ANOVA: an overview
One-way ANOVA has a single factor (fertilizer formula). Many experiments vary two factors at once, for instance study method (flashcards or practice tests) and amount of sleep (normal or short). Two-way ANOVA splits the total variation into four parts: the main effect of factor A, the main effect of factor B, their interaction A \(\times\) B, and error:
\[\text{SST} = \text{SS}_A + \text{SS}_B + \text{SS}_{AB} + \text{SS}_E.\]The interaction is the new idea. There is an interaction when the effect of one factor depends on the level of the other. On an interaction plot (cell means for each combination, with lines joining the levels of one factor for each level of the other), parallel lines mean no interaction; lines that cross or diverge signal interaction. When an interaction is present, main effects must be interpreted with care: "the effect of sleep" is not one number but depends on the study method.
Example 13.4: Study method and sleep (synthetic data)
Twenty students are randomly assigned, five to each combination of study method (flashcards, practice tests) and sleep (normal, short) and then take the same test. Scores (synthetic):
| Normal sleep | Short sleep | Row mean | |
|---|---|---|---|
| Flashcards | 74, 70, 77, 72, 75 (mean 73.6) | 62, 66, 60, 64, 63 (mean 63.0) | 68.3 |
| Practice tests | 82, 79, 84, 80, 83 (mean 81.6) | 78, 75, 79, 74, 77 (mean 76.6) | 79.1 |
| Column mean | 77.6 | 69.8 | 73.7 |
The two-way ANOVA table (balanced design, \(N = 20\), error df \(= 20 - 4 = 16\)):
| Source | SS | df | MS | F | p-value |
|---|---|---|---|---|---|
| Method | 583.2 | 1 | 583.2 | 111.6 | \(1.3 \times 10^{-8}\) |
| Sleep | 304.2 | 1 | 304.2 | 58.2 | \(1.0 \times 10^{-6}\) |
| Method \(\times\) Sleep | 39.2 | 1 | 39.2 | 7.50 | 0.0146 |
| Error | 83.6 | 16 | 5.225 | ||
| Total | 1010.2 | 19 |
Interpretation. Both main effects are highly significant: practice tests beat flashcards (about 10.8 points higher on average), and normal sleep beats short sleep (about 7.8 points higher). The interaction is also significant (\(p = 0.0146\)): short sleep costs flashcard users 10.6 points (73.6 to 63.0) but practice-test users only 5.0 points (81.6 to 76.6). The lines on the interaction plot are not parallel, so we should describe the sleep effect separately for each method rather than as a single number. (Each mean square is tested against MSE = 5.225 with 1 and 16 degrees of freedom; for example \(F_{AB} = 39.2/5.225 = 7.50\).)
Choosing a method and reporting
| Situation | Method |
|---|---|
| Two independent groups | Two-sample t test (equivalent to ANOVA with \(F = t^2\)) |
| Three or more independent groups, one factor | One-way ANOVA, then Tukey or Bonferroni if significant |
| Three or more groups, unequal variances | Welch's ANOVA, or a transformation |
| Two factors, possible interaction | Two-way ANOVA |
| Same subjects measured under all conditions | Repeated-measures ANOVA (or a paired analysis if only two conditions) |
| Strongly non-normal data, small groups | Kruskal-Wallis (Chapter 15) or a permutation F test |
| A quantitative predictor rather than groups | Regression (Chapter 14) |
"A one-way ANOVA showed a significant effect of [factor] on [response], \(F(\text{df}_1, \text{df}_2) = \) [value], \(p =\) [value], \(\eta^2 =\) [value]. Tukey HSD comparisons showed that [group X] differed from [groups Y, Z] (differences with 95% intervals), while [Y] and [Z] could not be distinguished."
Common pitfalls
- Running all pairwise t tests instead of ANOVA plus a correction. The family-wise error rate is inflated.
- Interpreting a significant F as "all means differ." It says at least one differs.
- Interpreting a non-significant F as "the means are equal." It may simply mean low power.
- Skipping the plots. One outlier can create or hide a significant F.
- Treating non-random groups causally. Only randomized assignment supports causal conclusions.
- Following a non-significant F by hunting for significant pairs. That is p-hacking (Chapter 10).
Chapter summary
- Many separate t tests inflate the family-wise error rate: \(1 - (1-\alpha)^m\) with \(m = k(k-1)/2\).
- One-way ANOVA tests \(H_0\!: \mu_1 = \dots = \mu_k\) with a single F test.
- \(\text{SST} = \text{SSB} + \text{SSW}\); \(\text{df}\): \(N - 1 = (k - 1) + (N - k)\); \(\text{MSB} = \text{SSB}/(k-1)\), \(\text{MSW} = \text{SSW}/(N-k)\); \(F = \text{MSB}/\text{MSW}\).
- Under \(H_0\), \(F \sim F_{k-1,\,N-k}\); the p-value is the right-tail area. With \(k = 2\), \(F = t^2\).
- Conditions: independence, approximate normality within groups, equal SDs (largest less than about twice the smallest).
- Effect size \(\eta^2 = \text{SSB}/\text{SST}\) (benchmarks 0.01, 0.06, 0.14); \(\omega^2\) is less biased.
- After a significant F, use Tukey's HSD or Bonferroni for pairwise comparisons; unadjusted comparisons inflate errors.
- Two-way ANOVA adds a second factor and an interaction; parallel lines on an interaction plot mean no interaction.
Key terms
| Term | Meaning |
|---|---|
| Factor and levels | The categorical explanatory variable and its values (groups) |
| SSB, SSW, SST | Between-group, within-group, and total sums of squares |
| Mean square | A sum of squares divided by its degrees of freedom |
| F statistic | Ratio MSB/MSW; large values are evidence that means differ |
| Family-wise error rate | Probability of at least one false positive in a set of comparisons |
| Tukey HSD | Honestly significant difference: multiple-comparison method for all pairs |
| Bonferroni correction | Use \(\alpha/m\) for each of \(m\) comparisons |
| \(\eta^2\) | Proportion of total variation explained by the factor |
| Interaction | The effect of one factor depends on the level of another |