18 Two-Way ANOVA
By the end of this chapter you will be able to:
- Distinguish factors (variables) from levels (categories within a variable).
- Explain what a significant interaction means — and when it requires qualifying the main effects.
- Read two-way ANOVA output, including partial η².
- Build and interpret an interaction plot: parallel vs. crossing lines.
Suppose discussion-based teaching beats lecture in small classes — but does nothing in large ones. A study that varies only teaching method would miss that entirely, and so would a study that varies only class size. The two-way ANOVA exists for exactly this kind of question: two factors at once, including the possibility that one factor’s effect depends on the other.
A two-way ANOVA tests the effects of two categorical predictors (and their interaction) on a continuous outcome. It is the ANOVA generalization of going from one to two grouping variables — and it’s where the concept of an interaction first becomes important.
18.1 When to use it
Use two-way ANOVA when you have:
- One continuous outcome variable.
- Two categorical predictors, each with at least two levels.
- Subjects are independent (between-subject design — not the same person measured in every condition).
A factor is the variable (teaching method); its levels are its categories (A, B, C). Method (3 levels) × class size (2 levels) is a 3 × 2 design with six cells.
Examples:
- Do exam scores depend on teaching method (A/B/C) and class size (small/large), separately or in interaction?
- Does reaction time depend on caffeine dose (low/medium/high) and time of day (morning/evening)?
- Does anxiety depend on gender and treatment condition?
If subjects are measured under multiple conditions of one factor, see Chapter 19 for repeated-measures and mixed designs.
18.2 What you can test
A two-way ANOVA gives you three tests:
- Main effect of Factor A — does the outcome differ across levels of A, averaging over B?
- Main effect of Factor B — does the outcome differ across levels of B, averaging over A?
- Interaction (A × B) — does the effect of A depend on the level of B?
The interaction is often the most interesting test. A significant interaction means you cannot fully describe the effect of A without also knowing the level of B (and vice versa).
18.3 Hypotheses
- \(H_0^{(A)}\): All levels of A have the same mean.
- \(H_0^{(B)}\): All levels of B have the same mean.
- \(H_0^{(A \times B)}\): The effect of A is the same across all levels of B.
Each test has its own F-statistic, df, and p-value.
Before running the model, try the Rossman/Chance two-way ANOVA applet.
Predict first: sketch what you expect the group means to look like under each combination of your two factors. If the lines on an interaction plot were parallel, what would that tell you? If they crossed, what would that tell you? Then run your analysis and compare the actual interaction plot to your prediction.

