Tutorials › Introduction to Statistics › The Kruskal-Wallis Test for Independent Groups

Nonparametric Alternatives · Tutorial 965 of 996

The Kruskal-Wallis Test for Independent Groups

Learn to rank observations across independent groups, calculate and interpret the Kruskal-Wallis statistic, and check when its approximation is appropriate.

Intermediate 10 min read

What You'll Learn

  • State the null and alternative hypotheses for a Kruskal-Wallis test.
  • Rank observations across groups and calculate each group’s rank sum.
  • Compute the statistic with and without tied observations.
  • Check independence, measurement, and distribution-shape conditions.
  • Use the chi-square approximation and interpret an omnibus p-value.
  • Recognize what a significant or nonsignificant result does—and does not—tell you.

Why Compare Groups by Ranks?

Suppose a researcher wants to compare an outcome across three treatment groups, but the measurements are strongly skewed or contain influential extreme values. The Kruskal-Wallis test offers a way to compare the groups using the ranks of all observations rather than their original numerical distances. It extends the two-independent-group rank-sum approach to three or more groups.

The test is an omnibus procedure: it assesses whether the group distributions are all alike, against the possibility that at least one differs. A significant result does not, by itself, identify which groups differ. Nor does ranking make the test assumption-free; the study design and the meaning of the outcome still matter.

Hypotheses and Conditions

Definition: The Kruskal-Wallis test compares \(k\geq 3\) independent groups on an outcome that can be ranked. Its null hypothesis is that the outcome has the same distribution in every group. Its alternative is that the distributions are not all identical. If the group distributions have similar shapes and spreads, differences can be interpreted more specifically as differences in location.

The test’s conditions concern both how the data were collected and how the statistic will be interpreted:

  • Independent groups: each observation belongs to one group only, and observations are independent within and between groups. Matched or repeated observations do not meet this condition.
  • Rankable outcome: the outcome is at least ordinal, so observations can be ordered. The method can be used for quantitative outcomes and for ordered categories.
  • Comparable observations: the same outcome is measured in the same way across groups. A valid sampling or assignment process is also needed for the intended population or causal conclusions.
  • Distributional interpretation: the formal null concerns identical distributions, not merely equal medians. When distributions have similar shapes and spreads, a difference in location is a reasonable interpretation. With very different shapes or spreads, a rejection may reflect those differences rather than a simple location shift.
  • Approximation: the usual p-value uses a chi-square approximation. It is most dependable when group sizes are not very small; ties are accounted for using a correction described below. With small samples, the approximation can be rough, so a suitable exact procedure may be preferable.

The earlier material on Mann-Whitney and Wilcoxon rank-sum tests uses the same basic idea of comparing pooled ranks for two independent groups. Here the ranking is pooled across all \(k\) groups.

Ranking and Calculating the Statistic

Let group \(i\) contain \(n_i\) observations, and let \(N=\sum_{i=1}^{k}n_i\) be the total sample size. Pool all \(N\) outcomes and rank them from smallest to largest. Assign rank 1 to the smallest and rank \(N\) to the largest. If values tie, assign each tied observation the average of the ranks those observations would have occupied. Let \(R_i\) be the sum of ranks in group \(i\).

Test statistic: With no ties, or before tie correction, the Kruskal-Wallis statistic is $$ H^*=\frac{12}{N(N+1)}\sum_{i=1}^{k}\frac{R_i^2}{n_i}-3(N+1). $$ For tied values, calculate $$ C=1-\frac{\sum_j(t_j^3-t_j)}{N^3-N}, \qquad H=\frac{H^*}{C}, $$ where \(t_j\) is the number of observations in the \(j\)th tied group. If there are no ties, \(C=1\) and \(H=H^*\).

Under the null hypothesis and when the chi-square approximation is appropriate, \(H\) is compared with a chi-square distribution having \(df=k-1\). The p-value is the probability, under that reference distribution, of a statistic at least as large as the observed \(H\). A larger \(H\) indicates more separation among the groups’ ranks.

Why the Formula Measures Rank Separation

The pooled ranks have mean \((N+1)/2\). Group \(i\)’s mean rank is \(R_i/n_i\). The statistic can therefore be written as a scaled sum of squared departures of group mean ranks from the pooled mean:

$$ \sum_{i=1}^{k}n_i\left(\frac{R_i}{n_i}-\frac{N+1}{2}\right)^2 = \sum_{i=1}^{k}\frac{R_i^2}{n_i}-\frac{N(N+1)^2}{4}. $$

To see the equality, expand the square. The cross-products sum to \(-(N+1)\sum_iR_i\), and the final terms sum to \(N(N+1)^2/4\). Since the ranks sum to \(N(N+1)/2\), the expanded expression simplifies to the right-hand side. Multiplying it by \(12/[N(N+1)]\) gives \(H^*\): the last term becomes \(3(N+1)\), which explains the subtraction in the formula. Thus, groups with mean ranks far from the pooled mean produce a larger statistic. Ties reduce the variation of the available ranks, which is why the tie correction divides \(H^*\) by \(C\).

