Tutorials › VEE Mathematical Statistics › A Worked Kruskal–Wallis Test With Three Groups

VEE Mathematical Statistics · Rank-Based and Distribution-Free Tests · Tutorial 862 of 880

A Worked Kruskal–Wallis Test With Three Groups

Learn to carry a three-group Kruskal–Wallis calculation from the raw observations through its corrected statistic and p-value to a properly qualified test conclusion.

Intermediate 12 min read

What You'll Learn

  • Pool observations from three independent groups and assign average ranks to tied values.
  • Calculate each group’s rank sum and the uncorrected Kruskal–Wallis statistic.
  • Apply the tie-correction factor and check the corrected statistic.
  • Use the chi-square reference distribution with the correct degrees of freedom.
  • Interpret a p-value against a stated significance level without overstating the conclusion.

From Three Samples to a Test Conclusion

A Kruskal–Wallis test compares three or more independent groups using the ranks of their pooled observations. The calculation has several stages: rank all observations together, total the ranks within each group, calculate the test statistic, correct it if there are ties, and use a reference distribution to obtain a p-value. Keeping these stages separate makes the arithmetic easier to audit and helps prevent a plausible-looking but incorrect conclusion.

The worked calculations below use three groups of equal size. The test can also be used with unequal group sizes, but the examples here keep the sample sizes balanced so that the ranking and tie-correction steps remain the focus.

Definition: For \(k\) independent groups with sample sizes \(n_1,\ldots,n_k\), let \(N=\sum_{j=1}^k n_j\). Pool the \(N\) observations and assign ranks from 1 to \(N\), using the average of the occupied ranks whenever observations are tied. Let \(R_j\) be the sum of the ranks belonging to group \(j\). The uncorrected Kruskal–Wallis statistic is
$$ H=\frac{12}{N(N+1)}\sum_{j=1}^{k}\frac{R_j^2}{n_j}-3(N+1). $$
If tied blocks have sizes \(t_1,\ldots,t_g\), the tie factor is
$$ C=1-\frac{\sum_{\ell=1}^{g}(t_\ell^3-t_\ell)}{N^3-N}, \qquad H_{\mathrm{corrected}}=\frac{H}{C}. $$
Under the null hypothesis and when the chi-square approximation is appropriate, \(H_{\mathrm{corrected}}\) is compared with a chi-square distribution with \(k-1\) degrees of freedom.

The null hypothesis is that all groups have the same distribution. The alternative is that at least one group’s distribution differs. The test requires independent groups and outcomes that can be ranked; when observations are tied, assign midranks and use the correction above. If the group distributions have similar shapes, differences in location are a useful way to describe what the test detects. A rejection does not by itself identify which groups differ.

A Reliable Calculation Sequence

For a three-group calculation, \(k=3\), so the chi-square reference distribution has \(k-1=2\) degrees of freedom. Use the following sequence, writing down the rank information before moving to the statistic.

1
Pool and rank.
Order all observations together. Give tied observations the average of their rank positions.
2
Find group rank sums.
Add the pooled ranks separately within each group. Check that the group rank sums total \(N(N+1)/2\).
3
Calculate and correct \(H\).
Substitute the rank sums and group sizes into the formula. If there are ties, calculate \(C\) and divide \(H\) by \(C\).
4
Obtain the p-value and conclude.
Use the upper tail of the chi-square distribution with \(k-1\) degrees of freedom, then compare the p-value with the stated significance level.

Why the Reference Distribution Has Two Degrees of Freedom

With three groups, the rank sums cannot vary independently: their total is fixed at \(N(N+1)/2\). Once two group rank sums are known, the third is determined. This leaves \(3-1=2\) independent group rank deviations, which is why the large-sample reference distribution has two degrees of freedom. The chi-square reference is an approximation, so its use depends on the sample size and conditions discussed in the earlier material on Kruskal–Wallis reference distributions.

For two degrees of freedom, the chi-square density at \(x\) is \(\frac{1}{2}e^{-x/2}\) for \(x\geq 0\). Integrating that density above an observed statistic \(h\) gives a convenient p-value formula:

$$ P(\chi^2_2\geq h) =\int_h^\infty \frac{1}{2}e^{-x/2}\,dx =e^{-h/2}. $$

