Skip to content

BCompare groups · Guide test 024

Kruskal–Wallis H test

Tests equality of independent group distributions using pooled ranks; detects location differences under a shift model.

Runs in CodeSplashPython example validatedR example validated

Do it in CodeSplash

In the app, set it up in Analyze with this method:

  • Kruskal–Wallis test — Do three or more independent groups differ in location (without assuming normality)?

In Kruskal–Wallis test, choose Rank scores = Wilcoxon ranks (Kruskal–Wallis H).

Do it in CodeSplash Run this test on your own data, in Python or R.

Bench

Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.

Bench · Kruskal–Wallis H test

3
10
0.60

Group i has true mean (i − 1) × spread.

1.00
0.050

Your data

Drag a point or use ↑/↓ to change it
Group 1 · n = 10 · mean −0.04Group 2 · n = 10 · mean 0.68Group 3 · n = 10 · mean 1.50

If H₀ were true

H = 5.60
  1. The reference: how H varies across samples when H₀ is true (χ² approximation).
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = .061).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

H=12N(N+1)∑i=1kRi2ni−3(N+1)H = \dfrac{12}{N(N+1)}\sum_{i=1}^{k} \dfrac{R_i^2}{n_i} - 3(N+1)

With your numbers

H=5.60,ν=2,N=30H = 5.60,\qquad \nu = 2,\quad N = 30
RiR_i
the sum of group i's ranks
nin_i
group i's size
NN
the total sample size

The verdict

H(2) = 5.60, p = .061, ε² = 0.19

p > α = .050: compatible with H₀

Effect size: epsilon squared ε² = 0.193

The rank analog of one-way ANOVA, compared with χ² on k − 1 degrees of freedom. Follow a significant H with Dunn's pairwise tests.

Statistic
5.597
p-value
0.0609
Kruskal–Wallis H test on Group 1: n = 10; Group 2: n = 10; Group 3: n = 10: H(2) = 5.60, p = .061, ε² = 0.19. The p-value is above α = .050, so the data are compatible with H0.

Kruskal–Wallis H test on Group 1: n = 10; Group 2: n = 10; Group 3: n = 10: H(2) = 5.60, p = .061, ε² = 0.19. The p-value is above α = .050, so the data are compatible with H₀.

Question and hypotheses

H0: k independent groups have identical distributions.

Assumptions

Independent observations and independent groups; ordinal or quantitative measurements. The permutation null is identical group distributions. A location or median interpretation additionally needs comparable distribution shapes.

Formula

H=12N(N+1)∑jnj[Rˉj−(N+1)/2]2Hc=H/[1−∑t(t3−t)N3−N]\begin{gathered}H=\frac{12}{N(N+1)}\sum_j n_j[\bar R_j-(N+1)/2]^2\\[5pt] H_c=H/\left[1-\frac{\sum_t(t^3-t)}{N^3-N}\right]\end{gathered}

Parameters and notation

N=Σnj; R̄j is the pooled mean rank; each t is a pooled tie group size.

Structure of the method

Assign pooled midranks, compare group average ranks with the pooled rank center, and scale the quadratic sum with the standard tie correction. A chi-square reference is asymptotic; conditional label permutations preserve group sizes and the observed values under the identical-distribution null. Follow-up comparisons require their own specified family.

Estimated and nuisance parameters

Estimate group rank means and pooled tie multiplicities, not group means or a pooled raw-data variance. Group counts and sample sizes determine the null structure. Rank locations summarize distributional ordering; interpreting differences as medians requires a compatible shift-type model and cannot be inferred from the omnibus statistic alone.

Settings and design choices

Choose alpha, exact/Monte Carlo/asymptotic calibration, tie handling, missing-value policy, and post-hoc contrast procedure in advance. The number of permutations affects attainable p-value precision. Dunn, Conover, and pairwise re-ranked procedures use different denominators or ranks and should not be interchanged silently.

Fitting and adaptation

Re-rank the full pooled dataset after changing observations or groups, and recompute tie correction. A permutation analysis must exchange only units exchangeable under the experimental design. For repeated measurements use a blocked method rather than applying Kruskal–Wallis to correlated rows; ranking does not remove that dependence.

Sensitivity and robustness

Tiny groups and heavy ties can degrade the chi-square approximation and limit attainable significance. Distributional spread or shape differences may drive rejection without a simple common median shift. An all-tied dataset has zero rank variance and cannot support the usual statistic. Report group distributions and appropriate effect summaries.

How to use and calibrate

Use χ²k−1 approximately or a group label permutation distribution. Follow a significant omnibus result with justified multiplicity adjusted contrasts.

Example application

Compare ordered severity scores across four independent treatments.

Interpretation and pitfalls

Watch out

A rejection identifies some distributional difference, not which pair differs. Unequal shapes can invalidate a simple median interpretation.

What changed in edition 2

  • New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.

Python and R

Ranks/scores use all observations pooled across independent groups; the omnibus p-value is not a pairwise conclusion.

Python · native

Validated — Executed successfully in the validation environment · needs numpy, scipy

import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
groups = [rng.normal(m,1,20) for m in (0,.4,.8)]
print(stats.kruskal(*groups))
Output
KruskalResult(statistic=np.float64(17.288852459016397), pvalue=np.float64(0.00017610569040312084))
R

Validated — Executed successfully in the validation environment · needs Base/standard library

set.seed(2026)
d <- data.frame(value=c(rnorm(20),rnorm(20,.4),rnorm(20,.8)), group=factor(rep(c("A","B","C"),each=20)))
print(kruskal.test(value~group,data=d))
Output
	Kruskal-Wallis rank sum test

data:  value by group
Kruskal-Wallis chi-squared = 12.972, df = 2, p-value = 0.001525

Sources

Statistics guide v4 (historical):
R: Executed successfully in the validation environment
Reference's record:
Edition 2 review: Python: executed · R: not executed (Rscript unavailable). Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.

From the CodeSplash statistics guide (v4), entry 024, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT024.