Tutorials › VEE Mathematical Statistics › Kruskal–Wallis Calculations With Unequal Group Sizes

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

Kruskal–Wallis Calculations With Unequal Group Sizes

Calculate and check Kruskal–Wallis statistics when independent groups have different sample sizes, including the effect of ties.

Intermediate 13 min read

What You'll Learn

  • Calculate pooled ranks and group rank sums for unequal sample sizes.
  • Apply the Kruskal–Wallis formula using each group’s own sample size.
  • Relate group rank sums to group mean ranks and deviations from the pooled mean rank.
  • Apply the tie correction and recognize when the corrected statistic is undefined.
  • Use the chi-square approximation for a global test under its stated conditions.
  • Identify why replacing unequal group sizes with a common denominator gives the wrong result.

Why Unequal Group Sizes Matter

The Kruskal–Wallis test compares the rank distributions of independent groups. Its calculation begins by ranking all observations together, but the groups do not necessarily contribute the same number of observations. Consequently, each group’s rank sum must be scaled by that group’s own size. Substituting a common denominator, or treating each rank sum as if it came from an equally sized group, changes the statistic.

This matters in practical data collection: for example, a hypothetical study might record different numbers of service times at three branches, or different numbers of claim-processing times under three procedures. Unequal group sizes are permitted; the calculation accounts for them directly.

Definition: Suppose there are \(k\) independent groups, with sample sizes \(n_1,\ldots,n_k\), and total sample size \(N=\sum_{j=1}^k n_j\). Pool the \(N\) observations and rank them from 1 to \(N\), assigning average ranks to tied observations. Let \(R_j\) be the sum of the ranks in group \(j\), and let \(\bar R_j=R_j/n_j\) be that group’s mean rank. The Kruskal–Wallis statistic before tie correction is $$ H=\frac{12}{N(N+1)}\sum_{j=1}^k\frac{R_j^2}{n_j}-3(N+1). $$ Each term uses its own \(n_j\). If tied values occur, the tie-corrected statistic is \(H_c=H/C\), where $$ C=1-\frac{\sum_{\ell=1}^g(t_\ell^3-t_\ell)}{N^3-N}. $$ Here \(t_\ell\) is the size of tied set \(\ell\), and \(g\) is the number of tied sets.

The test is applied to independent samples whose observations can be ordered. Under the null hypothesis, the groups have the same distribution. For sufficiently large samples, the reference distribution of \(H\), or \(H_c\) when ties are corrected, is approximately chi-square with \(k-1\) degrees of freedom. The approximation can be poor with very small samples; in that setting, use an appropriate exact procedure if one is available. If all observations are tied, \(C=0\), so the tie-corrected statistic is undefined: the ranks contain no information for distinguishing the groups.

How the Unequal-Size Formula Works

The factor \(R_j^2/n_j\) can be written as \(n_j\bar R_j^2\). Thus a group’s contribution depends both on its mean rank and on how many observations produced that mean rank. A group with twice as many observations is not treated as though its rank sum came from the same sample size as a smaller group.

$$ \frac{R_j^2}{n_j}=n_j\bar R_j^2 $$

A useful justification comes from expressing the statistic in terms of deviations of group mean ranks from the pooled mean rank \((N+1)/2\). The sum of the pooled ranks is \(N(N+1)/2\), so

$$ \begin{aligned} \sum_{j=1}^k n_j\left(\bar R_j-\frac{N+1}{2}\right)^2 &=\sum_{j=1}^k\frac{R_j^2}{n_j} -(N+1)\sum_{j=1}^kR_j +\frac{(N+1)^2}{4}\sum_{j=1}^k n_j\\ &=\sum_{j=1}^k\frac{R_j^2}{n_j} -\frac{N(N+1)^2}{4}. \end{aligned} $$

Multiplying this expression by \(12/[N(N+1)]\) gives exactly \(H\), because the final term becomes \(3(N+1)\). This shows why group size enters the calculation: the statistic aggregates squared departures of group mean ranks from the pooled mean, weighting each departure by the number of observations in that group.

