R ANOVA
last modified November 19, 2026
Analysis of variance compares the means of several groups at once. Instead of
running a separate t-test for every pair, ANOVA partitions the total variation
in the response into the part explained by the groups and the part left over
inside them. The ratio of those two parts is the F statistic, and a large F
means that the group means differ by more than the within-group noise would
explain. In R the model is fitted with aov, which uses the same
formula syntax as lm. This article covers a one-way design,
group means, post-hoc comparisons, two factors with an interaction, model
comparison, and the assumption checks that should follow a fit.
One-way analysis of variance
The built-in PlantGrowth data set records the dried weight of
thirty plants grown under a control condition and two treatments. The formula
weight ~ group models the weight by the group factor.
summary on the fitted object prints the degrees of freedom, the
sum of squares, the mean squares, the F statistic, and its p-value.
fit <- aov(weight ~ group, data = PlantGrowth)
cat("observations:", nrow(PlantGrowth), "\n")
cat("groups:", nlevels(PlantGrowth$group), "\n")
print(summary(fit))
The group line has two degrees of freedom, one less than the three groups, and the residuals have twenty-seven, which is thirty observations minus three group means. The group sum of squares is 3.766 against a residual sum of squares of 10.492, so the F statistic is 4.846 and the p-value is 0.0159. At the five percent level the hypothesis of equal means is rejected. The test says that at least one mean differs, not which one.
$ Rscript fit.R
observations: 30
groups: 3
Df Sum Sq Mean Sq F value Pr(>F)
group 2 3.766 1.8832 4.846 0.0159 *
Residuals 27 10.492 0.3886
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Group means and coefficients
model.tables summarises the fitted model in the units of the
response and prints the grand mean together with the mean of every group. The
coefficients of the model tell the same story from the baseline's point of
view: the intercept is the mean of the first level, and each other
coefficient is that group's difference from the baseline.
fit <- aov(weight ~ group, data = PlantGrowth) print(model.tables(fit, "means")) print(coef(fit))
The grand mean is 5.073. The control group averages 5.032, treatment one averages 4.661, and treatment two averages 5.526, so treatment two is the only group above the grand mean. The coefficients repeat the numbers relative to the control: the intercept is 5.032, treatment one is 0.371 below it, and treatment two is 0.494 above it. The group factor is unordered, so R picked the first level alphabetically as the baseline.
$ Rscript means.R
Tables of means
Grand mean
5.073
group
group
ctrl trt1 trt2
5.032 4.661 5.526
(Intercept) grouptrt1 grouptrt2
5.032 -0.371 0.494
Post-hoc comparisons
A significant F test does not say which pairs differ.
TukeyHSD compares every pair of means while controlling the
family-wise error rate, which is the chance of making at least one false
positive across all the comparisons. The result contains the difference, its
confidence interval, and an adjusted p-value for each pair.
fit <- aov(weight ~ group, data = PlantGrowth) print(TukeyHSD(fit))
Only the difference between treatment two and treatment one is significant, with a gain of 0.865 and an adjusted p-value of 0.012. Its interval runs from 0.174 to 1.556 and excludes zero. The other two intervals contain zero, and their adjusted p-values are 0.198 and 0.391. The adjustment is why these p-values are larger than the unadjusted ones that separate t-tests would report.
$ Rscript tukey.R
Tukey multiple comparisons of means
95% family-wise confidence level
Fit: aov(formula = weight ~ group, data = PlantGrowth)
$group
diff lwr upr p adj
trt1-ctrl -0.371 -1.0622161 0.3202161 0.3908711
trt2-ctrl 0.494 -0.1972161 1.1852161 0.1979960
trt2-trt1 0.865 0.1737839 1.5562161 0.0120064
Two factors
The warpbreaks data set counts the number of breaks in a fibre
under two wool types and three tension levels, with nine observations in each
of the six combinations. Writing wool * tension in the formula
includes both main effects and their interaction, which measures whether the
effect of one factor depends on the level of the other.
fit <- aov(breaks ~ wool * tension, data = warpbreaks)
cat("observations:", nrow(warpbreaks), "\n")
print(summary(fit))
Tension is the strongest term, with an F of 8.498 and a p-value of 0.0007. Wool is close to the threshold at 0.058, and the interaction is significant at 0.021. A significant interaction means that the tension effect is not the same for both wool types, so the main effects should not be read as if they applied uniformly. The residual mean square of 119.7 is the within-cell variance.
$ Rscript two-way.R
observations: 54
Df Sum Sq Mean Sq F value Pr(>F)
wool 1 451 450.7 3.765 0.058213 .
tension 2 2034 1017.1 8.498 0.000693 ***
wool:tension 2 1003 501.4 4.189 0.021044 *
Residuals 48 5745 119.7
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Comparing nested models
The interaction spends two degrees of freedom. A direct way to judge whether
those degrees of freedom earn their place is to fit the additive model and the
full model and pass both objects to anova. The function compares
the residual sums of squares and reports an F test for the added terms.
additive <- aov(breaks ~ wool + tension, data = warpbreaks) full <- aov(breaks ~ wool * tension, data = warpbreaks) print(anova(additive, full))
The additive model leaves a residual sum of squares of 6747.9 on fifty degrees of freedom. Adding the interaction reduces it to 5745.1 and spends two degrees of freedom, which gives an F of 4.189 and a p-value of 0.021. This is the same interaction test that the two-way summary printed, obtained by comparing two models instead of reading one table. Nested models can only be compared this way when the smaller model is contained in the larger one.
$ Rscript interaction-model.R Analysis of Variance Table Model 1: breaks ~ wool + tension Model 2: breaks ~ wool * tension Res.Df RSS Df Sum of Sq F Pr(>F) 1 50 6747.9 2 48 5745.1 2 1002.8 4.1891 0.02104 * --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Checking assumptions
ANOVA assumes independent observations, roughly normal residuals, and equal
variances within the groups. The residuals of the fit estimate the noise, and
shapiro.test checks their normality. bartlett.test
compares the variances of several groups, which is the multi-group extension
of the F test for two variances.
fit <- aov(weight ~ group, data = PlantGrowth)
res <- residuals(fit)
cat("residual mean:", round(mean(res), 4), "\n")
print(shapiro.test(res))
print(bartlett.test(weight ~ group, data = PlantGrowth))
The residuals average zero by construction, and their Shapiro p-value of 0.438 gives no reason to doubt normality. Bartlett's statistic is 2.879 on two degrees of freedom with a p-value of 0.237, so the group variances are compatible with equality. A large p-value never proves an assumption; it only says that this sample does not contradict it. The observations must also be independent, which no test in this list can establish.
$ Rscript assumptions.R residual mean: 0 Shapiro-Wilk normality test data: res W = 0.96607, p-value = 0.4379 Bartlett test of homogeneity of variances data: weight by group Bartlett's K-squared = 2.8786, df = 2, p-value = 0.2371
A complete example
The final script returns to the plant data and reports the effect size next to the test. Eta squared is the group sum of squares divided by the total sum of squares, and the residual standard deviation is the square root of the residual mean square. The Tukey table follows the same fit, so the whole report comes from one model.
fit <- aov(weight ~ group, data = PlantGrowth)
s <- summary(fit)[[1]]
ss <- s[["Sum Sq"]]
cat("total sum of squares:", sum(ss), "\n")
cat("eta squared:", round(ss[1] / sum(ss), 3), "\n")
cat("residual sd:", round(sqrt(s[["Mean Sq"]][2]), 3), "\n")
print(TukeyHSD(fit)$group)
The groups account for 0.264 of the total variation, about twenty-six percent, and the residual standard deviation is 0.623 grams. The Tukey table confirms that only treatment two and treatment one differ, with an interval from 0.174 to 1.556. An effect size and an interval together say more than a p-value alone: the p-value answers whether the means differ, and the other two numbers say by how much.
$ Rscript report.R
total sum of squares: 14.25843
eta squared: 0.264
residual sd: 0.623
diff lwr upr p adj
trt1-ctrl -0.371 -1.0622161 0.3202161 0.39087114
trt2-ctrl 0.494 -0.1972161 1.1852161 0.19799599
trt2-trt1 0.865 0.1737839 1.5562161 0.01200642
Next steps
The linear regression article fits the
same kind of model with a continuous predictor and explains the formula syntax
that aov shares with lm. The
hypothesis testing article introduces
the t-test for two groups and the p-value logic behind the F test. The
descriptive statistics article
covers the group summaries that should precede any test, and the
probability distributions article explains
the F distribution that turns the statistic into a p-value.
Source
R documentation: bartlett.test
R documentation: the PlantGrowth data set
R documentation: the warpbreaks data set
In this article we have fitted a one-way analysis of variance and read its degrees of freedom, sums of squares, and F test. We compared group means with the grand mean and the baseline, and separated the pairs with Tukey's honest significant difference. We extended the model to two factors, tested their interaction by comparing nested models, and checked normality and equal variances. The final example reported the effect size and the residual spread together with the post-hoc table. The series continues with R time series.
Author
List all R Language tutorials.