Skip to content

BCompare groups · Guide test 028

Jonckheere–Terpstra test

Tests a prespecified ordered trend across independent groups.

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

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 Alternative hypothesis = Increases across the group order or choose Trend test = Jonckheere–Terpstra.

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 · Jonckheere–Terpstra test

4
8
0.35

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

1.00
0.050

Alternative

Your data

Drag a point or use ↑/↓ to change it
Group 1mean 0.17Group 2mean 0.91Group 3mean 0.63Group 4mean 0.68

If H₀ were true

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

The formula

J=∑i<j#{xi⋅<xj⋅},z=J−E[J]Var JJ = \sum_{i<j}\#\{x_{i\cdot} < x_{j\cdot}\},\qquad z = \dfrac{J-E[J]}{\sqrt{\mathrm{Var}\,J}}

With your numbers

J=226,z=1.14J = 226,\quad z = 1.14
JJ
count of pairs in the hypothesized order across all group pairs

The verdict

J = 226.00, z = 1.14, p = .127

p > α = .050: compatible with H₀

Uses the order of the groups, so it beats Kruskal–Wallis when the trend is monotone — and misses umbrella patterns.

Statistic
1.143
p-value
0.1266
Jonckheere–Terpstra test on Group 1: n = 8; Group 2: n = 8; Group 3: n = 8; Group 4: n = 8: J = 226.00, z = 1.14, p = .127. The p-value is above α = .050, so the data are compatible with H0.

Jonckheere–Terpstra test on Group 1: n = 8; Group 2: n = 8; Group 3: n = 8; Group 4: n = 8: J = 226.00, z = 1.14, p = .127. The p-value is above α = .050, so the data are compatible with H₀.

Question and hypotheses

H0: identical group distributions; H1: stochastic locations increase in a prespecified group order.

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. Group ordering must be chosen independently of observed responses.

Formula

J=∑a<b∑i=1na∑j=1nb[I(Xai<Xbj)+12I(Xai=Xbj)]J=\sum_{a<b}\sum_{i=1}^{n_a}\sum_{j=1}^{n_b}[I(X_{ai}<X_{bj})+\tfrac12I(X_{ai}=X_{bj})]

Parameters and notation

Each ordered group pair contributes a Mann–Whitney count; large J supports increasing order.

Structure of the method

Sum pairwise concordance counts over every pair of groups in the prespecified order, assigning half contribution to ties. Large counts support the ordered alternative. Exact or conditional permutation calibration fixes group sizes; normal calibration uses the appropriate null mean and a variance adjusted for pooled ties.

Estimated and nuisance parameters

Estimate the observed ordered concordance pattern from cross-group pairs. Group sizes and tie frequencies enter its null moments. The dose or severity order is a fixed scientific specification, not a fitted sorting of observed means; the test estimates no regression slope and does not require equal numerical spacing of doses.

Settings and design choices

Choose increasing or decreasing order, alpha, tie handling, calibration method, and Monte Carlo budget. If several plausible orderings are tested, account for that search. A trend alternative can have more power than an omnibus rank test when the prespecified ordering is correct, but not for arbitrary nonmonotone differences.

Fitting and adaptation

Refit here means recalculating pairwise counts and null moments after revised observations while keeping the planned order. To adapt to a different dose schedule, state the new ordering before seeing outcomes. Permutations must respect independent experimental units and any randomization strata; unrestricted row shuffling is inappropriate for clustered sampling.

Sensitivity and robustness

Selecting the order from observed group medians invalidates the nominal trend p-value. Many ties or small groups affect discreteness and normal approximation. A significant monotone tendency does not establish that every adjacent dose differs or that the response is linear in numerical dose. Follow-up dose contrasts require separate uncertainty.

How to use and calibrate

Use exact/permutation ranks or a tie corrected normal approximation based on the null mean and variance of J.

Example application

Test an increasing response across prespecified dose levels.

Interpretation and pitfalls

Watch out

Do not choose the order after inspecting means. An omnibus difference without monotonicity may have little power.

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

Groups are ordered A, B, C; declare the trend/umbrella direction before observing outcomes.

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::jonckheereTest(value~group,data=d,alternative="greater"))
"""
# 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::jonckheereTest(value~group,data=d,alternative="greater"))

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 028, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT028.