This gives a direct check on three-group p-value calculations. It is still an approximate p-value when the Kruskal–Wallis chi-square approximation is being used.

Worked Example: A Small Tie and a Non-Significant Result

A hypothetical analyst compares a ranked service-time measure from three independent teams, with four observations per team. The observations are A: 4, 7, 9, 12; B: 5, 8, 10, 13; and C: 6, 8, 11, 14. At significance level \(0.05\), carry out the Kruskal–Wallis test using the chi-square approximation.

Method. Pool the 12 observations and assign midranks. The value 8 occurs twice, occupying ranks 5 and 6, so each 8 receives rank \((5+6)/2=5.5\). In increasing order, the observations and ranks are \(4:1,\ 5:2,\ 6:3,\ 7:4,\ 8:5.5,\ 8:5.5,\ 9:7,\ 10:8,\ 11:9,\ 12:10,\ 13:11,\ 14:12\).

The group rank sums are \(R_A=1+4+7+10=22\), \(R_B=2+5.5+8+11=26.5\), and \(R_C=3+5.5+9+12=29.5\). Their total is \(22+26.5+29.5=78\), which agrees with \(12(13)/2=78\).

Here \(N=12\) and all group sizes equal 4. The uncorrected statistic is

$$ H=\frac{12}{12(13)} \left(\frac{22^2}{4}+\frac{26.5^2}{4}+\frac{29.5^2}{4}\right)-3(13) =\frac{1}{13}(121+175.5625+217.5625)-39 =0.5481. $$

There is one tied block of size 2. Thus \(C=1-(2^3-2)/(12^3-12)=1-6/1716=0.9965\), and \(H_{\mathrm{corrected}}=0.5480769/0.9965035=0.5500\). With two degrees of freedom, the approximate p-value is \(e^{-0.5500/2}=e^{-0.2750}=0.7596\).

Conclusion and check. Since \(0.7596>0.05\), do not reject the null hypothesis. These data do not provide sufficient evidence that the three team distributions differ. The rank-sum total of 78 checks the pooled ranking, and the correction increases \(H\) slightly, as expected when ties reduce rank variation. A common mistake is to forget the correction and report \(0.5481\) as the final statistic; the corrected value is \(0.5500\).

Worked Example: Separated Groups With No Ties

In a hypothetical comparison of three independent equipment-monitoring procedures, five readings are recorded for each procedure. The readings are A: 1, 2, 3, 4, 5; B: 6, 7, 8, 9, 10; and C: 11, 12, 13, 14, 15. Test at significance level \(0.05\), using the chi-square approximation.

Method. There are no ties, so the pooled ranks are 1 through 15. Group A receives ranks 1 through 5, group B receives ranks 6 through 10, and group C receives ranks 11 through 15. Their rank sums are \(R_A=1+2+3+4+5=15\), \(R_B=6+7+8+9+10=40\), and \(R_C=11+12+13+14+15=65\). The sum \(15+40+65=120\) matches \(15(16)/2=120\).

With \(N=15\) and \(n_A=n_B=n_C=5\), calculate the statistic:

$$ H=\frac{12}{15(16)} \left(\frac{15^2}{5}+\frac{40^2}{5}+\frac{65^2}{5}\right)-3(16) =\frac{1}{20}(45+320+845)-48 =12.5000. $$

There are no ties, so \(C=1\) and \(H_{\mathrm{corrected}}=12.5000\). With two degrees of freedom, the approximate p-value is \(e^{-12.5/2}=e^{-6.25}=0.0019\), rounded to four decimal places.

Conclusion and check. Since \(0.0019<0.05\), reject the null hypothesis; the data provide evidence that the three distributions are not all the same. The rank sums total 120, and no tie adjustment is needed. A common mistake is to use \(3-1\) for the degrees of freedom but then consult the lower tail; the p-value is the upper-tail probability for a large \(H\).

Worked Example: Several Tied Blocks and a Small p-Value

A hypothetical quality study compares three independent production settings, with five measurements per setting. The observations are A: 1, 2, 2, 3, 4; B: 2, 4, 5, 5, 6; and C: 5, 6, 7, 8, 9. At significance level \(0.01\), perform the test using the chi-square approximation.

