Skip to content

BCompare groups · Guide test 280

Knapp–Hartung meta regression tests

Use adjusted uncertainty and t or F reference distributions for meta analytic mean effects or moderators.

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

Do it in CodeSplash

In the app, set it up in Analyze with one of these methods:

In Influence and sensitivity of a meta-analysis, choose Inference = Knapp–Hartung t. In Meta-analysis (pool studies, heterogeneity), choose Inference = Knapp–Hartung t. In Meta-regression (moderators of study effects), choose Inference = Knapp–Hartung t.

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 · Knapp–Hartung test

10
0.30
0.15
0.00

Adds bias × SE: small studies report bigger effects.

0.050

Your data

Simulated with a known ρ

If H₀ were true

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

How the studies combine

Study 1Study 2Study 3Study 4Study 5Study 6Study 7Study 8Study 9Study 10Knapp–HartungWald (z)

Boxes scale with the random-effects weight. The Knapp–Hartung interval rescales the SE by the observed spread q and uses t(k − 1), so it is usually wider than the Wald interval with few studies.

The formula

q=∑iwi(yi−μ^)2k−1,t=μ^q/∑iwi∼tk−1q=\frac{\sum_iw_i(y_i-\hat\mu)^2}{k-1},\qquad t=\frac{\hat\mu}{\sqrt{q/\sum_iw_i}}\sim t_{k-1}

With your numbers

μ^=0.32,q=1.03,SEKH=0.14 (Wald 0.14),t=2.26\hat\mu = 0.32,\quad q = 1.03,\quad \mathrm{SE}_{KH} = 0.14\ (\text{Wald } 0.14),\quad t = 2.26
wi=1/(sei2+τ^2)w_i = 1/(\mathrm{se}_i^2+\hat\tau^2)
random-effects weights with REML τ²
qq
the observed spread relative to the weights; it rescales the SE
tk−1t_{k-1}
the reference, wider than the normal with few studies

The verdict

t(9) = 2.26, p = .050, μ̂ = 0.32

p > α = .050: compatible with H₀

Effect size: pooled effect (REML) μ̂ = 0.316

Holds its level far better than the Wald z with few or heterogeneous studies. With very few studies q can fall below 1 and shrink the interval; the modified version caps it at 1.

Statistic
2.259
p-value
0.0502
Knapp–Hartung test on 10 (x, y) pairs: t(9) = 2.26, p = .050, μ̂ = 0.32. The p-value is above α = .050, so the data are compatible with H0.

Knapp–Hartung test on 10 (x, y) pairs: t(9) = 2.26, p = .050, μ̂ = 0.32. The p-value is above α = .050, so the data are compatible with H₀.

Question and hypotheses

H0: a meta-analytic mean effect or moderator coefficient equals its specified value.

Assumptions

Independent studies; correctly specified meta-regression, suitable within-study variances and heterogeneity estimate; enough studies relative to parameters.

Formula

q=∑iwi(yi−xiTβ^)2k−pV^KH=q(XTWX)−1tj=β^j−βj0(V^KH)jj\begin{gathered}q=\frac{\sum_iw_i(y_i-x_i^T\widehat\beta)^2}{k-p}\\[5pt]\widehat V_{KH}=q(X^TWX)^{-1}\\[5pt] t_j=\frac{\widehat\beta_j-\beta_{j0}}{\sqrt{(\widehat V_{KH})_{jj}}}\end{gathered}

Parameters and notation

wi=1/(vi+τ̂²) in random effects; k studies,p coefficients. Modified variants replace q by max(1,q).

Structure of the method

Fit a weighted random-effects meta-regression, estimate residual dispersion relative to k−p degrees of freedom, and multiply the coefficient covariance by that factor. Individual coefficients use a t reference; a specified coefficient set uses a corresponding joint F test. This modifies inference around a fitted model.

Estimated and nuisance parameters

Estimated quantities include regression coefficients, between-study heterogeneity τ², residual scaling q and their model-based covariance. The user chooses the moderator design and heterogeneity estimator. q is a data-derived uncertainty adjustment, not a neural learning rate or freely optimized regularizer.

Settings and design choices

State original Knapp–Hartung versus a modified form that prevents q below one. Select moderators, tau estimator, contrast and alpha in advance. The baseline test="knha" invokes the package’s stated adjustment; it should not be relabeled as a publication-bias diagnostic or an automatic small-sample guarantee.

Fitting and adaptation

Refit the meta-regression when studies or moderators change, retaining adequate residual degrees of freedom. Assess different scientifically justified tau estimators and influential studies as sensitivity analyses. Selecting moderators to minimize the adjusted p value still requires accounting for selection.

Sensitivity and robustness

Very small k, many moderators, leverage, uncertain within-study variances and heterogeneity near its boundary can make inference unstable. Original q<1 can produce narrower intervals than unadjusted Wald inference; modified procedures behave differently. Report coefficient estimates, confidence intervals, residual degrees of freedom and the exact adjustment variant.

How to use and calibrate

Use tk−p for individual coefficients or the corresponding joint F test; state original versus modified adjustment.

Example application

Test a random-effects meta-analysis mean while accounting better for estimated uncertainty.

Interpretation and pitfalls

Watch out

Very few studies still give fragile inference. Knapp–Hartung is an inference adjustment, not a test for publication bias.

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.

Python · invokes R

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(metafor::rma(yi,vi,mods=~moderator,data=d,method="REML",test="knha"))
"""
# 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 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(metafor::rma(yi,vi,mods=~moderator,data=d,method="REML",test="knha"))
Output
Mixed-Effects Model (k = 12; tau^2 estimator: REML)

tau^2 (estimated amount of residual heterogeneity):     0 (SE = 0.0156)
tau (square root of estimated tau^2 value):             0
I^2 (residual heterogeneity / unaccounted variability): 0.00%
H^2 (unaccounted variability / sampling variability):   1.00
R^2 (amount of heterogeneity accounted for):            0.00%

Test for Residual Heterogeneity:
QE(df = 10) = 5.3726, p-val = 0.8649

Test of Moderators (coefficient 2):
F(df1 = 1, df2 = 10) = 0.2928, p-val = 0.6003

Model Results:

           estimate      se     tval  df    pval    ci.lb   ci.ub    
intrcpt      0.1268  0.0863   1.4697  10  0.1724  -0.0654  0.3190    
moderator   -0.0065  0.0120  -0.5411  10  0.6003  -0.0333  0.0203    

---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

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