Running a t-test on every pair of four groups means six tests, and the chance of a false positive balloons. Analysis of variance (ANOVA) asks the question once: are these means all the same? Its test statistic is the F-ratio.
agricultural_yield_optimization.csv records crop_yield_bushels for 1,000 plots, each
treated with one of four fertilizers (A, B, C, D). We ask whether fertilizer choice changes yield, and by how
much.
Four Groups, Four Means
Start by looking, not testing. We group the plots by fertilizer and compare the average yields against the spread inside each group.
| fertilizer | plots | mean yield (bu) | std dev |
|---|---|---|---|
| A | 257 | 49.62 | 5.03 |
| B | 250 | 52.50 | 5.44 |
| C | 266 | 48.34 | 4.72 |
| D | 227 | 54.86 | 5.35 |
The means span from 48.3 bushels (C) to 54.9 (D), a gap of over 6 bushels, while each group's own scatter is about 5. The grand mean is 51.2. The between-group gaps look larger than the within-group noise, and ANOVA turns that visual hunch into a number.
Partition the Variance
ANOVA's core idea: total variation = between-group + within-group. If fertilizer matters, the between-group piece dominates and the F-ratio climbs well above 1.
The between-group mean square (2088) dwarfs the within-group mean square (26), giving
F = MSB / MSW = 79.4 on (3, 996) df, p ≈ 10−46
Under H0 the F-ratio would hover near 1; a value of 79 is astronomically unlikely by chance, so at least one fertilizer truly differs. The effect size η² ≈ 0.19 says fertilizer choice explains about 19% of the variation in yield, the rest is plot-to-plot noise.
But first, do the F-test's conditions hold? Independence is by design, and Levene's test (p ≈ 0.13) confirms similar variances. The residual QQ plot tells a subtler story: it bends at the tails and Shapiro fails (p ≈ 0.005), so the residuals are not perfectly normal. In a small study that would be a real worry, but with n = 1000 the Central Limit Theorem keeps the F-test valid, and the distribution-free Kruskal-Wallis test agrees overwhelmingly (p ≈ 4×10⁻⁴²), so the verdict stands. On a small, badly skewed sample you would switch to Kruskal-Wallis (non-normality) or Welch's ANOVA (unequal variances) instead. The companion notebook runs all three checks plus a Tukey interval plot.
Which Ones Differ? Tukey HSD
A significant F says only that the means are not all equal, not which differ. Tukey's Honest Significant Difference compares every pair while holding the family-wide error rate at 5%, so the ranking is trustworthy.
| comparison | mean diff (bu) | q | verdict (q > 3.64) |
|---|---|---|---|
| D vs C | +6.51 | 19.87 | significant |
| D vs A | +5.23 | 15.84 | significant |
| B vs C | +4.15 | 13.00 | significant |
| A vs B | −2.88 | 8.92 | significant |
| B vs D | −2.36 | 7.09 | significant |
| A vs C | +1.28 | 4.03 | significant |
Every one of the six comparisons clears the Tukey threshold, so the full ranking is real: D (54.9) > B (52.5) > A (49.6) > C (48.3). Fertilizer D is the clear winner, beating the worst performer C by more than 6 bushels per plot. Tukey is what keeps this honest, running six naive t-tests would inflate the false-positive rate, while the studentized-range adjustment holds it at 5%.
t, chi-square, and F are all built on the same foundation, the Central Limit Theorem. ANOVA generalizes the t-test from two groups to many; the F-ratio is to means what chi-square is to counts, a single number that grows as the data depart from "no difference."
Run the ANOVA
The companion notebook computes the group means and boxplots, partitions the variance by hand (SSB, SSW,
MSB, MSW) and confirms it with scipy.stats.f_oneway, reports η², then runs a Tukey
HSD over all six pairs.
View opens the rendered notebook instantly (no setup). Open in Colab runs &
edits it live in your browser. To run locally, install numpy, pandas, scipy,
and matplotlib and launch jupyter notebook.
Across fourteen chapters we matched thirteen realistic datasets to thirteen distributions, from Bernoulli clicks and binomial defects through Poisson arrivals, the geometric and negative-binomial wait, the hypergeometric catch, and the normal, exponential, and gamma continua, then closed with the three great test statistics: Student's t, chi-square, and the F-ratio. The thread tying them together is the Central Limit Theorem, which turns skewed data into normal sample means and powers every inference that follows. Next we turn to why and how we sample in the first place.
🎓 Key Takeaways
- ✓ANOVA compares 3+ means at once, avoiding the inflated error of many pairwise t-tests.
- ✓It partitions variance: total = between-group (SSB) + within-group (SSW); F = MSB/MSW.
- ✓F = 79.4 on (3, 996) df, p ≈ 10−46: the fertilizers are not all equal (η² ≈ 0.19).
- ✓Tukey HSD ranks them D > B > A > C, every pair significant, with family-wide error held at 5%.
- ✓t, χ², and F all rest on the Central Limit Theorem, the bridge from this Part into inference.
Quiz: Test Yourself
Eight quick questions on the ANOVA case study. Answer them, hit Check Answers, and keep refining until you score 100%. Your progress is saved, so you can hop back to the chapter and return anytime.