Skip to content

BCompare groups · Guide test 049

Dunn test

Compares selected groups’ average pooled ranks, typically with a multiple testing adjustment.

Runs in CodeSplashPython (calls R) example not runR example not run

Do it in CodeSplash

In the app, set it up in Analyze with one of these methods:

In Kruskal–Wallis test, choose Pairwise test = Dunn. In Pairwise comparisons (Tukey, Games–Howell, Dunnett, Dunn, Steel–Dwass …), choose Comparison = Dunn.

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 · Dunn test

4
10
0.65

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

1.00
0.050

Your data

Drag a point or use ↑/↓ to change it
Group 1mean 0.49Group 2mean 0.88Group 3mean 1.23Group 4mean 1.73

If H₀ were true

|z| = 2.77
  1. The reference: how |z| varies across samples when H₀ is true, the Bonferroni bound on 6 pairs, |z|.
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = .033).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The whole family

PairDifference in mean rankspp adj.
.2001.000
.092.554
.006.033
.6881.000
.136.814
.2761.000

6 pairs; adjusted by Bonferroni. The highlighted row is the one in the reference pane (the most extreme by default).

The formula

zij=Rˉi−Rˉj(N(N+1)12−∑(t3−t)12(N−1))(1ni+1nj)z_{ij}=\dfrac{\bar R_i-\bar R_j}{\sqrt{\left(\tfrac{N(N+1)}{12}-\tfrac{\sum(t^3-t)}{12(N-1)}\right)\left(\tfrac{1}{n_i}+\tfrac{1}{n_j}\right)}}

With your numbers

z=(−2.77),padj=0.03z = (-2.77),\qquad p_{adj} = 0.03
Rˉi\bar R_i
mean rank of group i in the joint ranking
tt
sizes of tied groups

The verdict

|z| = 2.77, p = .033, ΔR̄ = −14.50

p ≤ α = .050: reject H₀

Effect size: Group 1 − Group 4 (difference in mean ranks) ΔR̄ = −14.500

The usual follow-up to Kruskal–Wallis: joint ranks, normal z per pair, then a p adjustment (Bonferroni here).

Statistic
2.773
p-value
0.0333
Dunn test on Group 1: n = 10; Group 2: n = 10; Group 3: n = 10; Group 4: n = 10: |z| = 2.77, p = .033, ΔR̄ = −14.50. The p-value is below α = .050, so the data are hard to reconcile with H0.

Dunn test on Group 1: n = 10; Group 2: n = 10; Group 3: n = 10; Group 4: n = 10: |z| = 2.77, p = .033, ΔR̄ = −14.50. The p-value is below α = .050, so the data are hard to reconcile with H₀.

Question and hypotheses

H0ij: two groups have equal rank location under the common distribution null.

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

zij=Rˉi−RˉjSR2(1/ni+1/nj)SR2=∑l(Rl−Rˉ)2N−1\begin{gathered}z_{ij}=\frac{\bar R_i-\bar R_j}{\sqrt{S_R^2(1/n_i+1/n_j)}}\\[5pt] S_R^2=\frac{\sum_l(R_l-\bar R)^2}{N-1}\end{gathered}

Parameters and notation

Ri are ranks pooled across all groups; SR²=N(N+1)/12 without ties.

Structure of the method

Rank all groups jointly, compute pairwise differences in group average ranks, and standardize them using the global rank variance with tie adjustment. Normal-tail probabilities are then adjusted over the selected comparison family. This two-stage score-and-adjust structure differs from separately re-ranking every pair for independent Mann–Whitney tests.

Estimated and nuisance parameters

Estimate global pooled rank variance, group rank means, and tie multiplicities. Group sizes determine each contrast's variance. The rank scale depends on all included groups, so a pair's statistic can change when another group is added even before multiplicity adjustment. No common raw-data variance is estimated.

Settings and design choices

Prespecify pairwise or many-to-one family, normal versus justified permutation calibration, alpha, and p-value adjustment. Holm is a common choice with broad dependence validity given valid marginal tails. An omnibus Kruskal–Wallis gate can be part of a protocol but does not substitute for defining the follow-up family.

Fitting and adaptation

Recompute global ranks and all contrasts after any inclusion change. If a group is removed for a defensible design reason, the old pooled-rank contrasts are not the new Dunn analysis. For blocked data, use a blocked rank-comparison method instead of applying independent-group Dunn contrasts to repeated rows.

Sensitivity and robustness

Small groups and heavy ties can weaken the normal approximation. Unequal shape differences complicate a simple median interpretation. A significant adjusted rank contrast describes relative ordering under the stated model, not a difference in original measurement units. Report group distributions or an interpretable effect estimate alongside adjusted p-values.

How to use and calibrate

Use normal tail probabilities then a stated multiplicity adjustment, often Holm or Bonferroni.

Example application

Follow a Kruskal–Wallis comparison with adjusted rank contrasts.

Interpretation and pitfalls

Watch out

Dunn uses global ranks; separate Mann–Whitney tests re-rank each pair and are different.

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

Read the matrix of adjusted p-values. This is an independent-group example; Nemenyi also has a separate Friedman/block version. The first factor level is control for many-to-one procedures.

Python · invokes R

Not run — Python launcher syntax checked; R backend runtime not verified · needs Python standard library + Rscript; R packages: PMCMRplus

# Python launcher for the complete R implementation below.
# Requires Rscript on PATH and the listed R packages.
import shutil
import subprocess
rscript = shutil.which("Rscript")
if rscript is None:
    raise RuntimeError("Install R and make Rscript available on PATH.")
r_code = r"""
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(PMCMRplus::kwAllPairsDunnTest(value~group,data=d,p.adjust.method="holm"))
"""
# No shell interpolation. A failed R execution raises an error in Python.
result = subprocess.run([rscript, "--vanilla", "-"], input=r_code,
                        text=True, capture_output=True)
print(result.stdout)
if result.stderr:
    print(result.stderr)
result.check_returncode()
R

Not run — Syntax checked; runtime not verified in the final environment · needs PMCMRplus

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(PMCMRplus::kwAllPairsDunnTest(value~group,data=d,p.adjust.method="holm"))

Sources

Statistics guide v4 (historical):
Python: Python launcher syntax checked; R backend runtime not verified · R: Syntax checked; runtime not verified in the final environment
Reference's record:
Edition 2 review: Python: skip · R: not executed (Rscript unavailable); why not run: Python: Python-to-R bridge; Rscript is unavailable. R code was not executed. Python: Not run in the reference's review: Python-to-R bridge; Rscript is unavailable. R code was not executed. R: Reviewed but not run: R wasn't available to the reference's authors.

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