Method. Rank the 15 observations together. The ordered values and their ranks are \(1:1,\ 2:3,\ 2:3,\ 2:3,\ 3:5,\ 4:6.5,\ 4:6.5,\ 5:9,\ 5:9,\ 5:9,\ 6:11.5,\ 6:11.5,\ 7:13,\ 8:14,\ 9:15\). For example, the three observations equal to 2 occupy positions 2, 3, and 4, so each receives average rank 3.

The rank sums are \(R_A=1+3+3+5+6.5=18.5\), \(R_B=3+6.5+9+9+11.5=39\), and \(R_C=9+11.5+13+14+15=62.5\). They total \(18.5+39+62.5=120\), as required for 15 pooled ranks.

Before correction, the statistic is

$$ H=\frac{12}{15(16)} \left(\frac{18.5^2}{5}+\frac{39^2}{5}+\frac{62.5^2}{5}\right)-48 =0.05(68.45+304.2+781.25)-48 =9.6950. $$

The tied-block sizes are 3, 2, 3, and 2, corresponding to the values 2, 4, 5, and 6. Their correction contributions sum to \((3^3-3)+(2^3-2)+(3^3-3)+(2^3-2)=24+6+24+6=60\). Therefore \(C=1-60/(15^3-15)=1-60/3360=55/56=0.9821\), and \(H_{\mathrm{corrected}}=9.6950/(55/56)=9.8713\).

With two degrees of freedom, the approximate p-value is \(e^{-9.8713/2}=e^{-4.93565}=0.0072\), rounded to four decimal places.

Conclusion and check. Since \(0.0072<0.01\), reject the null hypothesis at the 1% level. The data provide evidence that the group distributions are not all the same. The ranks total 120, and \(C<1\), so dividing by \(C\) raises the statistic. A common mistake is to divide by the tie contribution itself rather than by the full factor \(C=1-\sum(t^3-t)/(N^3-N)\).

Reading the Result Carefully

A complete report gives the corrected statistic, degrees of freedom, approximate p-value, significance level, and conclusion in context. For example, a concise report might state that the corrected statistic is \(H=9.8713\), with 2 degrees of freedom and approximate \(p=0.0072\); at the 1% significance level, the null hypothesis is rejected. This says that the observed rank pattern would be unlikely under the null model represented by the reference distribution.

The p-value is not the probability that the null hypothesis is true, and a non-significant result does not establish that the group distributions are identical. Also, a significant overall result does not identify the particular groups responsible. Those interpretations require care beyond the arithmetic of the overall test.

Check the calculation before interpreting it: the rank sums must add to \(N(N+1)/2\); the tie factor must be positive and no greater than 1; and the p-value must be the upper-tail probability associated with the corrected statistic. For three groups, use 2 degrees of freedom, not 3.

Check Your Understanding

Work through each rank calculation before checking the final answer.

  1. Three independent groups each contain two observations: A: 2, 5; B: 3, 6; C: 4, 7. Find the group rank sums, \(H\), and the approximate p-value using two degrees of freedom. State the conclusion at level \(0.05\). (Answer: \(R_A=3,\ R_B=7,\ R_C=11;\ H=4.5714;\ p=0.1015\); do not reject at 0.05.)
  2. For groups A: 1, 4; B: 2, 4; C: 3, 5, pooled ranks are \(1,2,3,4.5,4.5,6\). Find the group rank sums and corrected statistic. (Answer: \(R_A=5.5,\ R_B=6.5,\ R_C=9;\ C=34/35;\ H_{\mathrm{corrected}}=0.9559\).)
  3. A three-group test has corrected statistic \(H=6.2\). Using the two-degree-of-freedom approximation, find the p-value and decide at level \(0.05\). (Answer: \(p=e^{-3.1}=0.0450\); reject at 0.05.)
  4. A four-group Kruskal–Wallis test has corrected statistic \(7.81\). Using a chi-square reference distribution, state the degrees of freedom and the approximate decision at level \(0.05\), using the 5% critical value \(7.815\). (Answer: 3 degrees of freedom; \(7.81<7.815\), so do not reject at 0.05.)
  5. There are \(N=10\) pooled observations, with tied-block sizes 3 and 2. Calculate the tie factor. (Answer: \(C=1-[(3^3-3)+(2^3-2)]/(10^3-10)=1-30/990=32/33=0.9697\).)
Clinical Biostats