1
Pool and rank.
Rank all observations together. For ties, assign each observation in a tied set the average of the ranks it occupies.
2
Summarize each group.
Compute its size \(n_j\), rank sum \(R_j\), and, if useful, mean rank \(R_j/n_j\).
3
Calculate the uncorrected statistic.
Use \(R_j^2/n_j\) separately for every group, then apply the formula for \(H\).
4
Correct for ties and assess the result.
When ties are present and \(C>0\), compute \(H_c=H/C\). Compare the statistic with the chi-square reference distribution only when its approximation is appropriate.

Worked Calculations

Worked Example: Unequal Groups With No Ties

Method: pool the observations, use each group’s own size in \(R_j^2/n_j\), and compare the resulting statistic with the chi-square approximation. A hypothetical operations study records response times (in minutes) for three independent procedures. The pooled ranks are: group A has ranks 1 and 4; group B has ranks 2, 5, and 8; group C has ranks 3, 6, 7, and 9. There are no ties. At significance level \(\alpha=0.05\), calculate the statistic and approximate p-value. The common mistake to avoid is dividing all squared rank sums by the same sample size.

The sizes are \(n_A=2\), \(n_B=3\), and \(n_C=4\), so \(N=2+3+4=9\). The rank sums are

$$ R_A=1+4=5,\qquad R_B=2+5+8=15,\qquad R_C=3+6+7+9=25. $$

Check the rank total: \(5+15+25=45\), which equals \(9(10)/2=45\), the sum of ranks from 1 to 9. Now use the separate group sizes:

$$ \begin{aligned} H &=\frac{12}{9(10)} \left(\frac{5^2}{2}+\frac{15^2}{3}+\frac{25^2}{4}\right)-3(10)\\ &=\frac{12}{90}(12.5+75+156.25)-30\\ &=\frac{12}{90}(243.75)-30\\ &=32.5-30=2.5. \end{aligned} $$

There are \(k-1=2\) degrees of freedom. For a chi-square variable with 2 degrees of freedom, the upper-tail probability at 2.5 is \(e^{-2.5/2}\approx0.2865\). Thus the approximate p-value is 0.2865, which exceeds 0.05. This calculation does not provide evidence against the null hypothesis at the stated level. As a check, the statistic is nonnegative, and the rank sums add to the required pooled total.

Worked Example: Unequal Groups With Ties

Method: assign average ranks to ties, calculate \(H\) with the actual group sizes, then apply the tie correction. In a hypothetical study, group A has observations 11, 12, and 17; group B has 12, 13, and 18; group C has 14, 14, 19, and 19. Calculate the corrected statistic and its approximate p-value. The mistake to avoid is either ranking ties consecutively instead of averaging their ranks or omitting the correction.

There are \(N=3+3+4=10\) observations. In pooled order, the values and ranks are: 11 gets rank 1; the two 12s get average rank \((2+3)/2=2.5\); 13 gets rank 4; the two 14s get average rank \((5+6)/2=5.5\); 17 gets rank 7; 18 gets rank 8; and the two 19s get average rank \((9+10)/2=9.5\).

The group rank sums are

$$ R_A=1+2.5+7=10.5,\qquad R_B=2.5+4+8=14.5,\qquad R_C=5.5+5.5+9.5+9.5=30. $$

They sum to \(10.5+14.5+30=55\), equal to \(10(11)/2=55\). The uncorrected statistic is

$$ \begin{aligned} H &=\frac{12}{10(11)} \left(\frac{10.5^2}{3}+\frac{14.5^2}{3}+\frac{30^2}{4}\right)-3(11)\\ &=\frac{12}{110}(36.75+70.0833\ldots+225)-33\\ &=\frac{12}{110}(331.8333\ldots)-33\\ &=36.2-33=3.2. \end{aligned} $$

There are three tied sets, each of size 2. Therefore \(\sum(t_\ell^3-t_\ell)=3(2^3-2)=18\), and

$$ C=1-\frac{18}{10^3-10} =1-\frac{18}{990} =\frac{54}{55}\approx0.9818. $$

The corrected statistic is \(H_c=3.2/(54/55)=3.2593\), rounded to four decimal places. With 2 degrees of freedom, the approximate p-value is \(e^{-3.2593/2}\approx0.1959\). The tie correction increases the statistic here, as it does whenever \(0<C<1\) and \(H>0\). The ranks, group sizes, and tie-set sizes have all been checked against the ten observations.