18.4 R syntax
The formula uses * to include both main effects and the interaction:
model <- aov(outcome ~ factorA * factorB, data = data)
summary(model)A * B is shorthand for A + B + A:B — main effect of A, main effect of B, and interaction. If you want main effects only (no interaction), use +:
model <- aov(outcome ~ factorA + factorB, data = data)Almost always include the interaction unless you have a specific reason not to.
18.4.1 Worked example
You’re testing how exam scores depend on teaching method (3 levels) and class size (2 levels), with 10 students in each of the six cells.
# Step 0: Load the data
data <- read.csv("https://jkoxford-a11y.github.io/awesomesaucestats/data/ch18_method_size.csv")
# Step 1: Inspect & ensure factors
str(data)
data$method <- as.factor(data$method)
data$size <- as.factor(data$size)
table(data$method, data$size) # cell sizes — should be roughly balanced
# Step 2: Visualize — interaction plot
library(ggplot2)
ggplot(data, aes(x = method, y = score, color = size, group = size)) +
stat_summary(fun = mean, geom = "line", linewidth = 1) +
stat_summary(fun = mean, geom = "point", size = 3)
# Or base R
interaction.plot(data$method, data$size, data$score,
main = "Method × Size interaction")
# Step 3: Fit model
model <- aov(score ~ method * size, data = data)
# Step 4: Check assumptions
shapiro.test(residuals(model))
plot(model, which = 2)
# Step 5: ANOVA table
summary(model)
# Step 6: Effect sizes
library(effectsize)
eta_squared(model, partial = TRUE)
# Step 7: Cell means
aggregate(score ~ method + size, data = data, FUN = mean)18.4.2 Reading the ANOVA table
Df Sum Sq Mean Sq F value Pr(>F)
method 2 473.6 236.82 7.364 0.00149 **
size 1 308.3 308.27 9.586 0.00311 **
method:size 2 16.2 8.12 0.252 0.77785
Residuals 54 1736.6 32.16
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
method: main effect of method. F(2, 54) = 7.36, p = .001 — significant.size: main effect of size. F(1, 54) = 9.59, p = .003 — significant.method:size: interaction. F(2, 54) = 0.25, p = .778 — not significant.
The interpretation: both main effects are significant. Method matters, and class size matters, but they don’t interact — the effect of method is roughly the same in small and large classes (and vice versa).
When the interaction term is significant, interpreting main effects in isolation can be misleading. A significant main effect of factor A means “A has an effect on average, collapsing across all levels of B.” But if A × B is significant, the effect of A is not constant — it depends on which level of B you’re in. Reporting only the main effect understates or obscures this. When the interaction is significant, follow up with simple effects (the effect of A within each level of B) rather than summarizing the main effect as if the interaction doesn’t exist.
Open ANOVA: Signal, Noise, and Interaction — Part B. Change the two main effects and the interaction separately. The profile plot, three ANOVA rows, partial \(\eta^2\), and simple-effect intervals update together—including a crossover with two null main effects but a large interaction.
Read this output carefully. The R code that produced it is not shown.
Df Sum Sq Mean Sq F value Pr(>F)
caffeine 1 1717 1717.4 11.317 0.00139 **
time_of_day 1 889 889.3 5.861 0.01875 *
caffeine:time_of_day 1 1188 1188.1 7.830 0.00703 **
Residuals 56 8498 151.8
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
- How many factors are in this model? Name them and their levels (infer from the table structure).
- Which effects are statistically significant at α = .05?
- The interaction is significant. What does this mean in plain language about how caffeine and time of day work together?
- A researcher concludes “caffeine significantly reduces reaction time.” What is problematic about this statement given these results?
- What is the appropriate follow-up analysis after a significant interaction?
- Calculate partial \(\eta_p^2\) for the caffeine main effect. (Hint: SSeffect / (SSeffect + SSresiduals))
- Write one APA 7 sentence for the interaction term alone.
18.5 Interactions: the most important concept
Does sugar improve food? Wrong question. Sugar improves lemonade and ruins scrambled eggs. The effect of sugar depends on the dish — there is no single “sugar effect” to report.
Mapping: sugar = factor A; dish = factor B; taste = outcome; “depends on the dish” = the interaction; reporting one overall sugar effect = interpreting a main effect when the interaction is significant.
Where this breaks: interactions aren’t always reversals. Often both dishes improve, just by different amounts (an ordinal interaction) — statistically, an interaction is a difference of differences, not “good here, bad there.”
An interaction means the effect of one factor depends on the level of the other. Examples:
- A drug works in younger adults but not in older adults —
Drug × Ageinteraction. - Teaching method A is best in small classes but method B is best in large classes —
Method × Sizeinteraction. - Caffeine speeds reaction time in the morning but has no effect in the evening —
Caffeine × Timeinteraction.
A picture of an interaction:
- No interaction: parallel lines on an interaction plot. The gap between groups is the same across levels of the other factor.
- Interaction: lines that diverge, converge, or cross.
If the interaction is significant, the main effects are sometimes hard to interpret on their own. The standard advice: interpret main effects with caution when the interaction is significant — you may want to report simple effects (the effect of A within each level of B) instead.
18.6 Effect size: partial eta-squared
In multi-factor ANOVA, the conventional effect size is partial eta-squared (\(\eta_p^2\)):
\[ \eta_p^2 = \frac{\text{SS}_\text{effect}}{\text{SS}_\text{effect} + \text{SS}_\text{residual}} \]
Each effect (main and interaction) gets its own \(\eta_p^2\).
library(effectsize)
eta_squared(model, partial = TRUE)Same interpretation conventions: 0.01 small, 0.06 medium, 0.14 large.
18.7 Post-hoc tests for significant effects
If a main effect is significant and the factor has more than two levels, use Tukey HSD to find which levels differ:
TukeyHSD(model, which = "method")If the interaction is significant, simple-effects analyses are usually more informative than Tukey. The emmeans package handles this gracefully:
library(emmeans)
emmeans(model, pairwise ~ method | size)This produces pairwise comparisons of method within each level of size.
18.8 Assumptions and diagnostics
Same as one-way ANOVA:
- Independence.
- Normality of residuals.
- Homogeneity of variance across all cells (not just main-effect groups).
- Continuous outcome.
Check residuals with shapiro.test(residuals(model)) and the four diagnostic plots from plot(model).
18.9 Reporting
A 3 (teaching method: A, B, C) × 2 (class size: small, large) between-subjects ANOVA was conducted on exam scores; residuals were approximately normal (Shapiro-Wilk p = .415). There was a significant main effect of method, F(2, 54) = 7.36, p = .001, \(\eta_p^2\) = .21 (large effect), and a significant main effect of class size, F(1, 54) = 9.59, p = .003, \(\eta_p^2\) = .15 (large effect). The interaction was not significant, F(2, 54) = 0.25, p = .778, \(\eta_p^2\) = .01. Tukey HSD comparisons indicated that method C (M = 77.85, SD = 5.38) scored higher than method A (M = 71.00, SD = 5.79; p = .001); method B (M = 73.85, SD = 6.78) did not differ significantly from A (p = .259) or C (p = .075). Students in small classes (M = 76.50, SD = 5.62) scored higher than those in large classes (M = 71.97, SD = 6.72).
18.10 Common mistakes
| Wrong | Right |
|---|---|
Not including the interaction (A + B) when you have no reason to leave it out |
Use A * B unless theory dictates otherwise |
| Interpreting main effects without noting a significant interaction | If the interaction is significant, qualify the main effects |
| Running separate one-way ANOVAs for each level of B | A proper two-way ANOVA handles this with one test and gives you the interaction term |
| Skipping the cell-size check | If cells are very unbalanced, results are sensitive to coding choices (Type I vs III SS) |
| Treating a continuous variable as a factor without good reason | If dose is truly continuous, regression is better |
18.11 A note on balanced vs. unbalanced designs
When each cell has the same number of observations, the design is balanced, and the ANOVA results are interpretation-clean. When cells differ in size, you’ve entered the world of Type I vs. Type II vs. Type III sums of squares — same data, different decompositions, sometimes different p-values.
For class purposes:
- If your design is approximately balanced, the default
aov()output is fine. - If your design is heavily unbalanced, prefer
Anova()from thecarpackage withtype = 3for Type III SS — and switch R to sum-to-zero contrasts before fitting the model. With R’s default contrasts, Type III main-effect tests quietly test the wrong hypotheses; the interaction test is unaffected, which makes the problem easy to miss.
library(car)
options(contrasts = c("contr.sum", "contr.poly")) # required for meaningful Type III
model <- aov(score ~ method * size, data = data) # refit after changing contrasts
Anova(model, type = 3)
options(contrasts = c("contr.treatment", "contr.poly")) # back to R's default18.12 Quick reference
| Element | R code |
|---|---|
| Two-way ANOVA with interaction | model <- aov(y ~ A * B, data = data) |
| Main effects only (no interaction) | model <- aov(y ~ A + B, data = data) |
| ANOVA table | summary(model) |
| Type III SS (unbalanced data) | options(contrasts = c("contr.sum", "contr.poly")), refit, then library(car); Anova(model, type = 3); afterward reset with options(contrasts = c("contr.treatment", "contr.poly")) |
| Partial eta-squared | library(effectsize); eta_squared(model, partial = TRUE) |
| Tukey for main effect | TukeyHSD(model, which = "factorA") |
| Simple effects (interaction follow-up) | library(emmeans); emmeans(model, pairwise ~ A | B) |
| Cell means | aggregate(y ~ A + B, data = data, FUN = mean) |
| Interaction plot | interaction.plot(A, B, y) or ggplot with stat_summary |
18.13 The same test, different disciplines
Two-way ANOVA applies whenever you have a continuous outcome and two categorical predictors — regardless of what those predictors represent.
| Discipline | Typical combination | Factors |
|---|---|---|
| Psychology | Condition × participant characteristic on a cognitive or behavioral outcome | Therapy type × diagnosis on anxiety score; stress level × sleep quality on memory |
| Biology | Treatment × organism characteristic or environmental variable | Drug × strain on growth rate; light condition × temperature on enzyme activity |
| Sociology | Program × demographic group on a social outcome | Policy type × income bracket on employment rate; region × education on political attitude |
Next: when the same subjects are measured under multiple conditions.
Core idea: Two-way ANOVA tests the effects of two categorical predictors — and their interaction — on a continuous outcome. The interaction is often the most important result: it tells you whether the effect of one factor depends on the level of the other.
You should understand:
- What a main effect is vs. what an interaction is
- Why a significant interaction qualifies (or can override) the interpretation of main effects
- What parallel vs. crossing lines on an interaction plot mean
You should be able to do in R:
- Fit the model:
model <- aov(y ~ A * B, data = data) - Read the ANOVA table:
summary(model) - Effect size:
library(effectsize); eta_squared(model, partial = TRUE) - Simple-effects follow-up:
library(emmeans); emmeans(model, pairwise ~ A | B)
Plain-language check: An ANOVA table shows a significant A × B interaction (p = .003) and a significant main effect of A (p = .01). Can you report “factor A significantly affects the outcome”? What else do you need to say?
The one mistake to avoid: Interpreting main effects as if they fully describe the data when the interaction is significant. A main effect averages over the other factor — when the interaction is significant, that average is hiding the real story.
Key terms from this chapter are defined in the Glossary.
Practice the skills from this chapter using real data: Two-Way ANOVA Practice: Attention and Age.