Skip to content

EProportions · Guide test 065

Cochran Q test for matched binary outcomes

Extends McNemar’s comparison to three or more related binary conditions.

Runs in CodeSplashPython example validatedR example not run

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 Q test for matched binary outcomes

30
3
0.30
0.45
1.0
0.050

Your data

Every subject answers every measurement
SubjectM1M2M3
S1yesyesno
S2nonono
S3noyesno
S4nonono
S5noyesyes
S6nonoyes
S7noyesno
S8noyesno
S9noyesyes
S10yesyesno
S11nonono
S12yesnoyes

30 subjects × 3 yes/no measurements · first 12 of 30 rows

If H₀ were true

Q = 4.53
  1. The reference: how Q 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 = .104).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

Q=(k−1)(k∑jCj2−N2)kN−∑iRi2Q=\dfrac{(k-1)\left(k\sum_j C_j^2-N^2\right)}{kN-\sum_i R_i^2}

With your numbers

Q=4.53,k−1=2,informative=19Q = 4.53,\qquad k-1 = 2,\qquad \text{informative} = 19
CjC_j
yes count of measurement j
RiR_i
yes count of subject i
NN
total yes count

The verdict

Q(2) = 4.53, p = .104, η²Q = 0.08

p > α = .050: compatible with H₀

Effect size: Serlin's eta squared η²Q = 0.075

McNemar extended to k related yes/no measurements. Only subjects whose answers differ inform Q; with few of them the χ² approximation is rough.

Statistic
4.526
p-value
0.1040
Cochran Q test for matched binary outcomes on 30 subjects × 3 yes/no measurements: Q(2) = 4.53, p = .104, η²Q = 0.08. The p-value is above α = .050, so the data are compatible with H0.

Cochran Q test for matched binary outcomes on 30 subjects × 3 yes/no measurements: Q(2) = 4.53, p = .104, η²Q = 0.08. The p-value is above α = .050, so the data are compatible with H₀.

Question and hypotheses

H0: k related binary treatments have equal success probabilities.

Assumptions

Independent subjects/blocks, complete binary observations under all k conditions; exchangeability for randomization inference.

Formula

Q=(k−1)k∑jCj2−T2kT−∑iRi2Q=(k-1)\frac{k\sum_j C_j^2-T^2}{kT-\sum_i R_i^2}

Parameters and notation

Cj is a treatment success total; Ri a subject success total; T=ΣCj. k=2 reduces to uncorrected McNemar.

Structure of the method

Arrange a complete subject-by-condition binary matrix, compare column success totals, and normalize by within-subject response variation. The resulting quadratic contrast has an approximate chi-square reference with k-1 degrees of freedom and reduces to uncorrected McNemar when k equals two.

Estimated and nuisance parameters

Condition totals, subject totals, and overall successes are empirical summaries. Subject-specific success propensities are nuisance structure removed through the blocked contrast rather than separately estimated random-effect parameters. Subjects with no variation across conditions provide no information about the condition contrast.

Settings and design choices

Fix the set and order of conditions, missing-block policy, omnibus significance level, and planned follow-up family. For a randomization calibration, specify valid within-subject permutations and the number of replicates. Pairwise follow-ups need their own paired tests and multiplicity adjustment.

Fitting and adaptation

New independent subjects can be analyzed by recomputing the blocked totals; accumulating subjects while repeatedly checking the p-value requires sequential control. No fine-tuning is involved. Do not impute missing binary outcomes as failures simply to satisfy the complete-block formula.

Sensitivity and robustness

Few informative subjects, extreme success probabilities, or many conditions can weaken chi-square calibration. Equality of marginal probabilities alone does not automatically justify every label permutation; randomization or a suitable exchangeability model is needed. Distinguish this Q from meta-analysis heterogeneity.

How to use and calibrate

Use χ²k−1 approximately or within subject permutations; follow-ups require multiplicity control.

Example application

Compare three binary diagnostic methods applied to the same people.

Interpretation and pitfalls

Watch out

Subjects with all equal responses supply no within subject contrast. This Cochran Q is unrelated to meta-analysis heterogeneity Q.

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

Rows are matched subjects and columns are binary conditions.

Python · native

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

import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
from statsmodels.stats.contingency_tables import cochrans_q
b=rng.binomial(1,.5,size=(30,3))
print(cochrans_q(b))
Output
CochransQResult(statistic=np.float64(1.1304347826086956), pvalue=np.float64(0.5682366036329846), df=2)
R

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

set.seed(2026)
b <- matrix(rbinom(90,1,.5),ncol=3)
print(DescTools::CochranQTest(b))

Sources

Statistics guide v4 (historical):
Python: Executed successfully in the validation environment · 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: Unavailable local dependencies: statsmodels. Python: Not run in the reference's review: Unavailable local dependencies: statsmodels. No installation attempted. R: Reviewed but not run: R wasn't available to the reference's authors.

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