Kruskal-Wallis H Test, Explained

After reading this you can decide whether three or more groups differ, using ranks instead of means, and read the H statistic and its p-value with confidence.

What the test does

The Kruskal-Wallis H test compares three or more independent groups to see whether at least one group tends to produce larger or smaller values than the others. It works on ranks, not on the raw numbers, so it does not assume the data follow a normal distribution.

Here is a quick hook. Suppose three machines produce these defect counts per batch: machine A gives 7, 8, 6, 7, machine B gives 9, 10, 8, 9, and machine C gives 12, 11, 13, 12. The means climb from about 7 to 9 to 12. The test asks a precise question: is that separation larger than what random shuffling of 12 numbers into three groups would typically produce? For this data the answer is yes, and the sections below show every number.

Think of it as the rank-based cousin of one-way ANOVA and the many-group extension of the Mann-Whitney U test.

When to use it, and when not

Reach for Kruskal-Wallis when you have one grouping factor with three or more levels, the observations are independent, and the response is ordinal or numeric. It is the sensible choice when your samples are small, skewed, or contain outliers that would distort a mean.

Independence
Each observation belongs to one group only, and observations do not influence each other. Repeated measures on the same subject break this. Use the Wilcoxon signed-rank test for paired data instead.
Same shape
If you want to read the result as a difference in medians, the groups should have similar distribution shapes and spreads. If shapes differ, the test still works, but it tells you the groups differ in some way (location or shape), not specifically in the median.

Do not use it for two groups only (use Mann-Whitney), for paired designs, or when your outcome is a proportion (see the A/B test calculator). If your data are roughly normal with equal variances, ANOVA has slightly more power, but the difference is small.

Kruskal-Wallis is more robust than ANOVA but not free of assumptions. If you plan to interpret medians, check that group spreads are comparable. Levene's test quantifies that.

The formula and the intuition

Pool all observations from every group and rank them from smallest to largest, giving 1 to the smallest value. Tied values share the average of the ranks they would occupy. Then sum the ranks within each group. The statistic compares those rank sums to what you would expect if the groups were interchangeable.

H = \frac{12}{N(N+1)} \sum_{i=1}^{k} \frac{R_i^2}{n_i} - 3(N+1)

Here N is the total number of observations across all groups, k is the number of groups, n_i is the size of group i, and R_i is the sum of ranks in group i. The intuition: if every group had the same average rank, each R_i would sit near n_i(N+1)/2, and H would be near 0. The more the rank sums pull apart, the larger H grows.

Under the null hypothesis, H follows approximately a chi-square distribution with k-1 degrees of freedom, as long as each group has about 5 or more observations. That link lets you turn H into a p-value.

When many values tie, the raw H is slightly too small. Apply the tie correction: divide H by 1 - \frac{\sum (t_j^3 - t_j)}{N^3 - N}, where t_j is the number of values in the jth tie group. With heavy ties this can nudge the p-value below your threshold.

Worked example with the demo data

Three machines, four batches each

Use the demo data: group A 7, 8, 6, 7, group B 9, 10, 8, 9, group C 12, 11, 13, 12. There are N = 12 values in k = 3 groups of n_i = 4.

  1. Rank all 12 values. Sorted, they are 6, 7, 7, 8, 8, 9, 9, 10, 11, 12, 12, 13. The single 6 gets rank 1. The two 7s share ranks 2 and 3, so each gets 2.5. The two 8s share ranks 4 and 5, so each gets 4.5. The two 9s share ranks 6 and 7, so each gets 6.5. The 10 gets rank 8. The 11 gets rank 9. The two 12s share ranks 10 and 11, so each gets 10.5. The 13 gets rank 12.
  2. Sum the ranks per group. Group A holds 6, 7, 7, 8: ranks 1, 2.5, 2.5, 4.5, so R_A = 10.5. Group B holds 9, 10, 8, 9: ranks 6.5, 8, 4.5, 6.5, so R_B = 25.5. Group C holds 12, 11, 13, 12: ranks 10.5, 9, 12, 10.5, so R_C = 42.
  3. Plug into the formula. \frac{12}{12 \cdot 13} = 0.07692. The bracket is \frac{10.5^2}{4} + \frac{25.5^2}{4} + \frac{42^2}{4} = 27.5625 + 162.5625 + 441 = 631.125.
  4. So H = 0.07692 \times 631.125 - 3 \times 13 = 48.548 - 39 = 9.548 before the tie correction.
  5. Correct for ties. There are three tie groups of size 2, giving \sum(t^3 - t) = 3 \times (8 - 2) = 18, and N^3 - N = 1728 - 12 = 1716. The correction factor is 1 - 18/1716 = 0.9895. So H = 9.548 / 0.9895 = 9.649.
  6. With k-1 = 2 degrees of freedom, the chi-square p-value for H = 9.649 is about 0.008.

