Kruskal-Wallis test explained, with an example

Updated

The Kruskal–Wallis test compares three or more independent groups without assuming normal data. It ranks all the values together and asks whether the average rank differs between groups more than chance would allow. It is the rank-based alternative to one-way ANOVA and the extension of the Mann–Whitney test to more than two groups.

When to use it

  • Comparing three or more suppliers, machines, lots or methods on a measurement that is skewed or has outliers.
  • Small groups where normality cannot be checked.
  • Ordinal data, such as visual grades or panel scores, from several groups.
  • If the groups are blocks of matched units (each unit measured under every condition), use the Friedman test instead.

How it works

With k groups of sizes n₁ … nₖ and N values in total:

  • H₀: all k groups come from the same distribution.
  • H₁: at least one group tends to give larger or smaller values than the others.

When the groups have similar shapes, this is a test of equal medians.

Steps:

  1. Rank all N values together, from 1 to N. Tied values get the average of their ranks.
  2. For each group, add up its ranks (Rⱼ) and compute its mean rank R̄ⱼ = Rⱼ/nⱼ. Under H₀, every mean rank should be close to (N + 1)/2.
  3. Compute the H statistic:
H = 12 / (N(N + 1)) · Σ Rⱼ² / nⱼ − 3(N + 1)
  1. With ties, divide H by a correction factor, where t is the size of each group of ties:
H_adj = H / ( 1 − Σ(t³ − t) / (N³ − N) )

Under H₀, H is close to a chi-square distribution with k − 1 degrees of freedom when each group has at least about 5 values. The p-value is the upper tail.

Which groups differ? A significant H says only that the groups are not all alike. To find which pairs differ, a common follow-up is Dunn's test: compare each pair of mean ranks with a z statistic and adjust the p-values for the number of comparisons (Bonferroni here).

Assumptions: independent observations within and between groups; a continuous or ordinal measurement; for the median interpretation, similar shapes in all groups.

Example

An electronics manufacturer qualifies three suppliers of an aluminum electrolytic capacitor. One key test is leakage current after 2 minutes at rated voltage, with an upper limit of 10 µA. Leakage current is typically right-skewed. The engineer tests 8 capacitors from each supplier, in µA:

SupplierLeakage current (µA)Median
A1.8, 2.4, 1.5, 3.1, 2.0, 1.7, 2.6, 5.92.2
B2.2, 3.4, 2.9, 4.8, 2.7, 3.8, 2.4, 3.33.1
C4.1, 6.3, 3.6, 5.2, 9.8, 4.4, 3.9, 5.54.8
Leakage current by capacitor supplier246810Leakage current (µA)Supplier ASupplier BSupplier C
Leakage current by capacitor supplier

Supplier A's 5.9 µA and Supplier C's 9.8 µA are far from the rest of their groups, and an Anderson–Darling test rejects normality for Supplier A (p = 0.016). The Kruskal–Wallis test is a natural choice.

  • Ranking all 24 values (2.4 appears twice and gets rank 6.5 both times): the rank sums are 57.5, 89.5 and 153.0.
  • Mean ranks: A 7.19, B 11.19, C 19.13, against an overall mean rank of 12.5.
  • H = 12/(24 × 25) × (57.5²/8 + 89.5²/8 + 153²/8) − 3 × 25 = 11.81.
  • Ties correction: one pair of ties, so H_adj = 11.81/(1 − 6/13,800) = 11.82.
  • df = 3 − 1 = 2, p-value = 0.0027.

Dunn's pairwise comparisons, Bonferroni-adjusted for 3 comparisons:

PairDifference in mean rankzAdjusted p
A vs B−4.00−1.130.77
A vs C−11.94−3.380.002
B vs C−7.94−2.250.074

How to read the results

  • The p-value of 0.0027 says the suppliers do not all give the same leakage current.
  • The follow-up shows where. Supplier C's capacitors leak clearly more than Supplier A's. Supplier B sits in between; the evidence that it differs from C is suggestive (adjusted p = 0.074) but not conclusive with 8 parts each.
  • In engineering terms, all 24 parts pass the 10 µA limit, but Supplier C's median is more than twice Supplier A's, and its worst part (9.8 µA) is almost at the limit. Higher leakage often signals a thinner or less stable oxide layer, which matters for life at high temperature. Supplier C needs a closer look before approval.
  • The outliers did not drive the result. In ranks, Supplier C's 9.8 µA counts as simply the largest value. An ANOVA would have given the same direction here, but with skewed data like these, its p-value is harder to trust.

Common mistakes

  • Stopping at "the groups differ". Kruskal–Wallis does not say which groups differ. Follow up with adjusted pairwise comparisons.
  • Running many Mann–Whitney tests without adjustment. Three unadjusted tests at 5% each raise the chance of a false alarm well above 5%.
  • Treating it as a test of medians when the shapes differ. If one group is much more spread out, H can be significant with equal medians.
  • Ignoring blocking. If each lot was tested on a different day and day matters, the comparison is confounded; design the test so groups are measured under the same conditions.

Frequently asked questions

What is the difference between Kruskal–Wallis and one-way ANOVA?

ANOVA compares means and assumes normal data with equal variances. Kruskal–Wallis compares rank distributions and needs neither. With normal data ANOVA is slightly more powerful; with skewed data or outliers Kruskal–Wallis is often more reliable.

What does the H statistic mean?

H measures how far the groups' mean ranks are from the overall mean rank, weighted by group size. H = 0 means every group has the same mean rank. Large H means the ranks are concentrated in some groups.

How many values per group do I need?

The chi-square approximation works well with at least about 5 values per group. With smaller groups, exact p-values (from all possible rank arrangements) are more accurate.

What post-hoc test should I use after Kruskal–Wallis?

Dunn's test with a multiple-comparison adjustment is the most common choice. Pairwise Mann–Whitney tests with a Bonferroni adjustment are a simpler alternative.

Try it with your own data

StatLab SPC is free, open-source statistics for engineers. It runs in your browser, works offline, and never uploads your data.

Open StatLab