STATHARBOR ANALYTICS · INTRODUCTION TO STATISTICS · CHAPTER 14
Analysis of Variance (ANOVA)
Analysis of variance (ANOVA) assesses whether three or more population means differ. While two–sample tests compare two means, ANOVA extends to multiple groups and controls the type I error rate. This chapter introduces one–way ANOVA, its assumptions, the –statistic, and multiple comparisons. We illustrate with code in R, Python and Julia.
One–Way ANOVA Model
Suppose we have groups, each with observations (). The one–way ANOVA model is where is the overall mean and is the effect of group . The null hypothesis states that all group means are equal.
ANOVA Table and –Statistic
The total sum of squares is partitioned into between–group and within–group components: where We compute mean squares by dividing sums of squares by their degrees of freedom: and . The –statistic is which follows an distribution under . A large suggests at least one group mean differs.
Example: Plant Growth Experiment
Suppose three fertiliser treatments are applied to plots with yields (kg) shown below:
| Treatment | Plot 1 | Plot 2 | Plot 3 | Plot 4 |
|---|---|---|---|---|
| A | 20 | 21 | 19 | 18 |
| B | 23 | 24 | 22 | 25 |
| C | 17 | 16 | 18 | 15 |
We wish to determine whether mean yields differ across treatments.
Worked example
One–way ANOVA in R, Python and Julia.
yield <- c(20,21,19,18, 23,24,22,25, 17,16,18,15)
treat <- factor(rep(c("A","B","C"), each=4))
aov_fit <- aov(yield ~ treat)
summary(aov_fit)
import pandas as pd
import statsmodels.api as sm
from statsmodels.formula.api import ols
df = pd.DataFrame({
'yield': [20,21,19,18, 23,24,22,25, 17,16,18,15],
'treat': ['A']*4 + ['B']*4 + ['C']*4
})
model = ols('yield ~ C(treat)', data=df).fit()
anova_table = sm.stats.anova_lm(model, typ=2)
print(anova_table)
using DataFrames, GLM, Statistics
df = DataFrame(yield = [20,21,19,18,23,24,22,25,17,16,18,15],
treat = repeat(["A","B","C"], inner = 4))
aov_fit = lm(@formula(yield ~ treat), df)
anova(aov_fit)
All three functions produce an ANOVA table with sums of squares, mean squares and the –statistic. In this example, is large and the p–value is small, suggesting treatment effects differ.
Multiple Comparisons
ANOVA tells us that not all group means are equal, but does not
specify which groups differ. To make pairwise comparisons while
controlling the family–wise error rate, we use methods such as Tukey’s
honestly significant difference (HSD) or Bonferroni adjustment. For
example, in R: TukeyHSD(aov_fit); in Python:
statsmodels.stats.multicomp.pairwise_tukeyhsd.
Assumptions and Alternatives
ANOVA assumes independent observations, normally distributed errors and equal variances across groups (homoscedasticity). Diagnostics include residual plots and tests like Levene’s test for equality of variances. If assumptions fail, consider transforming the response or using nonparametric alternatives such as the Kruskal–Wallis test (see Chapter [chap:nonparametric]).
Exercises
Practice
Design an experiment with four treatments and collect or simulate data. Perform one–way ANOVA and interpret the results. Conduct Tukey’s HSD for pairwise comparisons.
Derive the expected mean squares in the one–way ANOVA and show that and .
Use Python or R to verify that the –statistic follows the distribution under by simulation.
When variances differ across groups, compare the standard ANOVA with Welch’s ANOVA (available as
oneway_testin R andscipy.stats.f_onewaywith parameterequal_var=Falsein Python).
References
Gelman A. Data Analysis Using Regression and Multilevel/Hierarchical Models. Cambridge University Press; 2006.
Miller RG. Beyond ANOVA: Basics of Applied Statistics. Chapman & Hall; 1997.
Hsu JC. Multiple Comparisons: Theory and Methods. Chapman & Hall; 1996.