Sub topic 11
Comparing Three or More Means with ANOVA
This chapter assumes you can already run and read a two-sample t-test, state null and alternative hypotheses, and interpret a p-value against alpha. You should also be comfortable checking normality and equal variances from the earlier chapters.
The problem ANOVA solves
A Six Sigma team often compares one continuous Y, such as fill weight or cycle time, across a discrete X with three or more levels: three production lines, four suppliers, five operators. Running a t-test on every pair seems natural, but it inflates the chance of a false alarm.
If each test uses alpha = 0.05 and the tests were independent, the chance of at least one false positive is 1 − 0.95^m, where m is the number of tests.
| Groups | Pairs (m) | Chance of at least one false positive |
|---|---|---|
| 3 | 3 | about 14% |
| 4 | 6 | about 26% |
| 5 | 10 | about 40% |
One-way ANOVA (analysis of variance) answers the question "is there any difference among these means?" in a single test at the stated alpha. Pairwise follow-up then locates the difference without losing control of the error rate.
Hypotheses
- Null hypothesis H0: all group means are equal (μ1 = μ2 = … = μk).
- Alternative hypothesis Ha: at least one mean differs from the others.
Ha does not say all means differ, and it does not say which one does. A significant result only tells you that the groups are not all the same.
How the test works
Despite the name, ANOVA compares means by comparing two estimates of variance.
- Between-group variation measures how far the group means sit from the overall mean.
- Within-group variation measures how far individual values sit from their own group mean. It is the noise you would see even if the groups were identical.
The F statistic is the ratio of the mean square between groups to the mean square within groups.
If H0 is true, both estimate the same noise and F is near 1. If at least one mean differs, the between-group part grows and F becomes large. The p-value is the probability of an F at least this large when H0 is true. As usual, it is not the probability that H0 is true.
Assumptions
- Independence. Observations do not influence each other. Repeated readings from one part or one machine setup violate this, and no later check will fix it.
- Normality of the residuals. A residual is a value minus its group mean. Checking the residuals pooled across groups is more informative than testing each small group separately.
- Equal variances across groups. Check with Levene's test, whose H0 is that the variances are equal.
ANOVA tolerates mild non-normality, especially with similar group sizes of about 15 or more. Unequal variances combined with unequal group sizes cause more harm.
Choosing the path
Running it in Python
The script below checks the assumptions, runs the ANOVA, follows up with Tukey's HSD (honestly significant difference) test, and computes eta squared, an effect size explained later.
import numpy as np
from scipy import stats
rng = np.random.default_rng(7)
line_a = rng.normal(100, 5, 25)
line_b = rng.normal(102, 5, 25)
line_c = rng.normal(105, 5, 25)
groups = [line_a, line_b, line_c]
# Assumption checks
residuals = np.concatenate([g - g.mean() for g in groups])
print("Shapiro-Wilk on residuals p =", stats.shapiro(residuals).pvalue)
print("Levene p =", stats.levene(*groups).pvalue)
# One-way ANOVA
f_stat, p_anova = stats.f_oneway(*groups)
print("F =", f_stat, "p =", p_anova)
# Post hoc: Tukey HSD for all pairs (SciPy 1.8 or later)
print(stats.tukey_hsd(*groups))
# Effect size: share of total variation explained by the line
all_data = np.concatenate(groups)
grand_mean = all_data.mean()
ss_between = sum(len(g) * (g.mean() - grand_mean) ** 2 for g in groups)
ss_total = ((all_data - grand_mean) ** 2).sum()
print("eta squared =", ss_between / ss_total)Read the output in this order. Confirm the assumption checks do not raise concerns, then read the ANOVA p-value. Only if it is below alpha should you read the Tukey table, which gives a mean difference, a p-value adjusted for the number of comparisons, and a confidence interval for each pair.
Post hoc comparisons
A post hoc test is run after a significant ANOVA to find which pairs differ. Tukey's HSD is the usual choice when you compare every pair. It widens the intervals so that the family-wise error rate stays at alpha. Dunnett's test compares each group against a single control and has more power for that narrower question. Do not substitute unadjusted t-tests here, because that brings back the inflation shown in the table above.
A pairwise result can look odd. ANOVA may be significant while no single Tukey pair is, or A may equal B and B may equal C while A differs from C. These are not errors. Failing to detect a difference is not evidence of equality.
When assumptions fail
- Unequal variances. Use Welch's ANOVA, which does not pool variances. In Python,
statsmodels.stats.oneway.anova_oneway(groups, use_var="unequal")runs it. Games-Howell is the matching post hoc test. - Non-normal data or heavy outliers. Use the Kruskal-Wallis test with
stats.kruskal(*groups). It compares ranks, so strictly it tests whether values in one group tend to be larger than in another. It compares medians only if the group shapes are similar. - Outliers from a known cause. Investigate before choosing a test. A mis-keyed value or a changeover period is a data problem, not a reason to switch methods.
Statistical and practical significance
With large samples, ANOVA can flag differences too small to matter. Eta squared is the fraction of total variability explained by the grouping factor. Values of about 0.01, 0.06, and 0.14 are conventionally called small, medium, and large, but judge them against your process.
Also look at the mean differences and their intervals in engineering units. A 0.3 g difference between fillers with a significant p-value may be irrelevant if the specification tolerance is 5 g.
Common mistakes
- Reading a significant F as "all groups differ".
- Running the post hoc test when the ANOVA was not significant.
- Testing normality on the raw pooled data instead of the residuals. Pooled raw data look non-normal whenever the group means truly differ.
- Treating a non-significant Levene test as proof that variances are equal when groups are small.
- Using one-way ANOVA when a second factor, such as shift, also affects Y. That calls for a model with more than one factor.
Exercise
A team compares average cycle time across four suppliers with 20 orders each. Levene's test gives p = 0.003, the residuals look normal, and one-way ANOVA is significant. Which test should they report, and what do they do next?
Show answer
Equal variances is rejected (p = 0.003 is below 0.05), so classic ANOVA is not appropriate. They should report Welch's ANOVA, which handles unequal variances and still fits the normal residuals. If it is significant, they follow up with Games-Howell for pairwise comparisons, then report mean differences with confidence intervals and an effect size to judge practical importance.
Exercise
Why is running six separate t-tests across four groups at alpha = 0.05 a problem, and roughly how large is the overall false-positive risk?
Show answer
Each test has a 5% chance of a false positive, and the chances accumulate across six tests. The family-wise risk is about 1 − 0.95^6, or roughly 26%, well above the intended 5%. ANOVA followed by an adjusted post hoc test such as Tukey keeps the overall error rate at alpha.