Kruskal-Wallis test

Analytika TeamLast reviewed 8 min read

The Kruskal-Wallis H test (Kruskal & Wallis, 1952) extends the Mann-Whitney U test to three or more groups. It does the job of a one-way ANOVA without assuming normal data or equal variances.

How it works

All values are pooled and ranked from smallest to largest. If the groups don’t differ, each group’s average rank should be close to the overall average rank. H measures how far the groups’ average ranks spread apart, and it follows a chi-square distribution with k − 1 degrees of freedom.

  • H₀: all groups come from the same distribution.
  • H₁: at least one group tends to have larger (or smaller) values.

Like Mann-Whitney, it’s often described as a test of medians. That’s strictly true only if the groups’ distributions have the same shape.

When to use it

  • Three or more independent groups.
  • The outcome is ordinal, or continuous but skewed or outlier-prone.
  • Group sizes are small, so normality can’t be relied on.

For repeated measurements of the same people, use the Friedman test instead.

Worked example: ozone levels by month

R’s airquality dataset records daily ozone levels (parts per billion) in New York from May to September 1973. Do ozone levels differ by month?

Step 1: check the data

Ozone is strongly right-skewed (skewness 1.24), the spread differs a lot between months, and June has only 9 days with readings. Thirty-seven days have no ozone reading and are excluded, leaving 116. Every check points away from ANOVA: normality fails, variances are unequal, and one group is small. Analytika therefore chooses Kruskal-Wallis.

Step 2: run the test

Analytika engine output
H-statistic: 29.2666
P-value: 6.901e-06
Epsilon-squared: 0.2545
df: 4
N: 116
  5: median=18.0000, n=26, iqr=20.5000
  6: median=23.0000, n=9, iqr=17.0000
  7: median=60.0000, n=26, iqr=43.5000
  8: median=52.0000, n=26, iqr=53.7500
  9: median=23.0000, n=29, iqr=20.0000
Rows excluded for missing values: 37 of 153

=== POSTHOC ===
5 vs 6: p_adj=1.0000, significant=no, r=-0.2991, z=-0.9252, p_bonf=1.0000
5 vs 7: p_adj=0.0001, significant=yes, r=-0.6760, z=-4.4195, p_bonf=0.0001
5 vs 8: p_adj=0.0003, significant=yes, r=-0.6228, z=-4.1328, p_bonf=0.0004
5 vs 9: p_adj=0.7457, significant=no, r=-0.2467, z=-1.3212, p_bonf=1.0000
6 vs 7: p_adj=0.1489, significant=no, r=-0.5598, z=-2.2442, p_bonf=0.2482
6 vs 8: p_adj=0.2074, significant=no, r=-0.5085, z=-2.0386, p_bonf=0.4149
6 vs 9: p_adj=1.0000, significant=no, r=0.0153, z=0.0025, p_bonf=1.0000
7 vs 8: p_adj=1.0000, significant=no, r=0.0296, z=0.2867, p_bonf=1.0000
7 vs 9: p_adj=0.0104, significant=yes, r=0.5318, z=3.2172, p_bonf=0.0129
8 vs 9: p_adj=0.0243, significant=yes, r=0.4642, z=2.9228, p_bonf=0.0347
Method used: dunn
R 4.6 output
> kruskal.test(Ozone ~ factor(Month), data = airquality)

	Kruskal-Wallis rank sum test

data:  Ozone by factor(Month)
Kruskal-Wallis chi-squared = 29.267, df = 4, p-value = 6.901e-06

The same H (R calls it chi-squared), df and p. Analytika’s test suite checks the test, the effect size and the Dunn comparisons against R on every change.

Step 3: interpret

  • H(4) = 29.27, p < .001: there’s strong evidence that ozone levels differ across months.
  • ε² = .25: month accounts for about a quarter of the variation in the ranks, a large effect.
  • Medians rise from 18 ppb in May to 60 in July and 52 in August, then fall back to 23 in September.
  • Dunn’s test (Holm-adjusted): July and August each had higher ozone than May (p = .0001 and .0003) and than September (p = .010 and .024). June didn’t differ significantly from any month, partly because it has so few days.

Step 4: report it (APA 7)

A Kruskal-Wallis test showed that ozone levels differed by month, H(4) = 29.27, p < .001, ε² = .25. Dunn’s post-hoc comparisons with Holm correction indicated higher ozone in July (Mdn = 60 ppb) and August (Mdn = 52 ppb) than in May (Mdn = 18 ppb) and September (Mdn = 23 ppb), all ps ≤ .024.

These are observational data, so the pattern describes seasonal differences. It doesn’t identify what caused them (temperature and sunlight are the obvious candidates).

Post-hoc tests after Kruskal-Wallis

Dunn’s test is the standard follow-up. It compares mean ranks using the same ranking as the overall test, with a correction for the number of pairs (Holm is less conservative than Bonferroni). Running separate Mann-Whitney tests for every pair, without correction, inflates false positives. See post-hoc tests and multiple comparisons.

How to run it

  • SPSS: Analyze → Nonparametric Tests → Independent Samples, then choose Kruskal-Wallis. Pairwise comparisons are in the model viewer.
  • R: kruskal.test(y ~ group, data = df), then FSA::dunnTest() or dunn.test::dunn.test().
  • Python: scipy.stats.kruskal(a, b, c), plus scikit_posthocs.posthoc_dunn.
  • Analytika: ask whether the outcome differs across groups. If the checks rule out ANOVA, it runs Kruskal-Wallis with Dunn’s test.

References

  • Kruskal, W. H., & Wallis, W. A. (1952). Use of ranks in one-criterion variance analysis. Journal of the American Statistical Association, 47(260), 583–621.

More in Comparing groups

Run this on your own data

Upload a dataset and ask your question in plain English. Analytika checks the assumptions, picks the test, runs it, and writes the results up in APA format.

Try it free