Worked Example: A Rank-Sum Calculation With Ties

Three hypothetical groups each contain four measurements, in arbitrary units:

Group A: 1, 1, 1, 1. Group B: 2, 2, 2, 4. Group C: 3, 3, 5, 6.

There are \(N=12\) observations. Ranking the pooled data assigns rank 2.5 to each value of 1 (ranks 1–4), rank 6 to each value of 2 (ranks 5–7), rank 8.5 to each value of 3 (ranks 8–9), and ranks 10, 11, and 12 to values 4, 5, and 6. The group rank sums are \(R_A=10\), \(R_B=6+6+6+10=28\), and \(R_C=8.5+8.5+11+12=40\). They total 78, as they must: \(12(13)/2=78\).

First calculate the uncorrected statistic:

$$ H^*=\frac{12}{12(13)} \left(\frac{10^2}{4}+\frac{28^2}{4}+\frac{40^2}{4}\right)-39 =\frac{1}{13}(25+196+400)-39 =\frac{621}{13}-39 \approx 8.7692. $$

The tied groups have sizes 4, 3, and 2; the other three observations are untied. Therefore,

$$ C=1-\frac{(4^3-4)+(3^3-3)+(2^3-2)}{12^3-12} =1-\frac{60+24+6}{1716} =1-\frac{90}{1716} \approx 0.94755. $$

The corrected statistic is \(H=8.7692/0.94755\approx9.2546\). With \(k=3\), \(df=2\); the chi-square approximation gives \(p\approx0.00978\). Under that approximation, these data provide evidence against identical group distributions. Each group has only four observations, however, so the chi-square p-value may be rough; the calculation should not obscure that limitation.

Worked Example: No Ties and Little Rank Separation

Suppose three hypothetical independent groups have outcomes that, when pooled and ranked, receive these ranks: Group A has 1, 4, 7, 10, 13; Group B has 2, 5, 8, 11, 14; and Group C has 3, 6, 9, 12, 15. There are no ties, and each group has \(n_i=5\). The rank sums are \(R_A=35\), \(R_B=40\), and \(R_C=45\), with total 120, equal to \(15(16)/2\).

Here \(N=15\), so

$$ H^*=\frac{12}{15(16)} \left(\frac{35^2}{5}+\frac{40^2}{5}+\frac{45^2}{5}\right)-48 =0.05(245+320+405)-48 =0.50. $$

Because there are no ties, \(H=H^*=0.50\). With \(df=2\), the approximate p-value is \(P(\chi^2_2\geq0.50)\approx0.7788\). This result does not provide evidence against identical distributions. It does not establish that the distributions are identical; a test can fail to detect differences, especially when differences are small or sample sizes are limited.

Interpreting the Omnibus Result

The alternative hypothesis says that the distributions are not all identical. It does not say that every group differs from every other group. If the p-value is small relative to the chosen significance level \(\alpha\), reject \(H_0\) in favor of evidence that at least one group distribution differs. If it is not small, do not reject \(H_0\); describe the result as insufficient evidence of a difference, not proof of equality.

The result is about ranks and distributions, not directly about the size of a difference in the original measurement units. Under the additional similar-shape condition, a small p-value can support a location-shift interpretation. Without that condition, avoid reducing the finding to “the medians differ.” The omnibus test also does not specify which groups account for the result; that question requires appropriate follow-up comparisons.

Common Mistakes

  • Using the two-group version for three or more groups. Pool all observations across the \(k\) groups, rank them together, and use the Kruskal-Wallis formula.
  • Forgetting the tie correction. Average the ranks within each tie, then calculate \(C\) and divide \(H^*\) by \(C\). Do not use the uncorrected value as the final statistic when ties are present.
  • Calling the test a test of medians in every setting. The null is equality of distributions. A median or location interpretation needs the groups’ distributions to have similar shapes and spreads.
  • Treating a significant omnibus result as a list of group differences. It indicates evidence that not all distributions are alike; it does not identify the differing groups.
  • Ignoring the design. Ranking does not make repeated measurements independent. The observations must meet the independent-groups condition for this test.
Key takeaway: Pool and rank the observations, sum ranks within each independent group, calculate \(H\) with a tie correction when needed, and compare it with a chi-square reference distribution using \(df=k-1\) when the approximation is suitable. Interpret the result as an omnibus comparison of distributions, with any location interpretation qualified by the shape condition.

Check Your Understanding

Try these questions before moving on.

  1. Three independent groups have sample sizes 8, 10, and 12. What is the total sample size and the chi-square degrees of freedom for the usual Kruskal-Wallis approximation?
  2. Why are tied observations assigned average ranks, and what role does the correction factor \(C\) play?
  3. A Kruskal-Wallis test gives a small p-value. What does that say about the group distributions, and what does it not identify?
  4. When is it reasonable to describe a Kruskal-Wallis result as evidence of a difference in location rather than a more general distributional difference?
  5. Explain why repeated measurements on the same participants do not meet the independent-groups condition.