BCompare groups · Guide test 017
Factorial ANOVA F tests
Test main effects and interactions in designs with multiple factors; this is a family of model based tests.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- Factorial ANOVA (two or three between factors) — Do two or more factors, and their interaction, affect the mean of an outcome?
Do it in CodeSplash Run this test on your own data, in Python or R.
Bench
Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.
Bench · Factorial ANOVA F tests
Your data
Drag a point or use ↑/↓ to change itIf H₀ were true
- The reference: how F varies across samples when H₀ is true.
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = < .001).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- interaction sum of squares
- within-cell sum of squares
- levels of A and B, per-cell size
The verdict
F(2, 42) = 9.81, p < .001, η²_p = 0.32
p ≤ α = .050: reject H₀
Effect size: partial eta squared (interaction) η²_p = 0.318
Test the interaction first: if A × B is real, main effects average over different stories and must be read cell by cell.
- Statistic
- 9.811
- p-value
- 0.0003
Factorial ANOVA F tests on A1·B1: n = 8; A1·B2: n = 8; A1·B3: n = 8; A2·B1: n = 8; A2·B2: n = 8; A2·B3: n = 8: F(2, 42) = 9.81, p < .001, η²_p = 0.32. The p-value is below α = .050, so the data are hard to reconcile with H₀.
Question and hypotheses
H0: a selected main effect or interaction contrast vector equals zero; H1: at least one selected contrast differs.
Assumptions
A correctly specified factorial linear model and independent normal, equal variance errors give classical exact F inference. The design must identify the requested contrasts.
Formula
Parameters and notation
R defines the q tested contrasts, p is the number of fitted coefficients, and N is total observations. Main effect interpretation depends on interactions and coding.
Structure of the method
Construct a design matrix for factors and interactions, fit the full linear model, and test an estimable contrast subspace using a quadratic F statistic or equivalent nested RSS comparison. The matrix coding and sums-of-squares convention determine which scientific hypothesis a displayed 'main effect' actually represents.
Estimated and nuisance parameters
Estimate cell or regression coefficients and a common residual variance. Contrasts can express marginal or conditional effects, but their estimability depends on observed cells and design rank. The factor levels, contrast weighting, and interaction specification are design inputs; they cannot be inferred merely from the coefficient names printed by software.
Settings and design choices
Choose Type I, II, or III hypotheses deliberately, including factor coding and weighting for unbalanced data. Prespecify interaction tests and follow-up simple effects, with multiplicity control for the selected family. Changing factor order in sequential Type I ANOVA can change a test because it changes the adjustment set.
Fitting and adaptation
Refit the full model and recompute contrasts after releveling, changing interactions, or revising the observation mask. A coding change preserving the same estimable contrast should preserve its test. Adaptation to heteroskedastic or clustered errors requires a justified robust contrast test rather than assuming the classical exact F law remains valid.
Sensitivity and robustness
Empty cells and aliasing may make nominal main effects uninterpretable or unestimable. Strong interactions make averaged main effects an incomplete summary. Sparse cells can have high leverage, while a balanced total sample can conceal imbalance in individual cells. The supplied examples use sequential tests on balanced fixtures, not every adjusted-ANOVA convention.
How to use and calibrate
Define the scientific contrast, fit the full factorial model, construct R, and use the F reference. In unbalanced designs specify whether sequential or adjusted hypotheses are intended.
Example application
In a treatment by temperature experiment, first test the interaction to determine whether treatment differences vary with temperature.
Interpretation and pitfalls
Watch out
Type I, II, and III sums of squares can test different hypotheses in unbalanced data. A main effect averaged over an interaction can obscure important conditional differences.
What changed in edition 2
- New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.
Python and R
Balanced 2×3 design, including interaction; both examples use sequential/type-I sums of squares.
Validated — Executed successfully in the validation environment · needs numpy, pandas, scipy, statsmodels
import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
import pandas as pd
import statsmodels.api as sm
from statsmodels.formula.api import ols
d=pd.DataFrame([(a,b) for a in "AB" for b in "XYZ" for _ in range(12)],columns=["a","b"])
d["y"]=rng.normal(size=len(d))+.4*(d.a=="B")
print(sm.stats.anova_lm(ols("y~C(a)*C(b)",d).fit(),typ=1))
Output
df sum_sq mean_sq F PR(>F) C(a) 1.0 3.892198 3.892198 4.372804 0.040370 C(b) 2.0 2.363533 1.181767 1.327690 0.272075 C(a):C(b) 2.0 11.158638 5.579319 6.268249 0.003218 Residual 66.0 58.746079 0.890092 NaN NaN
Validated — Executed successfully in the validation environment · needs Base/standard library
set.seed(2026)
d <- expand.grid(A=factor(1:2),B=factor(1:3),rep=1:12); d$y <- rnorm(nrow(d))+as.numeric(d$A)*.4
print(summary(aov(y~A*B,d)))
Output
Df Sum Sq Mean Sq F value Pr(>F) A 1 12.14 12.138 13.937 0.000396 *** B 2 0.12 0.058 0.067 0.935497 A:B 2 0.03 0.015 0.017 0.982755 Residuals 66 57.48 0.871 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Sources
- R stats — R stats · anova.lmstat.ethz.ch
- anova.lmstat.ethz.ch
- Statistics guide v4 (historical):
- Python: Executed successfully in the validation environment · R: Executed successfully in the validation environment
- Reference's record:
- Edition 2 review: Python: skip · R: not executed (Rscript unavailable); why not run: Python: Unavailable local dependencies: statsmodels. Python: Not run in the reference's review: Unavailable local dependencies: statsmodels. No installation attempted. R: Reviewed but not run: R wasn't available to the reference's authors.
From the CodeSplash statistics guide (v4), entry 017, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT017.