Book contents

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 FF–statistic, and multiple comparisons. We illustrate with code in R, Python and Julia.

One–Way ANOVA Model

Suppose we have gg groups, each with njn_j observations YijY_{ij} (i=1,…,nji=1,\dots,n_j). The one–way ANOVA model is Yij=μ+τj+εij,εij∼Normal(0,σ2),Y_{ij} = \mu + \tau_j + \varepsilon_{ij}, \quad \varepsilon_{ij} \sim \mathrm{Normal}(0, \sigma^2), where μ\mu is the overall mean and τj\tau_j is the effect of group jj. The null hypothesis H0:τ1=⋯=τg=0H_0: \tau_1 = \cdots = \tau_g = 0 states that all group means are equal.

ANOVA Table and FF–Statistic

The total sum of squares is partitioned into between–group and within–group components: SST=∑j=1g∑i=1nj(Yij−Y‾)2=SSB+SSW,\text{SST} = \sum_{j=1}^g \sum_{i=1}^{n_j} (Y_{ij} - \bar{Y})^2 = \text{SSB} + \text{SSW}, where SSB=∑j=1gnj(Y‾j−Y‾)2,SSW=∑j=1g∑i=1nj(Yij−Y‾j)2.\text{SSB} = \sum_{j=1}^g n_j (\bar{Y}_j - \bar{Y})^2, \quad \text{SSW} = \sum_{j=1}^g \sum_{i=1}^{n_j} (Y_{ij} - \bar{Y}_j)^2. We compute mean squares by dividing sums of squares by their degrees of freedom: MSB=SSB/(g−1)\text{MSB} = \text{SSB}/(g-1) and MSW=SSW/(N−g)\text{MSW} = \text{SSW}/(N-g). The FF–statistic is F=MSBMSW,F = \frac{\text{MSB}}{\text{MSW}}, which follows an Fg−1,N−gF_{g-1, N-g} distribution under H0H_0. A large FF 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 FF–statistic. In this example, FF 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

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

  2. Derive the expected mean squares in the one–way ANOVA and show that E(MSW)=σ2\mathrm{E}(\text{MSW}) = \sigma^2 and E(MSB)=σ2+∑nj(τj2)/(g−1)\mathrm{E}(\text{MSB}) = \sigma^2 + \sum n_j(\tau_j^2)/(g-1).

  3. Use Python or R to verify that the FF–statistic follows the FF distribution under H0H_0 by simulation.

  4. When variances differ across groups, compare the standard ANOVA with Welch’s ANOVA (available as oneway_test in R and scipy.stats.f_oneway with parameter equal_var=False in Python).

References