Method · Statistics Library

Two-way (factorial) ANOVA

Two-way ANOVA tests how a continuous response is affected by two categorical factors and their interaction at once, decomposing total variation into two main effects, an interaction, and residual error, each with its own F-test.

Topic indexA reference reading outside the guided chapter sequence

Understand the idea

This test lets you study TWO categorical factors and a continuous outcome together in one analysis, not just 'does the training program matter?' and separately 'does region matter?', but ALSO 'does the training program's effect depend on which region you're in?'. That third question is the interaction, and it is often the most interesting result in the whole analysis.

Read the interaction FIRST, before either main effect, because it changes how you should read everything else. If the interaction is significant, it means the effect of one factor genuinely DEPENDS on the level of the other: a single 'average' main effect for either factor no longer tells the whole story, and can even be actively misleading. In that case, the right move is to look at 'simple effects' (the effect of one factor separately within each level of the other) rather than trusting the pooled main effects. If the interaction is NOT significant, the two main effects can be interpreted directly and simply, exactly as you'd expect.

When your design is unbalanced (different numbers of observations in different combinations of the two factors), there's a subtlety in exactly how to split up the variation between the two factors, controlled by a 'sum-of-squares type'. For a perfectly balanced design, this choice doesn't matter at all: all versions agree.

Two-way ANOVA is the simplest factorial ANOVA. An interaction effect means the effect of one factor depends on the level of the other. Type II sum of squares tests each main effect after allowing for the other main effect, which keeps the results sensible when group sizes are unequal.

Practitioner explanation

KernelStats fits via statsmodels OLS with a factorial formula (response ~ C(A)*C(B) when interactions are included) and reports SS/F/p per term via anova_lm at the requested type. Type II is the DEFAULT, and the product states directly when each type is appropriate: Type II tests each main effect adjusted for the OTHER main effect but not the interaction (appropriate when no interaction is assumed, or the interaction is not of primary interest); Type III tests every term adjusted for ALL others including the interaction, but CAUTION: the engine builds the factors with default treatment (reference-level) coding, and with that coding statsmodels' Type III main-effect tests in a model WITH an interaction are tests of each factor at the reference level of the other, not the SPSS/SAS Type III tests (which need sum-to-zero coding). On an unbalanced design with an interaction the card's Type III main-effect F values therefore will not match SPSS; the interaction row is unaffected. Until this is fixed, prefer Type II and read simple effects when the interaction matters; Type I (sequential) is named as rarely appropriate, since results then depend on the arbitrary order terms are entered.

Partial eta-squared = SS_effect/(SS_effect+SS_residual) is reported per term, holding the OTHER effects fixed, a genuinely different quantity from ordinary (non-partial) eta-squared, since the denominator excludes the variance explained by other terms in the model. A term whose F/p come out undefined (a rank-deficient model, for instance from an empty design cell) is handled explicitly: its partial eta-squared is correctly reported as undefined too, rather than computing a nonsensical ratio from an undefined numerator or denominator.

Three assumptions are checked: independence (by design), normality of residuals (Shapiro-Wilk), and homogeneity of variance ACROSS ALL DESIGN CELLS specifically (Levene's test), not just across the levels of one factor, since a two-way design's cells are the genuinely relevant comparison groups for the equal-variance assumption. The card's own guidance on failure modes is specific rather than generic: unequal variances point toward a robust/Welch-type analysis or a response transformation; non-normal residuals point toward a rank-based/nonparametric factorial approach (naming ART ANOVA specifically); strong outliers point toward inspection and possibly robust regression.

When to use it

Consider it when

You have one continuous response and TWO categorical factors, and want to test both main effects and whether the two factors interact.

Use caution when

You only have one factor (use one-way ANOVA), the design has fundamentally unbalanced, near-empty cells that make the model rank-deficient, or the residuals badly fail normality (consider a rank-based factorial approach instead of the ordinary F-tests).

Formulas and symbols

Two-way ANOVA F statistic (per term)

F=MSeffectMSresidualF = \dfrac{MS_{\text{effect}}}{MS_{\text{residual}}}

Computed separately for Factor A's main effect, Factor B's main effect, and the A x B interaction, each against the residual mean square.

Partial eta-squared

ηp2=SSeffectSSeffect+SSresidual\eta_p^2 = \dfrac{SS_{\text{effect}}}{SS_{\text{effect}} + SS_{\text{residual}}}

The proportion of residual-plus-effect variance attributable to that specific effect, holding the other effects fixed. Cohen's marks: 0.01 small, 0.06 medium, 0.14 large.

Assumptions and checks

  • Independence of observations. Observations within and across cells must be independent; violating this invalidates every F-test in the table regardless of what the normality and variance checks show.

    In KernelStats: Not checkable from the data alone: it is a property of how the data were collected.

  • Normality (of residuals, not of the raw data). Non-normal residuals distort the true Type I error rate of the main-effect and interaction F-tests.

    In KernelStats: Shapiro-Wilk on the model residuals (sampled for large n), reported directly on the card.

  • Homogeneity of variance (homoscedasticity). Unequal variance across the factor-combination cells, especially with unbalanced cell sizes, distorts the F-tests' true Type I error rate.

    In KernelStats: Levene's test (median-centered) across cells, computed when at least two cells have 2+ observations; reported directly on the card.

Alternatives

  • One-Way ANOVA. Only a single categorical factor is actually of interest.
  • ANCOVA (Analysis of Covariance). One of the two predictors is actually a continuous covariate rather than a categorical factor.
  • MANOVA (Multivariate ANOVA). There are SEVERAL continuous outcomes to test jointly against the same factors, rather than a single response.

Worked example

Scenario

Testing customer satisfaction as a function of training program (2 levels) and region (3 levels), with their interaction, on a balanced design.

Result

Training program: F(1,144)=8.2, p=.005, partial eta-squared=0.05; Region: F(2,144)=1.1, p=.34; Training x Region interaction: F(2,144)=6.7, p=.002, partial eta-squared=0.09.

Interpretation

Because the interaction is significant, the training program's effect should NOT be read as a single uniform boost across all regions; simple-effects analysis within each region is needed to see where the training actually helped, rather than reporting the overall training main effect (F=8.2) as if it applied equally everywhere.

Common misconceptions

Misconception

When a two-way ANOVA's interaction is significant, you can still safely interpret and report the two main effects as the headline result.

Correction

A significant interaction means the effect of one factor genuinely DEPENDS on the level of the other; a single averaged 'main effect' can be actively misleading in that situation, since it blends together what may be very different (even oppositely-signed) effects at different levels of the other factor. The correct approach is to lead with the interaction and examine simple effects (the effect of one factor within each level of the other) rather than reporting the main effects as if they told the complete story.

Misconception

The choice between Type I, II, and III sums of squares never really matters in practice.

Correction

For a perfectly BALANCED design, all three types agree exactly, and the choice is indeed inconsequential. But for an UNBALANCED design (unequal cell sizes, common in real observational data), the three types can give genuinely different F-tests and p-values for the same data, because they allocate shared variance between overlapping factors differently; the choice matters a great deal in that common, realistic situation.

References

  1. Design and Analysis of Experiments, Montgomery, D. C. (2019).
  2. Type I, Type II, Type III sums of squares: The nature of the sum of squares in a linear model, Langsrud, O. (2003), Statistics and Computing, 13(2), 163-167.