BCompare groups · Guide test 041
Tukey HSD and Tukey–Kramer
Compare all pairs of means with familywise error control under a common variance; Kramer accommodates unequal sample sizes.
Do it in CodeSplash
In the app, set it up in Analyze with one of these methods:
- ANCOVA (group means adjusted for covariates) — Do group means differ after adjusting for one or more numeric covariates?
- Factorial ANOVA (two or three between factors) — Do two or more factors, and their interaction, affect the mean of an outcome?
- One-way ANOVA (Welch / classic) — Do the means of three or more independent groups differ?
- Pairwise comparisons (Tukey, Games–Howell, Dunnett, Dunn, Steel–Dwass …) — Which groups differ from which?
In One-way ANOVA (Welch / classic), choose Post hoc comparisons = Tukey–Kramer. In Pairwise comparisons (Tukey, Games–Howell, Dunnett, Dunn, Steel–Dwass …), choose Comparison = Tukey–Kramer.
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 · Tukey HSD and Tukey–Kramer
Group i has true mean (i − 1) × spread.
Your data
Drag a point or use ↑/↓ to change itIf H₀ were true
- The reference: how q varies across samples when H₀ is true, the studentized range q(4, 36).
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .054).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The whole family
| Pair | Difference in means with 95% simultaneous interval | p | p adj. |
|---|---|---|---|
| .412 | .840 | ||
| .120 | .395 | ||
| .012 | .054 | ||
| .450 | .870 | ||
| .076 | .277 | ||
| .293 | .712 |
6 pairs; adjusted by studentized range (Tukey–Kramer). The highlighted row is the one in the reference pane (the most extreme by default).
The formula
With your numbers
- pooled within-group mean square on N − k df
- studentized range for k groups and ν df
The verdict
q = 3.76, p = .054, Δ = −1.24
p > α = .050: compatible with H₀
Effect size: Group 1 − Group 4 (difference in means) Δ = −1.239
All pairs at once with the pooled variance: the family-wise error is exact for equal n and conservative otherwise. Unequal spreads call for Games–Howell.
- Statistic
- 3.760
- p-value
- 0.0541
Tukey HSD and Tukey–Kramer on Group 1: n = 10; Group 2: n = 10; Group 3: n = 10; Group 4: n = 10: q = 3.76, p = .054, Δ = −1.24. The p-value is above α = .050, so the data are compatible with H₀.
Question and hypotheses
H0ij: μi=μj for every pair in a simultaneous comparison family.
Assumptions
Independent normal errors with common variance in a one factor model; group sizes may differ. The family of comparisons must be specified.
Formula
Parameters and notation
MSE is pooled within group mean square; k is group count; ν=N−k. Equal sizes give Tukey HSD; unequal sizes give Tukey–Kramer.
Structure of the method
Fit the common-variance one-way model, estimate all mean differences, and scale each by the pooled standard error appropriate to a studentized range. Tukey uses the balanced case; Tukey–Kramer extends the denominator to unequal sample sizes. Joint range calibration produces adjusted p-values or simultaneous intervals for the all-pairs family.
Estimated and nuisance parameters
Estimate group means and one pooled residual mean square. Residual degrees of freedom come from the fitted model, while the number of groups defines the range distribution. The all-pairs comparison family is specified by the included factor levels; it is not a subset chosen after looking at significant results.
Settings and design choices
Choose familywise alpha, included levels, measurement transformation, and the common-variance model. An omnibus ANOVA gate is not necessary for Tukey's simultaneous guarantee under its own assumptions. Restricting the scientific question to treatment-versus-control comparisons may favor Dunnett, whose different family can yield more targeted intervals.
Fitting and adaptation
Refit pooled variance and every group estimate if observations or model terms change. For covariate-adjusted or factorial contrasts, use a simultaneous model-based procedure with the correct covariance rather than blindly substituting adjusted means into a one-way formula. A changed comparison family requires recalculating its joint critical value.
Sensitivity and robustness
Strong heteroskedasticity, extreme imbalance, and nonnormal small samples can undermine nominal error control. A nonsignificant adjusted comparison does not establish equivalence. Compact-letter displays can conceal interval width and nontransitive nonsignificance; retain estimated differences and simultaneous confidence intervals as the substantive result.
How to use and calibrate
Compare with the studentized range qk,ν or report its adjusted p values and simultaneous intervals.
Example application
Identify which of five treatment means differ while controlling familywise error.
Interpretation and pitfalls
Watch out
Not suitable for strongly unequal variances. Applying separate unadjusted t tests does not reproduce this simultaneous procedure.
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
Simultaneous all-pairs comparisons assuming a common variance; Tukey–Kramer handles unequal group sizes.
Validated — Executed successfully in the validation environment · needs numpy, scipy
import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
groups = [rng.normal(m,1,20) for m in (0,.4,.8)]
print(stats.tukey_hsd(*groups))
Output
Pairwise Group Comparisons (95.0% Confidence Interval) Comparison Statistic p-value Lower CI Upper CI (0 - 1) -0.707 0.059 -1.435 0.021 (0 - 2) -1.478 0.000 -2.206 -0.750 (1 - 0) 0.707 0.059 -0.021 1.435 (1 - 2) -0.771 0.035 -1.499 -0.043 (2 - 0) 1.478 0.000 0.750 2.206 (2 - 1) 0.771 0.035 0.043 1.499
Validated — Executed successfully in the validation environment · needs Base/standard library
set.seed(2026)
d <- data.frame(value=c(rnorm(20),rnorm(20,.4),rnorm(20,.8)), group=factor(rep(c("A","B","C"),each=20)))
print(TukeyHSD(aov(value~group,d)))
Output
Tukey multiple comparisons of means
95% family-wise confidence level
Fit: aov(formula = value ~ group, data = d)
$group
diff lwr upr p adj
B-A 1.1041489 0.4142563 1.7940416 0.0008641
C-A 0.9458261 0.2559335 1.6357187 0.0047013
C-B -0.1583229 -0.8482155 0.5315697 0.8457445Sources
- R stats — R stats · TukeyHSDstat.ethz.ch
- TukeyHSDstat.ethz.ch
- Statistics guide v4 (historical):
- R: Executed successfully in the validation environment
- Reference's record:
- Edition 2 review: Python: executed · R: not executed (Rscript unavailable). Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.
From the CodeSplash statistics guide (v4), entry 041, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT041.