BCompare groups · Guide test 024
Kruskal–Wallis H test
Tests equality of independent group distributions using pooled ranks; detects location differences under a shift model.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- Kruskal–Wallis test — Do three or more independent groups differ in location (without assuming normality)?
In Kruskal–Wallis test, choose Rank scores = Wilcoxon ranks (Kruskal–Wallis H).
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 · Kruskal–Wallis H test
Group i has true mean (i − 1) × spread.
Your data
Drag a point or use ↑/↓ to change itIf H₀ were true
- The reference: how H varies across samples when H₀ is true (χ² approximation).
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .061).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- the sum of group i's ranks
- group i's size
- the total sample size
The verdict
H(2) = 5.60, p = .061, ε² = 0.19
p > α = .050: compatible with H₀
Effect size: epsilon squared ε² = 0.193
The rank analog of one-way ANOVA, compared with χ² on k − 1 degrees of freedom. Follow a significant H with Dunn's pairwise tests.
- Statistic
- 5.597
- p-value
- 0.0609
Kruskal–Wallis H test on Group 1: n = 10; Group 2: n = 10; Group 3: n = 10: H(2) = 5.60, p = .061, ε² = 0.19. The p-value is above α = .050, so the data are compatible with H₀.
Question and hypotheses
H0: k independent groups have identical distributions.
Assumptions
Independent observations and independent groups; ordinal or quantitative measurements. The permutation null is identical group distributions. A location or median interpretation additionally needs comparable distribution shapes.
Formula
Parameters and notation
N=Σnj; R̄j is the pooled mean rank; each t is a pooled tie group size.
Structure of the method
Assign pooled midranks, compare group average ranks with the pooled rank center, and scale the quadratic sum with the standard tie correction. A chi-square reference is asymptotic; conditional label permutations preserve group sizes and the observed values under the identical-distribution null. Follow-up comparisons require their own specified family.
Estimated and nuisance parameters
Estimate group rank means and pooled tie multiplicities, not group means or a pooled raw-data variance. Group counts and sample sizes determine the null structure. Rank locations summarize distributional ordering; interpreting differences as medians requires a compatible shift-type model and cannot be inferred from the omnibus statistic alone.
Settings and design choices
Choose alpha, exact/Monte Carlo/asymptotic calibration, tie handling, missing-value policy, and post-hoc contrast procedure in advance. The number of permutations affects attainable p-value precision. Dunn, Conover, and pairwise re-ranked procedures use different denominators or ranks and should not be interchanged silently.
Fitting and adaptation
Re-rank the full pooled dataset after changing observations or groups, and recompute tie correction. A permutation analysis must exchange only units exchangeable under the experimental design. For repeated measurements use a blocked method rather than applying Kruskal–Wallis to correlated rows; ranking does not remove that dependence.
Sensitivity and robustness
Tiny groups and heavy ties can degrade the chi-square approximation and limit attainable significance. Distributional spread or shape differences may drive rejection without a simple common median shift. An all-tied dataset has zero rank variance and cannot support the usual statistic. Report group distributions and appropriate effect summaries.
How to use and calibrate
Use χ²k−1 approximately or a group label permutation distribution. Follow a significant omnibus result with justified multiplicity adjusted contrasts.
Example application
Compare ordered severity scores across four independent treatments.
Interpretation and pitfalls
Watch out
A rejection identifies some distributional difference, not which pair differs. Unequal shapes can invalidate a simple median interpretation.
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
Ranks/scores use all observations pooled across independent groups; the omnibus p-value is not a pairwise conclusion.
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.kruskal(*groups))
Output
KruskalResult(statistic=np.float64(17.288852459016397), pvalue=np.float64(0.00017610569040312084))
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(kruskal.test(value~group,data=d))
Output
Kruskal-Wallis rank sum test data: value by group Kruskal-Wallis chi-squared = 12.972, df = 2, p-value = 0.001525
Sources
- R stats — R stats · kruskal.teststat.ethz.ch
- kruskal.teststat.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 024, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT024.