Below is a “recipe” that researchers typically follow when they want permutation (randomisation / exchangeability) tests for a 2-way factorial design
Y = μ + Aᵢ + Bⱼ + (AB)ᵢⱼ + ε
with independent units (or subjects) inside every cell (i,j).
──────────────────────────────────────────────
- Pick the test statistic
──────────────────────────────────────────────
• Use the usual ANOVA F-ratio, a partial R², or the corresponding sum-of-squares (SS).
Because ANOVA decomposes SS additively, the same statistic works for the permutation test.
──────────────────────────────────────────────
2. Why we cannot “shuffle everything” for every effect
──────────────────────────────────────────────
• A global permutation of the raw responses would test the single null
H₀: μᵢⱼ = μ for all i,j.
That is not the null we want for “main effect of A”, “main effect of B”, or “interaction”.
• To isolate one effect we must keep the nuisance terms intact while we destroy only the target effect.
Two popular ways to do that are
(i) Constrained label permutations (a.k.a. “stratified permutations”)
(ii) Permuting residuals from a reduced model (Freedman–Lane family of procedures)
──────────────────────────────────────────────
3. Practical algorithms
──────────────────────────────────────────────
Below are working algorithms for each of the three effects.
(They all assume a balanced or at least orthogonal design; with unbalanced data,
Freedman–Lane is preferred.)
──────── A. Main effect of A ────────
Method 1 (Constrained label shuffle)
- Within every level of B, randomly permute the A labels across units.
→ B totals and AB interaction structure remain intact.
- Re-compute the F for factor A.
- Repeat Nperm times → permutation distribution of F_A.
- p = ( # F* ≥ F_obs + 1 ) / (Nperm + 1).
Method 2 (Freedman–Lane residual shuffle) ← easiest to code
- Fit the reduced model without A:
Y = μ + B + AB (i.e. keep all terms except A).
- Save the fitted values Ŷ and residuals r.
- Permute r arbitrarily (all units interchangeable under H₀: A=0).
- Create Y* = Ŷ + r*.
- Fit the full model with A, B, AB to Y*; record F_A*.
- Repeat → distribution → p-value.
──────── B. Main effect of B ────────
• Same as above, but swap the roles of A and B.
──────── C. Interaction A×B ────────
Method 1 (Constrained shuffle)
- Shuffle the entire (A,B) pairs across units.
That keeps each main effect intact but breaks the concordance between them.
Equivalently: independently permute A levels and B levels, or
permute A within B and B within A.
- Re-compute F_AB, build the distribution.
Method 2 (Freedman–Lane)
- Reduced model: Y = μ + A + B (no interaction).
- Permute residuals r, add back Ŷ, refit full model including AB.
- Compute F_AB*, iterate, p-value.
──────────────────────────────────────────────
4. Which scheme should I use?
──────────────────────────────────────────────
• Balanced data, equal variances, no missing cells → either scheme works.
• Unbalanced, heteroskedastic, or random effects in one factor →
Freedman–Lane (or its cousins Anderson–Robinson, Ter Braak) is safer, because
it conditions on the observed nuisance effects instead of assuming exchangeability.
──────────────────────────────────────────────
5. Quick R example (Freedman–Lane, effect = “A”)
──────────────────────────────────────────────
perm_test_ANOVA <- function(y, A, B, nperm = 5000, effect = "A") {
dat <- data.frame(y, A = factor(A), B = factor(B))
# FULL model
full <- lm(y ~ A * B, data = dat)
# REDUCED model depends on effect
red_formula <- switch(effect,
"A" = y ~ B + A:B,
"B" = y ~ A + A:B,
"AB" = y ~ A + B)
red <- lm(red_formula, data = dat)
# observed statistic
F_obs <- anova(full)[effect, "F value"]
# permutation loop
F_star <- numeric(nperm)
for (i in seq_len(nperm)) {
y_star <- fitted(red) + sample(residuals(red))
full_star <- lm(y_star ~ A * B, data = dat)
F_star[i] <- anova(full_star)[effect, "F value"]
}
(sum(F_star >= F_obs) + 1) / (nperm + 1) # p-value
}
(The same logic can be written in Python/statsmodels; just keep the model matrices.)
──────────────────────────────────────────────
6. Multiple testing and reporting
──────────────────────────────────────────────
• You will obtain three p-values (A, B, AB).
If you want to control family-wise error, adjust (e.g. maxT step-down, Bonferroni, FDR).
• Always report:
– Test statistic used
– Permutation scheme (e.g. “Freedman–Lane, 10 000 permutations”)
– Any finite-sample corrections (random tie-breaking, +1 in numerator & denominator)
– Confidence intervals if desired (can be built by inversion of the test).
──────────────────────────────────────────────
Key references
──────────────────────────────────────────────
• Freedman, D., & Lane, D. (1983). A Nonstochastic Interpretation of Reported Significance Levels. J. Bus. Econ. Stat.
• Anderson, M. J., & Robinson, J. (2001). Permutation tests for linear models. Aust. N.Z. J. Stat.
• Manly, B. F. J. (1997). Randomization, Bootstrap and Monte Carlo Methods in Biology.
That’s it! Choose a permutation scheme that keeps everything that must stay fixed under the null, shuffle what must be random under the null, and use the standard ANOVA statistics to finish the job. 🎲