Start With the Data Structure
Kruskal–Wallis and runs tests both use ranks or patterns rather than the usual mean-based procedures, but they answer different questions. Kruskal–Wallis compares the distributions of a response across independent groups. A runs test asks whether the order of observations in a sequence shows evidence of clustering or alternation. Choosing correctly starts with asking whether the group labels define separate samples or whether the order of observations is itself meaningful.
For example, comparing hypothetical claim amounts from three independent policy portfolios is a group-comparison problem. Checking whether high and low daily readings in one monitoring sequence tend to occur in clusters is a sequence problem. Treating the first situation as an ordered sequence, or discarding the order in the second, changes the question being tested.
A practical selection rule is to identify the unit of analysis before doing arithmetic. If each observation belongs to one of several independent groups and the response is at least ordinal, consider Kruskal–Wallis. If one sequence has been converted into two categories and the order of those categories matters, consider a runs test. Neither test repairs dependence that contradicts its assumptions.
Kruskal–Wallis: Rank Independent Groups
Suppose there are \(k\) independent samples, with group \(j\) containing \(n_j\) observations, and let \(N=\sum_{j=1}^{k}n_j\). Pool all \(N\) responses and rank them from smallest to largest. When responses tie, assign each tied observation the average of the ranks they would have occupied. Write \(R_j\) for the sum of the ranks in group \(j\).
The null hypothesis is that the group response distributions are the same. The alternative is that at least one group distribution differs. A difference in medians is a reasonable interpretation only when the group distributions have broadly similar shapes and spreads; otherwise, a rejection can reflect other distributional differences. The observations must be independent within and across groups. The test is not a procedure for paired observations or for a single time-ordered sequence.
Worked Example: Comparing Three Independent Samples With a Tie
Three hypothetical independent review teams each score a set of cases, with higher scores indicating a higher rating. Their observations are A: \(4,7,8\), B: \(5,8,10\), and C: \(6,9,11\). At the 5% level, calculate the tie-corrected Kruskal–Wallis statistic and decide whether the chi-square approximation gives evidence against identical group distributions.
Method and assumptions. The response is numerical and the three teams are independent groups, so Kruskal–Wallis is appropriate. There are \(k=3\) groups of size 3, hence \(N=9\). Pooling the values gives the ordered list \(4,5,6,7,8,8,9,10,11\). The two tied values of 8 occupy ranks 5 and 6, so each receives average rank \(5.5\).
Rank sums. Group A has ranks \(1,4,5.5\), so \(R_A=10.5\). Group B has ranks \(2,5.5,8\), so \(R_B=15.5\). Group C has ranks \(3,7,9\), so \(R_C=19\). As a check, the rank sums total \(10.5+15.5+19=45=N(N+1)/2\).
Uncorrected statistic. Substituting the three rank sums and group sizes gives
The squared rank sums are \(110.25\), \(240.25\), and \(361\). Their sum is \(711.5\), and \(711.5/3=237.1667\). Since \(12/90=0.133333\), the product is \(31.6222\), giving \(H=31.6222-30=1.6222\).
Tie correction and decision. There is one tied block of size 2, so
For \(k-1=2\) degrees of freedom, the 5% upper-tail chi-square critical value is approximately \(5.991\). Since \(1.6359<5.991\), this approximation does not give sufficient evidence to reject identical group distributions at the 5% level. The approximate upper-tail probability is \(e^{-1.6359/2}\approx0.4414\). This conclusion concerns the evidence in these data; it does not establish that the distributions are identical.
Common mistake avoided. Using the uncorrected statistic as though there were no ties would ignore the tied rank block. Here the correction is small, but it is still part of the specified calculation.
Runs Tests: Preserve the Order
For a runs test, each observation is assigned one of two symbols, such as A and B. A run is a maximal consecutive block of identical symbols. Thus, the sequence A A B B A has three runs: A A, B B, and A. The test conditions on the number of each symbol and asks whether the observed number of runs is unusually low or high under random ordering.
The exact distribution can be used when the counts are small. The total number of equally likely arrangements is \(\binom{N}{n_1}\). For an even number of runs \(r=2k\), the number of arrangements is \(2\binom{n_1-1}{k-1}\binom{n_2-1}{k-1}\). For an odd number \(r=2k+1\), it is \(\binom{n_1-1}{k}\binom{n_2-1}{k-1}+\binom{n_1-1}{k-1}\binom{n_2-1}{k}\), with impossible binomial terms treated as zero. Divide the relevant count by \(\binom{N}{n_1}\) to obtain an exact probability.
For larger counts, a normal approximation may be used when appropriate. With \(\mu_R=E[R]\) and \(\sigma_R=\sqrt{\operatorname{Var}(R)}\), a continuity-corrected statistic for a lower-tail test is \(z=(R+0.5-\mu_R)/\sigma_R\); for an upper-tail test it is \(z=(R-0.5-\mu_R)/\sigma_R\). The correction places the boundary halfway between possible integer run counts. A two-sided test evaluates departures in either direction according to the stated alternative.
Why the Expected Number of Runs Has This Form
The expectation formula follows by counting adjacent changes. For each of the \(N-1\) neighboring pairs, define an indicator that equals 1 if the pair contains different symbols and 0 otherwise. The number of runs is one plus the number of such changes, because the first symbol starts the first run and each change starts one additional run.
Under random ordering, the first symbol can be type 1 and the next type 2, or vice versa. The probability is \(\frac{n_1}{N}\frac{n_2}{N-1}+\frac{n_2}{N}\frac{n_1}{N-1}=\frac{2n_1n_2}{N(N-1)}\).
There are \(N-1\) adjacencies, so the expected number of changes is \((N-1)\frac{2n_1n_2}{N(N-1)}=\frac{2n_1n_2}{N}\).
Since \(R\) is one plus the number of changes, \(E[R]=1+\frac{2n_1n_2}{N}\).
Worked Example: An Exact Test for Clustering
A hypothetical sensor produces a sequence of nine readings classified as low (A) or high (B). The observed sequence is A A A B B B B A A. Test the one-sided alternative that the readings cluster, using an exact runs test at the 5% level.
Method and hypotheses. The sequence order is meaningful, so a runs test is appropriate. There are \(n_1=5\) A symbols and \(n_2=4\) B symbols. The null hypothesis is random ordering conditional on these counts; the alternative is clustering, corresponding to unusually few runs.
Observed statistic. The sequence consists of A A A, B B B B, and A A, so \(R=3\). There are \(\binom{9}{5}=126\) equally likely arrangements under the null.
Exact lower-tail probability. With counts 5 and 4, exactly two arrangements have 2 runs: all five A symbols followed by all four B symbols, or the reverse. For 3 runs, the sequence can have pattern A–B–A or B–A–B. The first pattern has \(\binom{4}{1}\binom{3}{0}=4\) arrangements, and the second has \(\binom{4}{0}\binom{3}{1}=3\). Thus 7 arrangements have 3 runs. The clustering probability is
Since \(0.0714>0.05\), this exact test does not reject random ordering in favor of clustering at the 5% level. The observed sequence looks clustered, but the exact probability is not small enough for this decision rule.
Common mistake avoided. Counting only arrangements with exactly 3 runs would give \(7/126\), but a lower-tail clustering test requires \(P(R\leq3)\), including the two arrangements with even fewer runs.
Worked Example: A Continuity-Corrected Runs Approximation
A hypothetical inspection log contains 20 A classifications and 18 B classifications in time order. Its observed sequence is A A A A, B B B B, A A A A, B B B B, A A A A, B B B B, A A A A, B B B, A A A A, B B. Test for unusually strong clustering using a lower-tail normal approximation at the 5% level.
Method and observed runs. This is one ordered sequence, not a comparison of independent groups. The displayed sequence has 10 alternating blocks, so \(R=10\), with \(n_1=20\), \(n_2=18\), and \(N=38\). The alternative is fewer runs than expected under random ordering.
Mean and standard deviation. The expected count is
The variance is
Continuity-corrected test. For the lower tail, use \(R+0.5=10.5\):
The lower-tail normal probability is approximately \(0.0009\), which is below \(0.05\). There is evidence of clustering relative to random ordering, under the model and approximation used.
Common mistake avoided. Using \(R=10\) directly as the continuous normal boundary omits the continuity correction. For a lower-tail event \(R\leq10\), the corresponding boundary is 10.5, not 10.
Selection Checks and Exam Traps
The distinction between the tests is more important than any single formula. Use the research question, sampling design, and role of order to guide the choice. These checks help keep the calculations aligned with the question:
- Independent samples with group labels: pool and rank the responses for Kruskal–Wallis. Do not rank each group separately.
- One ordered sequence with two symbols: count runs for the runs test. Do not sort the sequence before counting, because sorting destroys the order under investigation.
- Ties in Kruskal–Wallis: assign average ranks and apply the tie correction. Do not use the correction for a runs test; it is a different statistic with different assumptions.
- Runs-test direction: fewer runs support clustering, while more runs support alternation. State the alternative before choosing a tail.
- Approximation choice: use the exact runs distribution when it is practical for the counts; use a normal approximation only when its conditions are reasonable, with the appropriate continuity correction.
A runs test also requires a meaningful and defensible way to assign the two symbols. For numeric data, the classification rule must be specified, such as above versus below a reference value. The order must represent the sequence of interest, and the random-ordering null must be meaningful for that sequence. A rejection signals nonrandom ordering in this particular runs sense; it does not identify the cause or rule out every other pattern. Likewise, a non-rejection is not proof that the sequence is random.
Check Your Understanding
For each problem, identify the appropriate method before calculating or interpreting.
- Three independent groups have observations A: \(2,4\), B: \(3,5\), and C: \(6,8\). Calculate the uncorrected Kruskal–Wallis statistic. (Answer: \(H=3.7143\).)
- A sequence has counts \(n_A=3\) and \(n_B=2\), and its observed order is A B B A A. Find \(R\) and the exact upper-tail probability \(P(R\geq R_{\mathrm{obs}})\). (Answer: \(R=4\), \(P(R\geq4)=5/10=0.5000\).)
- Three independent groups have observations A: \(1,3\), B: \(2,3\), and C: \(4,5\). Use average ranks and the tie correction to calculate the corrected Kruskal–Wallis statistic. (Answer: \(H=3.5000\), \(C=34/35\), and \(H_{\mathrm{corrected}}\approx3.6029\).)
- A binary sequence has \(n_1=12\), \(n_2=10\), and \(R=8\). Find the lower-tail continuity-corrected normal \(z\)-statistic for a clustering alternative. (Answer: \(\mu_R=11.9091\), \(\sigma_R\approx2.2688\), and \(z\approx-1.502\).)
- A study records one response from each of four independent treatment groups, while a separate monitoring study records high/low indicators in time order. Which test matches each question: comparing treatment-group distributions, and assessing ordering in the monitoring sequence? (Answer: Kruskal–Wallis for the independent treatment groups; a runs test for the ordered high/low sequence.)