BCompare groups · Guide test 279
Cochran Q heterogeneity test
Tests whether effect estimates across studies vary more than their sampling errors would predict.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- Meta-analysis (pool studies, heterogeneity) — What is the average effect across studies, and how much do the studies' true effects differ?
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 heterogeneity test
Adds bias × SE: small studies report bigger effects.
Your data
Simulated with a known ρIf H₀ were true
- The reference: how Q varies across samples when H₀ is true.
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .517).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- study effects and standard errors
- fixed-effect pooled estimate
The verdict
Q(13) = 12.13, p = .517, I² = 0.00
p > α = .050: compatible with H₀
Effect size: share of variation beyond chance I² = 0.000
Low power with few studies, over-sensitive with many: report I² and τ² with intervals rather than the Q test alone.
- Statistic
- 12.125
- p-value
- 0.5174
Cochran Q heterogeneity test on 14 (x, y) pairs: Q(13) = 12.13, p = .517, I² = 0.00. The p-value is above α = .050, so the data are compatible with H₀.
Question and hypotheses
H0: all studies share one true effect.
Assumptions
Independent approximately normal study estimates with correctly estimated sampling variances; sufficiently accurate large sample approximation.
Formula
Parameters and notation
yi is a study effect estimate,vi its variance; df=k−1 without moderators.
Structure of the method
Fit a common-effect weighted mean using inverse within-study variances and sum squared residuals with those same weights. With moderators, the analogous residual heterogeneity statistic projects effects onto a prespecified design matrix. The reference degrees of freedom subtract the number of fitted regression coefficients.
Estimated and nuisance parameters
The common mean or moderator coefficients are estimated nuisance quantities for the heterogeneity test. Study variances are supplied estimates, often treated as known in the large-sample approximation. A random-effects heterogeneity estimate τ² may be fitted for other inference, but ordinary Cochran Q uses within-study inverse-variance weighting.
Settings and design choices
Specify the effect-size scale, dependence handling, covariates and treatment of zero cells before analysis. Distinguish this meta-analytic Q from Cochran’s binary repeated-measures Q. If studies contribute multiple correlated outcomes, a suitable multivariate/cluster framework is needed rather than ordinary independent-study weighting.
Fitting and adaptation
Refit the pooled or moderator model on the eligible study set; neural fine-tuning is not applicable. Examine influence diagnostics and the consequences of alternative defensible effect calculations. The metafor QE output can accompany a REML fit without implying that its Q statistic uses random-effects weights.
Sensitivity and robustness
Q can have little power with few studies and excessive sensitivity to small heterogeneity with many precise studies. Uncertain sampling variances, small-study effect estimators and unmodeled dependence affect calibration. Report heterogeneity magnitude, uncertainty and substantive consequences alongside the p value; nonrejection does not prove identical true effects.
How to use and calibrate
Use χ²k−1 approximately; in meta-regression df becomes k−p for p fitted coefficients.
Example application
Assess heterogeneity before interpreting a pooled effect.
Interpretation and pitfalls
Watch out
Low power with few studies and high power for trivial variation with many studies limit interpretation. This is not binary repeated-measures Cochran 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
yi are study effect estimates on one common scale and vi their sampling variances. Egger/Begg assess small-study asymmetry, not proof of publication bias; power is limited with few studies.
Not run — Executed end to end through Rscript · needs Python standard library + Rscript; R packages: metafor
# 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(yi=rnorm(12,.2,.2),vi=runif(12,.02,.08),moderator=seq_len(12));fit<-metafor::rma(yi,vi,data=d,method="REML")
print(c(Q=fit$QE,df=fit$k-fit$p,p=fit$QEp))
"""
# 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()
Validated — Executed successfully in the validation environment · needs metafor
set.seed(2026)
d<-data.frame(yi=rnorm(12,.2,.2),vi=runif(12,.02,.08),moderator=seq_len(12));fit<-metafor::rma(yi,vi,data=d,method="REML")
print(c(Q=fit$QE,df=fit$k-fit$p,p=fit$QEp))
Output
Q df p 5.5298996 11.0000000 0.9028206
Sources
- metafor meta analysis methodswviechtb.github.io
- rma.uniwviechtb.github.io
- rmasearch.r-project.org
- 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 279, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT279.