Contents/ Part IX · Probability & Distributions Case Studies/ Chapter 61

Crop Yield by Fertilizer: F & ANOVA

A t-test compares two means. To compare four fertilizers at once we need ANOVA, which splits the total variance into between-group and within-group pieces and forms the F-ratio. Then Tukey reveals exactly which fertilizers differ.

⏱️ ~14 min read
🐍 Notebook included
📊 Chapter 61

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.

F
ANOVA partitions total variation into between-group (SSB) and within-group (SSW) sums of squares. The F-statistic = MSB / MSW = (SSB/dfb) / (SSW/dfw). Under H0 (all means equal) F ≈ 1; a large F means the groups differ by more than chance.
🌾
The dataset

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.

1

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.

fertilizerplotsmean yield (bu)std dev
A25749.625.03
B25052.505.44
C26648.344.72
D22754.865.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.

2

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.

F = MSB / MSW: signal over noise MSB = 2088 between-group (df = 3) MSW = 26.3 = F = 79.4 p ≈ 10⁻⁴⁶ η² ≈ 0.19 19% of variance

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.

3

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.

comparisonmean diff (bu)qverdict (q > 3.64)
D vs C+6.5119.87significant
D vs A+5.2315.84significant
B vs C+4.1513.00significant
A vs B−2.888.92significant
B vs D−2.367.09significant
A vs C+1.284.03significant

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%.

The pattern across this Part

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 Notebook (code & outputs) ▶ Open in Colab ⬇ View / Download on GitHub

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.

🏁
That completes Probability & Distributions Case Studies

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.
4

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.