Mathwords logoMathwords

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 H0H_0: all kk populations have identical distributions, against HaH_a: at least one population tends to yield larger values than another. All NN observations are ranked together, and the statistic HH measures how far each group's mean rank is from the overall mean rank N+12\frac{N+1}{2}. Under H0H_0, HH approximately follows a chi-square distribution with k−1k - 1 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

H=12N(N+1)∑i=1kRi2ni−3(N+1)H = \frac{12}{N(N+1)} \sum_{i=1}^{k} \frac{R_i^2}{n_i} - 3(N+1)
Where:
  • HH = The Kruskal–Wallis test statistic. It is never negative.
  • NN = Total number of observations across all groups
  • kk = Number of groups being compared
  • nin_i = Number of observations in group ii
  • RiR_i = Sum of the ranks in group ii, where ranks come from the combined data. Tied values get the average of the ranks they span. Whenever ties occur, divide HH by the correction factor C=1−∑j(tj3−tj)N3−NC = 1 - \frac{\sum_j (t_j^3 - t_j)}{N^3 - N}, where tjt_j is the number of observations in the jjth set of tied values. Because C≤1C \le 1, the correction makes HH 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 5(16)2=40\frac{5(16)}{2} = 40.
RA=1+2+3+6+8=20,RB=4+5+9+10+12=40,RC=7+11+13+14+15=60R_A = 1+2+3+6+8 = 20, \quad R_B = 4+5+9+10+12 = 40, \quad R_C = 7+11+13+14+15 = 60
Step 2: Substitute N=15N = 15 and ni=5n_i = 5 into the formula.
H=1215(16)(2025+4025+6025)−3(16)=112020−48=8H = \frac{12}{15(16)}\left(\frac{20^2}{5} + \frac{40^2}{5} + \frac{60^2}{5}\right) - 3(16) = \frac{1120}{20} - 48 = 8
Step 3: Compare HH with the chi-square distribution with k−1=2k - 1 = 2 degrees of freedom. The critical value for α=0.05\alpha = 0.05 is 5.991. The test is always right-tailed, because only large values of HH count against H0H_0.
H=8>5.991,p-value=P(χ22≥8)≈0.018H = 8 > 5.991, \qquad \text{p-value} = P(\chi^2_2 \ge 8) \approx 0.018
Answer: Reject H0H_0. With H=8H = 8 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 H=−41.25H = -41.25, which is impossible. Rank the combined data once, then split the ranks back into their groups.

Related Terms