Part 6 · Module 8
Two-Way and Factorial Analysis of Variance
Learn how factorial ANOVA estimates main effects and interactions between two categorical factors, with cell means and reproducible R output.
Learning outcomes
After completing this lecture, you should be able to:
- Describe a two-factor factorial design using cells, main effects and interaction.
- Interpret a treatment-by-factor interaction before interpreting marginal main effects.
- Fit a balanced two-way ANOVA using linear regression in R.
- Calculate and interpret simple effects and interaction contrasts.
- Explain the importance of factor coding, balance and sums-of-squares conventions.
- Report cell means, interaction estimates and confidence intervals.
Overview
A factorial design studies two or more factors simultaneously and has a long history in efficient experimental design [1].
In a two-by-two design, two factors each have two levels, creating four cells.
Factorial ANOVA estimates:
- the main effect of factor A;
- the main effect of factor B;
- the interaction between factors A and B.
An interaction means that the effect of one factor differs according to the level of the other factor [2].
When interaction is present, marginal main effects can conceal clinically important differences. Cell means and simple effects should be prioritised.
Why this matters
Suppose telemonitoring lowers blood pressure substantially without coaching but has little additional effect when intensive coaching is already present.
The average telemonitoring effect across coaching levels may appear modest. The interaction reveals that the effect is not constant.
Testing one subgroup and not another separately is not an interaction test. The correct question is whether the subgroup effects differ from each other.
When to use this method
Use factorial ANOVA when:
- the outcome is numerical;
- two or more categorical factors define independent groups;
- cell-level means and interactions are scientifically meaningful;
- observations are independent;
- and the linear-model assumptions are sufficiently reasonable.
If one factor is repeated within participants, use a mixed or repeated-measures factorial model rather than ordinary between-participant ANOVA.
Research question and variables
The outcome is 12-week systolic blood pressure.
Factors are:
- treatment;
- coaching.
The four population cell means are the basic quantities.
The interaction null states that the telemonitoring effect is the same under coaching and no coaching.
Data structure
Two-by-two factorial structure
simulated data| treatment | coaching | participants | Mean systolic blood pressure |
|---|---|---|---|
| Usual care | No coaching | 15 | 144.87 mmHg |
| Usual care | Coaching | 15 | 136.40 mmHg |
| Telemonitoring | No coaching | 15 | 135.27 mmHg |
| Telemonitoring | Coaching | 15 | 138.13 mmHg |
Note: Means are simulated.
A plot of the four means is essential. Non-parallel lines suggest interaction; crossing lines indicate qualitative interaction but are not required for statistical interaction.
Statistical assumptions
Balanced designs simplify interpretation because factor contrasts are orthogonal. In unbalanced observational data, sequential, type II and type III sums of squares can test different hypotheses. The analysis plan must specify the estimand and coding.
How the method works
A two-factor model is:
If interaction is clinically important, report:
- cell means;
- simple effect of treatment within each coaching level;
- interaction contrast and CI.
Worked example
Reproducible R analysis
Base R fits the factorial model with lm() and produces an ANOVA table with anova() [3], [4].
factorial <- data.frame(
treatment = factor(
rep(c("Usual care", "Telemonitoring"), each = 30),
levels = c("Usual care", "Telemonitoring")
),
coaching = factor(
rep(rep(c("No coaching", "Coaching"), each = 15), 2),
levels = c("No coaching", "Coaching")
),
sbp_week12 = c(
141,148,133,139,146,149,137,147,153,160,144,142,144,141,149,
123,144,133,137,137,141,143,124,133,142,130,130,147,140,142,
141,131,135,126,133,135,148,126,141,132,131,129,148,132,141,
135,145,141,144,138,146,140,136,134,133,146,125,130,140,139
)
)
# `lm()` fits the factorial linear model [3]
fit <- lm(
sbp_week12 ~ treatment * coaching,
data = factorial
)
# `anova()` provides the sequential ANOVA table for this balanced design [4]
anova(fit)
summary(fit)
confint(fit)
R ANOVA output
Df Sum Sq Mean Sq F value Pr(>F)
treatment 1 232.1 232.1 5.025 0.0290
coaching 1 117.6 117.6 2.547 0.1162
treatment:coaching 1 481.7 481.7 10.431 0.0021
Residuals 56 2586.0 46.2
R coefficient output
Estimate Std. Error t value Pr(>|t|)
(Intercept) 144.8667 1.754 82.6 <2e-16
treatmentTelemonitoring -9.6000 2.481 -3.87 0.00029
coachingCoaching -8.4667 2.481 -3.41 0.00120
treatmentTelemonitoring:coachingCoaching 11.3333 3.510 3.23 0.00208
Cell means and simple effects
cell_means <- with(
factorial,
tapply(sbp_week12, list(treatment, coaching), mean)
)
tele_effect_no_coaching <-
cell_means["Telemonitoring", "No coaching"] -
cell_means["Usual care", "No coaching"]
tele_effect_coaching <-
cell_means["Telemonitoring", "Coaching"] -
cell_means["Usual care", "Coaching"]
interaction <-
tele_effect_coaching - tele_effect_no_coaching
cell_means
interaction
Interpreting the output
Presenting the results
| Treatment | No coaching mean (SD) | Coaching mean (SD) |
|---|---|---|
| Usual care | 144.9 (6.7) | 136.4 (7.3) |
| Telemonitoring | 135.3 (7.1) | 138.1 (6.1) |
Treatment-by-coaching interaction 11.3 mmHg (95% CI 4.3 to 18.4), , .
Suggested reporting
Factorial efficiency and interaction power MMed extension
Factorial designs can estimate two intervention main effects efficiently when interaction is negligible and treatments can be combined safely.
However, the study may have much less power for interaction than for each main effect. A non-significant interaction does not prove absence of effect modification.
The primary sample-size calculation should reflect whether interaction is a confirmatory objective; general design principles are developed in standard experimental-design texts [5].
Common mistakes
Knowledge check
What does interaction mean in this study?
Answer and reasoning
It means the telemonitoring effect on mean blood pressure differs according to whether coaching is present.
Why are separate subgroup p-values not an interaction test?
Answer and reasoning
The question is whether subgroup effects differ. That requires a direct difference-in-differences estimate and its standard error.
What is the simple telemonitoring effect without coaching?
Answer and reasoning
It is 135.27 minus 144.87, or −9.60 mmHg.
Why should marginal main effects be interpreted cautiously when interaction is present?
Answer and reasoning
They average over levels where the effect differs and may not represent any clinically relevant subgroup.
What changes if coaching is repeated within the same participants?
Answer and reasoning
The observations become dependent, requiring a mixed or repeated-measures factorial model rather than ordinary between-participant ANOVA.
Key take-home messages
References
- Fisher RA. The design of experiments. Edinburgh: Oliver and Boyd; 1935.
- VanderWeele TJ, Knol MJ. A tutorial on interaction. Epidemiol Methods. 2014;3(1):33–72. https://doi.org/10.1515/em-2013-0005
- R Core Team. Fitting linear models. R documentation, package stats version 4.6.1. Vienna: R Foundation for Statistical Computing; 2026. https://search.r-project.org/R/refmans/stats/html/lm.html
- R Core Team. ANOVA for linear model fits. R documentation, package stats version 4.6.1. Vienna: R Foundation for Statistical Computing; 2026. https://search.r-project.org/R/refmans/stats/html/anova.lm.html
- Montgomery DC. Design and analysis of experiments. 10th ed. Hoboken (NJ): Wiley; 2019.
These notes support learning and research planning and do not replace statistical consultation for a specific study.