The p-value sits well below 0.05, so you reject the null hypothesis. At least one machine differs. Group C, with the largest rank sum (42 out of a possible maximum), is the clear driver.

The rank sums separate cleanly: A near the bottom, C near the top. That separation is what drives H upward.

Reading and interpreting the result

A significant H tells you the groups are not all the same, but it does not say which pairs differ. It is an omnibus test, like ANOVA. To find the specific differences, run pairwise Mann-Whitney tests and adjust for multiple comparisons, for example with a Bonferroni correction that divides your alpha by the number of pairs. With three groups there are three pairs, so compare each pairwise p-value to 0.05 / 3 = 0.0167.

Report the statistic, degrees of freedom, and p-value together, for example: H(2) = 9.65, p = 0.008. If shapes are similar, add the group medians so readers see the direction of the effect. You can confirm any p-value from an H value with the p-value calculator using the chi-square option.

A worked slider would let you shift group C's values up and down and watch H change. As C's values rise from overlapping the others to fully above them, H climbs from near 0 (p near 1) toward about 9.6 (p near 0.008) for this four-per-group design.

Common mistakes

Several errors show up again and again.

  • Using it for two groups. With k = 2, Kruskal-Wallis reduces to Mann-Whitney but throws away the direction of the effect. Use Mann-Whitney directly.
  • Ignoring ties. The demo data moved from H = 9.548 to H = 9.649 after correction. With many repeated values the gap is larger.
  • Claiming a median difference when shapes differ. If one group is highly skewed and another is symmetric, a significant H may reflect shape, not location.
  • Skipping the post-hoc step. A significant omnibus result does not identify the guilty group. Follow up with adjusted pairwise tests.
  • Trusting the chi-square approximation with tiny groups. With fewer than about 5 per group, use exact or permutation p-values where available.

Related tools on this site

If your data meet normality and equal-variance assumptions, compare group means with one-way ANOVA, and check those assumptions first with the normality test battery. For two groups only, use the Mann-Whitney U test or, for normal data, the t-test. When your outcome is categorical rather than numeric, the chi-square test of independence and Fisher's exact test apply instead.

Frequently asked questions

Does Kruskal-Wallis test medians?

Not directly. It tests whether groups tend to have larger or smaller values, based on ranks. You can read it as a median comparison only when the groups share a similar distribution shape and spread.

What is a good H value?

There is no single threshold. Compare H to the chi-square distribution with k-1 degrees of freedom. For 3 groups (2 df), H \gt 5.99 gives p below 0.05. The demo data reached H = 9.65, so p was about 0.008.

How many observations do I need per group?

Aim for at least 5 per group so the chi-square approximation holds. Smaller groups still work but call for exact p-values, and the test loses power to detect real differences.

What do I do after a significant result?

Run pairwise comparisons, usually Mann-Whitney tests, and adjust the significance level for the number of comparisons. With 3 groups that means comparing each pair against 0.05 / 3 = 0.0167.

Is Kruskal-Wallis weaker than ANOVA?

Only slightly, and only when the data really are normal. For normal data it retains roughly 95% of ANOVA's power. For skewed data or data with outliers, it is often the stronger choice.