Kruskal–Wallis Test — Definition, Formula & Example
The Kruskal–Wallis test is a way to check whether three or more independent groups tend to produce different values. It works with the ranks of the data instead of the raw numbers. That makes it useful when the data are skewed or contain outliers.
The Kruskal–Wallis H test is a nonparametric test of : all populations have identical distributions, against : at least one population tends to yield larger values than another. All observations are ranked together, and the statistic measures how far each group's mean rank is from the overall mean rank . Under , approximately follows a chi-square distribution with degrees of freedom when each group has at least 5 observations. If the groups' distributions have similar shapes, the test can be read as a comparison of medians.
Key Formula
Where:
- = The Kruskal–Wallis test statistic. It is never negative.
- = Total number of observations across all groups
- = Number of groups being compared
- = Number of observations in group
- = Sum of the ranks in group , where ranks come from the combined data. Tied values get the average of the ranks they span. Whenever ties occur, divide by the correction factor , where is the number of observations in the th set of tied values. Because , the correction makes larger, and it can matter when ties are common.
Worked Example
Problem: A clinic compares recovery times, in days, for 15 patients randomly assigned to three physical-therapy programs, 5 per program. Program A: 2.1, 2.4, 2.8, 3.5, 4.2. Program B: 3.0, 3.3, 4.6, 5.0, 5.7. Program C: 3.9, 5.4, 6.1, 6.8, 7.5. Recovery times are usually right-skewed, and with only 5 patients per program normality is hard to check. At the 0.05 level, is there evidence that recovery times differ among the programs?
Step 1: Rank all 15 values together, from 1 (smallest, 2.1) to 15 (largest, 7.5). Then add the ranks within each group. If the programs made no difference, each rank sum would be close to .
Step 2: Substitute and into the formula.
Step 3: Compare with the chi-square distribution with degrees of freedom. The critical value for is 5.991. The test is always right-tailed, because only large values of count against .
Answer: Reject . With and a p-value of about 0.018, there is convincing evidence that recovery time differs for at least one program. The test does not say which programs differ. Follow-up pairwise comparisons would answer that.
Why It Matters
The Kruskal–Wallis test is the standard backup for one-way ANOVA when its normality condition fails. Medical and psychology researchers use it for skewed measurements like hospital stay length. They also use it for ordinal data like 1-to-10 pain ratings, where a mean is hard to interpret.
Common Mistakes
Mistake: Ranking the values separately within each group instead of ranking all observations together.
Correction: Separate ranking gives every group the same rank sum. In the example, each sum would be 15, and the formula would produce , which is impossible. Rank the combined data once, then split the ranks back into their groups.