Worked Example: Finding and Correcting a Common-Denominator Error

Method: use the group-specific denominators in the rank-sum formula and compare against the erroneous common-denominator calculation. A hypothetical quality audit has groups of sizes 2, 3, and 4, with pooled ranks allocated as follows: A has ranks 3 and 5; B has ranks 1, 6, and 8; C has ranks 2, 4, 7, and 9. There are no ties. Calculate \(H\). The mistake to avoid is using the average group size, 3, as the denominator for all three terms.

The rank sums are \(R_A=3+5=8\), \(R_B=1+6+8=15\), and \(R_C=2+4+7+9=22\). Their total is \(8+15+22=45\), as required for ranks 1 through 9. The correct computation is

$$ \begin{aligned} H &=\frac{12}{9(10)} \left(\frac{8^2}{2}+\frac{15^2}{3}+\frac{22^2}{4}\right)-30\\ &=\frac{12}{90}(32+75+121)-30\\ &=\frac{12}{90}(228)-30\\ &=30.4-30=0.4. \end{aligned} $$

Using 3 as a common denominator would instead produce

$$ \frac{12}{90}\left(\frac{8^2+15^2+22^2}{3}\right)-30 =\frac{12}{90}\left(\frac{773}{3}\right)-30 \approx4.3556. $$

That value is incorrect: the three groups have sizes 2, 3, and 4, not 3, 3, and 3. With 2 degrees of freedom, the correct statistic has approximate p-value \(e^{-0.4/2}\approx0.8187\). The common-denominator result is not a harmless rounding difference; it alters the statistic because it weights the group rank sums incorrectly.

Checks That Prevent Unequal-Size Errors

Before drawing a conclusion, verify the arithmetic and the structure of the calculation. These checks are especially useful when the group sizes differ:

  • Check the rank total. The group rank sums must add to \(N(N+1)/2\), including when ranks have been averaged for ties.
  • Keep the denominators attached to their groups. The term for group \(j\) is \(R_j^2/n_j\), not \(R_j^2\) divided by a pooled, average, or largest group size.
  • Check tie handling separately. Average ranks are assigned before forming rank sums. The tie correction uses the sizes of tied sets, not the group sample sizes.
  • Check the correction factor. In general, \(0\leq C\leq1\). If every observation is tied, \(C=0\) and \(H_c\) is undefined; otherwise, a positive correction factor can be used.
  • Use the right reference degrees of freedom. The chi-square approximation has \(k-1\) degrees of freedom, regardless of whether the groups have equal sizes.

The statistic is a global comparison of the groups’ ranks. A small p-value indicates evidence that the groups do not all have the same distribution under the test’s assumptions; it does not, by itself, identify which groups differ. Also, a rank-based result should not automatically be described as a difference in medians without additional assumptions about the group distributions.

Key takeaway: Unequal sample sizes are handled directly by the Kruskal–Wallis formula: calculate \(R_j^2/n_j\) using each group’s own \(n_j\). Rank sums should be checked against the pooled rank total, and ties require average ranks and, when \(C>0\), the tie correction.

Check Your Understanding

For each problem, use the group-specific denominators. Give statistics to four decimal places where needed.

  1. Two independent groups have sizes 2 and 4, with pooled rank sums 5 and 16. There are no ties. Calculate \(H\). (Answer: \(H=0.8571\))
  2. Three independent groups have sizes 2, 2, and 4, with rank sums 5, 9, and 22. There are no ties. Calculate \(H\). (Answer: \(H=2\))
  3. Eight observations form three groups. Group A has ranks 1, 2.5, and 4; group B has ranks 2.5 and 8; group C has ranks 5, 6.5, and 6.5. Calculate \(H\), the tie correction \(C\), and \(H_c\). (Answer: \(H=3.3125,\ C=41/42,\ H_c\approx3.3933\))
  4. Three groups have sizes 2, 3, and 5, with rank sums 7, 15, and 33. There are no ties. Calculate \(H\). (Answer: \(H\approx1.6145\))
  5. All observations in a pooled Kruskal–Wallis calculation have the same value. What is \(C\), and can \(H_c\) be calculated? (Answer: \(C=0\); \(H_c\) is undefined.)
Clinical Biostats