Two-Way Analysis of Variance
Two-way analysis of variance extends one-way ANOVA to designs with two independent factors. Rather than asking whether group means differ along a single classification — as one-way ANOVA does — two-way ANOVA simultaneously estimates the effect of each factor on its own (main effects) and whether the effect of one factor depends on the level of the other (interaction). The interaction is often the most clinically important question: a drug that works in men but not in women, or a treatment whose benefit depends on disease severity, produces an interaction even if the main effects appear modest. Both factors must be categorical (or reduced to categorical levels); the outcome is continuous. When the design includes a within-subjects (repeated-measures) factor, the appropriate method is repeated-measures ANOVA.
Main Effects and Interaction
Imagine a study in which patients are assigned to one of two treatments (drug or placebo) and simultaneously classified by a second factor — say, smoking status (non-smoker or smoker). There are four groups in total, one for each combination of treatment and smoking status, and the data form a 2 × 2 table of cell means.
A main effect is the average influence of one factor, pooled across all levels of the other. The main effect of treatment is the average difference between drug and placebo, ignoring smoking status; the main effect of smoking status is the average difference between non-smokers and smokers, ignoring treatment allocation. A main effect summarises the overall impact of a factor but says nothing about whether that impact is uniform across subgroups.
An interaction exists when the effect of one factor is not the same across the levels of the other. If the drug reduces blood pressure by 8 mmHg in non-smokers but by only 2 mmHg in smokers, the two factors interact: the treatment benefit depends on smoking status. Equivalently, the combination of drug treatment and non-smoking produces a larger benefit than would be expected if the two factors acted entirely independently of one another.
Interaction is not merely a statistical complication — it is frequently the primary scientific finding. When a significant interaction is present, the effect of each factor is best described separately for each level of the other factor. The overall average drug effect — a weighted mixture of the 8 mmHg gain in non-smokers and the 2 mmHg gain in smokers — exists as an arithmetic fact but does not represent what the drug does in any particular patient. Reporting only the average conceals the clinically important distinction between subgroups.
Tests of Between-Subjects Effects
The primary output of a two-way ANOVA is the tests of between-subjects effects table. The total variability in the data is partitioned into four components, each with its own F-test:
| Source | Degrees of freedom | What it captures |
|---|---|---|
| Factor A | $a - 1$ | Variability due to the main effect of factor A |
| Factor B | $b - 1$ | Variability due to the main effect of factor B |
| A × B interaction | $(a-1)(b-1)$ | Variability due to the combined, non-additive influence of A and B |
| Residual | $N - ab$ | Within-cell variability unexplained by the model |
Here $a$ and $b$ are the number of levels of factors A and B, and $N$ is the total sample size. Each F-statistic is the ratio of the mean square for that source to the residual mean square. A P value below 0.05 indicates a statistically significant effect.
The interaction test should be examined first. If the interaction is significant, the main effects of A and B must not be interpreted independently: the effect of each factor differs across the levels of the other, so a single averaged main effect is a misleading summary. The marginal means for each factor will still be reported, but they represent averages over subgroups that may behave very differently.
Assumptions
Two-way ANOVA rests on the same three assumptions as one-way ANOVA.
- Independence. All observations must be independent of one another, both within and across groups. This is a design requirement met by proper randomisation; it is violated when the same subjects contribute measurements to more than one cell, or when observations within a cell are clustered. When the same subjects are measured under all combinations of both factors, repeated-measures ANOVA is the appropriate method.
- Normality of residuals. The residuals — the deviations of individual observations from their cell means — should follow a normal distribution. With reasonably large cells the F-tests are robust to moderate non-normality, but with small cells this assumption deserves attention. Residuals can be saved and assessed visually or with a formal normality test, as described in the Are the data normally distributed? chapter.
- Equal variances across cells (homoscedasticity). The population variance is assumed to be the same in all cells. Levene's test provides a formal check: a significant result (P < 0.05) indicates that the cell variances differ and the equal-variance assumption is not met. Levene's test can be oversensitive in large samples, so visual inspection of residuals complements the formal test.
Estimated Marginal Means and Pairwise Comparisons
After fitting the model, MedCalc reports estimated marginal means for each level of each factor: the model-based mean at that level, averaged across all levels of the other factor, with its standard error and 95% confidence interval. When group sizes are unequal, these model-based averages are balanced across the design in a way that raw group averages are not.
Estimated marginal means are the predicted mean scores for a specific level of one factor, obtained by averaging across all levels of the other factor in the design.
Consider a 2 × 2 study with two factors: Diet (A vs B) and Exercise (low vs high). The estimated marginal mean for Diet A is the average of the two cell means in the Diet A row — one from the low-exercise group and one from the high-exercise group — collapsing exercise level entirely.
- In a balanced design (equal numbers of observations in every cell), the estimated marginal mean equals the ordinary arithmetic mean for that factor level.
- In an unbalanced design (unequal cell sizes), estimated marginal means weight each level of the other factor equally, regardless of how many participants it contains. This prevents a large subgroup from disproportionately pulling the overall mean toward its own value.
Alongside the marginal means, MedCalc provides pairwise comparisons with Bonferroni-corrected P values and 95% confidence intervals for the pairwise differences. The Bonferroni correction ensures that the probability of any false-positive result across all reported comparisons stays at or below 5%. When only two levels exist for a factor — as in a simple drug-versus-placebo design — there is a single pairwise difference and no correction is needed; for factors with three or more levels the correction becomes meaningful.
If the interaction is significant, the marginal mean comparisons for the main effects remain valid as arithmetic averages but their clinical meaning is limited. The more informative analysis is then to examine the effect of each factor separately within each level of the other, using the cell means directly.
Residual Analysis
The normality assumption for two-way ANOVA applies to the residuals from the fitted model, not to the raw observations. MedCalc can save the model residuals to the spreadsheet, where they can be assessed with a histogram, a box-and-whisker plot, or a normal probability plot, or tested formally with a normality test. Examining residuals is particularly valuable in small datasets, where the central limit theorem offers little protection against departures from normality.
Worked Example
A randomised trial tests a new antihypertensive drug against placebo, with participants simultaneously classified by smoking status (non-smoker vs smoker). Fifteen patients are allocated to each of the four treatment-by-smoking cells (60 patients in total). The outcome is the reduction in systolic blood pressure (mmHg) after 24 weeks.
The cell means and standard deviations are:
| Non-smoker | Smoker | Marginal mean | |
|---|---|---|---|
| Placebo | 4.2 (SD 3.8) | 4.0 (SD 4.1) | 4.1 |
| Drug | 12.6 (SD 4.0) | 5.8 (SD 3.9) | 9.2 |
| Marginal mean | 8.4 | 4.9 |
A preliminary pattern is already visible: in non-smokers the drug reduces blood pressure by 8.4 mmHg more than placebo (12.6 − 4.2), whereas in smokers the additional reduction is only 1.8 mmHg (5.8 − 4.0). The drug appears substantially more effective in non-smokers, which suggests interaction.
Levene's test yields P = 0.82, giving no evidence that the cell variances differ. The tests of between-subjects effects are:
| Source | df | F | P |
|---|---|---|---|
| Treatment | 1 | 25.0 | <0.001 |
| Smoking status | 1 | 11.8 | 0.001 |
| Treatment × Smoking status | 1 | 10.5 | 0.002 |
| Residual | 56 |
The interaction is significant (P = 0.002), confirming that the treatment effect differs between smoking strata. The estimated marginal means and Bonferroni pairwise comparisons show:
- Overall drug effect: 5.1 mmHg (95% CI 3.1 to 7.2, P < 0.001).
- Overall effect of smoking status: 3.5 mmHg (95% CI 1.5 to 5.5, P = 0.001), with non-smokers showing the larger reduction on average.
Because the interaction is significant, these averaged figures should not be reported as the primary result. The average drug effect of 5.1 mmHg is a mixture of an 8.4 mmHg reduction in non-smokers and a 1.8 mmHg reduction in smokers; presenting only the average conceals the finding of greatest clinical relevance. The appropriate conclusion is that the drug produces a meaningful reduction in blood pressure in non-smokers but not in smokers — a distinction with direct implications for prescribing decisions.
MedCalc computes two-way ANOVA with Levene's test, estimated marginal means, and Bonferroni post-hoc comparisons in its Two-way analysis of variance procedure.