Skip to content

EProportions · Guide test 069

Cochran–Mantel–Haenszel test

Tests association across stratified contingency tables; its standard binary form adjusts for strata.

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

Do it in CodeSplash

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

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 · Cochran–Mantel–Haenszel test

60
0.60
0.00
0.050

Your data

Edit the counts
EventNo eventTotal
Stratum 1: exposed
16E = 23.8
44E = 36.2
60
Stratum 1: unexposed
13E = 23.8
47E = 36.2
60
Stratum 2: exposed
25E = 23.8
35E = 36.2
60
Stratum 2: unexposed
23E = 23.8
37E = 36.2
60
Stratum 3: exposed
35E = 23.8
25E = 36.2
60
Stratum 3: unexposed
31E = 23.8
29E = 36.2
60
Total143217360

E is the count expected if rows and columns were unrelated.

If H₀ were true

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

The formula

χMH2=(∣∑kak−∑kE[ak]∣−12)2∑kVar(ak)\chi^2_{MH} = \dfrac{\left(\left|\sum_k a_k-\sum_k E[a_k]\right|-\tfrac12\right)^2}{\sum_k\mathrm{Var}(a_k)}

With your numbers

χMH2=0.79\chi^2_{MH} = 0.79
aka_k
exposed events in stratum k
E[ak], Var(ak)E[a_k],\ \mathrm{Var}(a_k)
hypergeometric mean and variance given the margins

The verdict

χ²(1) = 0.79, p = .375, OR_MH = 1.25

p > α = .050: compatible with H₀

Effect size: Mantel–Haenszel common odds ratio OR_MH = 1.251

Combines strata without pooling them, so a confounder that defines the strata cannot create or hide the association. Assumes a common odds ratio — check Breslow–Day.

Statistic
0.788
p-value
0.3746
Cochran–Mantel–Haenszel test on a 6 × 2 table of 360 counts: χ²(1) = 0.79, p = .375, OR_MH = 1.25. The p-value is above α = .050, so the data are compatible with H0.

Cochran–Mantel–Haenszel test on a 6 × 2 table of 360 counts: χ²(1) = 0.79, p = .375, OR_MH = 1.25. The p-value is above α = .050, so the data are compatible with H₀.

Question and hypotheses

H0: no conditional association between two binary variables across strata; general CMH versions test broader association.

Assumptions

Independent strata and units; appropriate stratification. A common odds ratio interpretation needs reasonably homogeneous stratum effects.

Formula

XCMH2=[∑s(as−E0as)]2∑sV0(as)E0as=r1sc1snsV0(as)=r1sr2sc1sc2sns2(ns−1)\begin{gathered}X^2_{CMH}=\frac{[\sum_s(a_s-E_0a_s)]^2}{\sum_sV_0(a_s)}\\[5pt] E_0a_s=\frac{r_{1s}c_{1s}}{n_s}\\[5pt] V_0(a_s)=\frac{r_{1s}r_{2s}c_{1s}c_{2s}}{n_s^2(n_s-1)}\end{gathered}

Parameters and notation

as is the first cell of stratum s; row/column totals define conditional hypergeometric moments.

Structure of the method

For each stratum, compute the observed first cell minus its conditional hypergeometric expectation, sum those deviations, and standardize by the sum of conditional variances. The binary CMH statistic uses a one-degree-of-freedom reference; generalized tables require explicitly defined vector contrasts.

Estimated and nuisance parameters

Stratum margins supply conditional expectations and variances under no association. A common odds-ratio estimate is a separate pooled effect summary, not the same object as the CMH statistic. Homogeneity is relevant to interpreting that single effect even when an aggregate test is calculable.

Settings and design choices

Choose strata before examining outcomes, identify the binary reference categories, and state continuity correction or exact conditional calibration. Extremely fine stratification can create uninformative tables. Do not use an automatic significance screen to decide which confounders to control.

Fitting and adaptation

New strata or observations require recalculation of each stratum contribution. Conditional simulations must preserve margins within strata, and randomization must match the stratified design. Fine-tuning is inapplicable; if effect modification is central, fit or test interactions directly instead of relying only on pooling.

Sensitivity and robustness

Opposite stratum effects can cancel in the summed contrast. Small or degenerate strata may contribute little variance. Clustering within strata is not removed merely by giving clusters a stratum label. Report stratum effects and a heterogeneity assessment alongside a common estimate.

How to use and calibrate

Use χ²1, optionally continuity correction, or exact conditional inference. General tables use a vector quadratic form with suitable scores.

Example application

Compare treatment response controlling for study center.

Interpretation and pitfalls

Watch out

Combining opposite stratum effects can conceal interaction. Stratification alone does not guarantee removal of confounding.

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

Three independent 2×2 strata; maintain the same category ordering in every stratum. Homogeneity tests assess whether a common odds ratio is tenable.

Python · invokes R

Not run — Executed end to end through Rscript · needs Python standard library + Rscript; R packages: base R

# 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"""
tab <- array(c(18,9,12,21,15,7,15,23,20,12,10,18),dim=c(2,2,3))
print(mantelhaen.test(tab))
"""
# 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

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

tab <- array(c(18,9,12,21,15,7,15,23,20,12,10,18),dim=c(2,2,3))
print(mantelhaen.test(tab))
Output
	Mantel-Haenszel chi-squared test with continuity correction

data:  tab
Mantel-Haenszel X-squared = 12.956, df = 1, p-value = 0.0003189
alternative hypothesis: true common odds ratio is not equal to 1
95 percent confidence interval:
 1.747434 6.052957
sample estimates:
common odds ratio 
         3.252252

Sources

Statistics guide v4 (historical):
Python: Executed end to end through Rscript · R: Executed successfully in the validation 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 069, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT069.