EProportions · Guide test 065
Cochran Q test for matched binary outcomes
Extends McNemar’s comparison to three or more related binary conditions.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- Cochran's Q test (several paired proportions) — Does the share of yes answers differ across several related measurements on the same subjects?
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
Your data
Every subject answers every measurement| Subject | M1 | M2 | M3 |
|---|---|---|---|
| S1 | yes | yes | no |
| S2 | no | no | no |
| S3 | no | yes | no |
| S4 | no | no | no |
| S5 | no | yes | yes |
| S6 | no | no | yes |
| S7 | no | yes | no |
| S8 | no | yes | no |
| S9 | no | yes | yes |
| S10 | yes | yes | no |
| S11 | no | no | no |
| S12 | yes | no | yes |
30 subjects × 3 yes/no measurements · first 12 of 30 rows
If H₀ were true
- The reference: how Q varies across samples when H₀ is true (χ² approximation).
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .104).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- yes count of measurement j
- yes count of subject i
- 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 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
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.
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)
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
- DescTools — DescTools · CochranQTestAuthor documentation/source reviewed: DescTools 0.99.60.search.r-project.org
- CochranQTestsearch.r-project.org
- 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.