Skip to content

Meta-analysis: Fixed & Random Effects

A meta-analysis combines the estimates of several studies into one. The arithmetic is a weighted average. The decisions are what the average estimates when the studies differ, how much they differ, and what the result says about the next study. This page pools the risk ratios of 14 simulated trials with a common-effect and a random-effects model, measures the heterogeneity, builds a prediction interval, looks for small-study effects, and fits a meta-regression on dose.

Download copd_trials.csv into your working directory. The invented table has 14 rows, one per randomized placebo-controlled trial of an oral drug that is meant to prevent exacerbations of chronic obstructive pulmonary disease. Its six columns are trial, dose_mg (the daily dose that the trial tested), events_treatment and n_treatment (patients with at least one moderate or severe exacerbation within 12 months, and patients randomized, in the drug arm), and events_control and n_control for the placebo arm. The trial is the unit of analysis.

The values come from a simulation with known answers. Each trial has its own true risk ratio. The true log risk ratios have a mean of log(0.75) = -0.288 and a standard deviation, tau, of 0.20, so tau squared is 0.040. Part of that variation is a dose effect: each additional mg changes the log risk ratio by -0.012, which multiplies the risk ratio by 0.887 for every 10 mg. The trials have 40 to 400 patients per arm, and the true risk of an exacerbation on placebo was drawn between 0.30 and 0.45 for each trial. No trial was hidden, so there is no publication bias in the file.

A meta-analysis needs one effect estimate per study and the sampling variance of that estimate. For a binary outcome in a trial, the usual choice is the log risk ratio, because its sampling distribution is closer to normal than that of the ratio itself. With a events among n1 treated patients and c events among n2 controls, the log risk ratio is log((a / n1) / (c / n2)) and its variance is 1/a − 1/n1 + 1/c − 1/n2, the square of the standard error on the risk measures page. escalc() in metafor and effectsize_2proportions() in statsmodels compute the same two columns. The table also gives each trial’s share of the weight in the common-effect model of the next section.

No trial here has a zero cell. When one does, the log risk ratio or its variance is not finite. escalc() then adds 1/2 to every cell of that trial’s table by default, and effectsize_2proportions() makes no correction unless zero_correction is set. The constant is a convention, and it changes the estimate of that trial most when events are rare, so state the rule that was used.

library(metafor)
trials <- read.csv("copd_trials.csv")
dir.create("outputs", showWarnings = FALSE)
z <- qnorm(0.975)
risk_treatment <- trials$events_treatment / trials$n_treatment
risk_control <- trials$events_control / trials$n_control
trials$yi <- log(risk_treatment / risk_control)
trials$vi <- 1 / trials$events_treatment - 1 / trials$n_treatment +
1 / trials$events_control - 1 / trials$n_control
weight_share <- (1 / trials$vi) / sum(1 / trials$vi)
package_sizes <- escalc(measure = "RR", ai = events_treatment,
n1i = n_treatment, ci = events_control,
n2i = n_control, data = trials,
var.names = c("yi_package", "vi_package"))
cat(sprintf("Largest difference from escalc(): %.1e\n",
max(abs(package_sizes$yi_package - trials$yi),
abs(package_sizes$vi_package - trials$vi))))
cat(sprintf("%-8s %4s %5s %5s %7s %6s %6s %6s %s\n", "Trial", "Dose",
"n/arm", "Drug", "Placebo", "log RR", "Var", "Weight",
"RR [95% CI]"))
cat(sprintf("%-8s %4d %5d %5.3f %7.3f %6.3f %6.4f %5.1f%% %.2f [%.2f, %.2f]\n",
trials$trial, trials$dose_mg, trials$n_treatment,
risk_treatment, risk_control, trials$yi, trials$vi,
100 * weight_share, exp(trials$yi),
exp(trials$yi - z * sqrt(trials$vi)),
exp(trials$yi + z * sqrt(trials$vi))), sep = "")
Largest difference from escalc(): 2.1e-16
Trial Dose n/arm Drug Placebo log RR Var Weight RR [95% CI]
Trial 01 20 150 0.260 0.333 -0.248 0.0323 4.9% 0.78 [0.55, 1.11]
Trial 02 40 50 0.300 0.440 -0.383 0.0721 2.2% 0.68 [0.40, 1.15]
Trial 03 30 220 0.241 0.395 -0.496 0.0213 7.4% 0.61 [0.46, 0.81]
Trial 04 30 260 0.269 0.412 -0.424 0.0159 9.9% 0.65 [0.51, 0.84]
Trial 05 20 380 0.242 0.363 -0.405 0.0129 12.3% 0.67 [0.53, 0.83]
Trial 06 30 40 0.350 0.450 -0.251 0.0770 2.0% 0.78 [0.45, 1.34]
Trial 07 30 60 0.350 0.383 -0.091 0.0578 2.7% 0.91 [0.57, 1.46]
Trial 08 10 340 0.312 0.400 -0.249 0.0109 14.4% 0.78 [0.64, 0.96]
Trial 09 20 80 0.225 0.463 -0.721 0.0576 2.7% 0.49 [0.30, 0.78]
Trial 10 40 120 0.333 0.300 0.105 0.0361 4.4% 1.11 [0.77, 1.61]
Trial 11 40 400 0.207 0.398 -0.650 0.0133 11.8% 0.52 [0.42, 0.65]
Trial 12 10 180 0.378 0.350 0.076 0.0195 8.1% 1.08 [0.82, 1.42]
Trial 13 20 100 0.200 0.380 -0.642 0.0563 2.8% 0.53 [0.33, 0.84]
Trial 14 10 300 0.373 0.383 -0.026 0.0110 14.4% 0.97 [0.79, 1.20]

The hand calculation and the package agree to machine precision in both languages. Ten of the 14 risk ratios are below 0.8, and three are at 0.97 or above. Seven intervals exclude 1 and seven do not, which is the kind of mixed record that a meta-analysis is asked to settle. The variances run from 0.0109 in Trial 08 to 0.0770 in Trial 06. Trials 08 and 14, with 340 and 300 patients per arm, carry 14.4% of the common-effect weight each, and Trial 06, with 40 per arm, carries 2.0%.

The common-effect model assumes that every trial estimates one true risk ratio, and that the estimates differ by sampling error alone. The best estimate of that one value is the average of the log risk ratios with weights 1/v, the reciprocal of each sampling variance, and the variance of the average is 1 over the sum of the weights. In metafor this is rma(method = "EE"), for equal effects. In statsmodels, combine_effects() returns both the common-effect and the random-effects result in one object, and names the first one the fixed effect.

#' Pool effect estimates with inverse-variance weights.
#'
#' @param yi,vi Effect estimates and their sampling variances.
#' @param tau2 A between-trial variance to add to each sampling variance.
#' @return The pooled estimate and its standard error.
PoolEffects <- function(yi, vi, tau2 = 0) {
weight <- 1 / (vi + tau2)
c(estimate = sum(weight * yi) / sum(weight), se = sqrt(1 / sum(weight)))
}
#' Format a pooled log risk ratio as a risk ratio with its limits.
#'
#' @param label A short name for the model.
#' @param estimate,se The pooled log risk ratio and its standard error.
#' @param critical The quantile that multiplies the standard error.
#' @return One line of text.
RatioLine <- function(label, estimate, se, critical) {
sprintf("%-22s log RR %.4f, SE %.4f, RR %.3f [%.3f, %.3f]\n", label,
estimate, se, exp(estimate), exp(estimate - critical * se),
exp(estimate + critical * se))
}
common <- PoolEffects(trials$yi, trials$vi)
fit_common <- rma(yi, vi, data = trials, method = "EE")
cat(RatioLine("By hand", common[["estimate"]], common[["se"]], z),
RatioLine("rma(method = \"EE\")", fit_common$beta[1], fit_common$se, z),
sep = "")
By hand log RR -0.2999, SE 0.0397, RR 0.741 [0.685, 0.801]
rma(method = "EE") log RR -0.2999, SE 0.0397, RR 0.741 [0.685, 0.801]

The pooled risk ratio is 0.741 with a 95% interval from 0.685 to 0.801, the same by hand and from both packages. As an interval for one common effect, it is valid only if the assumption holds, and in this simulation it does not, since the true effects differ by design. The number still has a meaning. It is the precision-weighted average of the true effects of these 14 trials, and its interval describes the sampling error inside them. It says nothing about the mean of a wider population of trials or about the effect in a fifteenth one, because the model has no term for the differences between trials.

Three statistics describe heterogeneity, and they answer different questions. Cochran’s Q is the weighted sum of squared deviations of the trial estimates from the common-effect estimate. If all trials share one effect, Q follows a chi-square distribution with k − 1 degrees of freedom for k trials, so Q tests that hypothesis.

Tau squared is the variance of the true effects across trials, and tau is their standard deviation, in the units of the log risk ratio. It measures how much the effects differ. The DerSimonian-Laird estimate is (Q − df) / C, where C = sum(w) − sum(w squared) / sum(w), set to zero when Q is below its degrees of freedom.

I squared is (Q − df) / Q. It is the share of the observed variation in the estimates that is not sampling error. It can also be written tau squared / (tau squared + s squared), where s squared = df / C is the typical sampling variance of one trial. The last three lines of the output use that form to ask what I squared would be if every trial were a quarter of its size or four times its size, with tau unchanged.

#' Compute Cochran's Q and the DerSimonian-Laird heterogeneity statistics.
#'
#' @param yi,vi Effect estimates and their sampling variances.
#' @return Q with its degrees of freedom and p-value, tau2, I squared as a
#' fraction, and the typical sampling variance of one trial.
Heterogeneity <- function(yi, vi) {
weight <- 1 / vi
pooled <- sum(weight * yi) / sum(weight)
q <- sum(weight * (yi - pooled)^2)
df <- length(yi) - 1
scaling <- sum(weight) - sum(weight^2) / sum(weight)
c(q = q, df = df, p = pchisq(q, df, lower.tail = FALSE),
tau2 = max(0, (q - df) / scaling), i2 = max(0, (q - df) / q),
typical_vi = df / scaling)
}
het <- Heterogeneity(trials$yi, trials$vi)
cat(sprintf("Q = %.2f on %d df, p = %.4f\n", het[["q"]], het[["df"]],
het[["p"]]))
cat(sprintf("tau^2 = %.4f, tau = %.3f, I^2 = %.1f%%\n", het[["tau2"]],
sqrt(het[["tau2"]]), 100 * het[["i2"]]))
cat(sprintf("rma(): Q = %.2f, I^2 = %.1f%%\n", fit_common$QE,
fit_common$I2))
size_factor <- c(0.25, 1, 4)
i2_by_size <- het[["tau2"]] /
(het[["tau2"]] + het[["typical_vi"]] / size_factor)
cat(sprintf("Trials %4.2f times this size: tau = %.3f, I^2 = %.1f%%\n",
size_factor, sqrt(het[["tau2"]]), 100 * i2_by_size), sep = "")
Q = 37.83 on 13 df, p = 0.0003
tau^2 = 0.0434, tau = 0.208, I^2 = 65.6%
rma(): Q = 37.83, I^2 = 65.6%
Trials 0.25 times this size: tau = 0.208, I^2 = 32.3%
Trials 1.00 times this size: tau = 0.208, I^2 = 65.6%
Trials 4.00 times this size: tau = 0.208, I^2 = 88.4%

Q is 37.83 on 13 degrees of freedom, p = 0.0003, so one shared effect fits these estimates poorly. The estimate of tau squared is 0.0434, and tau is 0.208 against a true 0.20. I squared is 65.6%: about two thirds of the spread of the 14 estimates reflects real differences, and one third is sampling error.

The last three lines show why I squared is not the size of the heterogeneity. Tau is 0.208 in all three. With trials a quarter of this size, the sampling variances are four times larger and I squared is 32.3%. With trials four times this size, it is 88.4%. The effects differ by exactly as much in each case. I squared says how visible the differences are through the sampling error of these particular trials. Tau, on the scale of the effect, says how large they are.

The random-effects model lets each trial have its own true effect, drawn from a distribution with mean mu and variance tau squared. The target is mu, the mean of that distribution. Each estimate now has two sources of variance, so the weights become 1 / (v + tau squared). The between-trial variance is the same for every trial, which flattens the weights and gives the small trials a larger say than they have in the common-effect model.

The estimators of tau squared differ. DerSimonian-Laird is the moment estimator above. Restricted maximum likelihood (REML) is the default of rma(). Paule-Mandel ("PM") iterates until a generalized Q statistic equals its degrees of freedom. combine_effects() offers DerSimonian-Laird ("dl" or "chi2") and Paule-Mandel ("pm" or "iterated", its default), and has no REML estimator, so the REML lines are in the R tab only.

The standard interval treats the estimate of tau squared as if it were the true value. The Knapp-Hartung adjustment replaces the standard error by one that is estimated from the weighted residuals, sqrt(sum(w (y − mu) squared) / ((k − 1) sum(w))), and uses a t distribution with k − 1 degrees of freedom. In metafor it is test = "knha". In statsmodels, conf_int(use_t=True) returns four intervals, and the fourth, which the summary table labels “random effect wls”, is the Knapp-Hartung interval.

k <- nrow(trials)
random_dl <- PoolEffects(trials$yi, trials$vi, het[["tau2"]])
cat(RatioLine("DL by hand", random_dl[["estimate"]], random_dl[["se"]], z))
for (method in c("DL", "REML", "PM")) {
fit <- rma(yi, vi, data = trials, method = method)
cat(RatioLine(sprintf("%s, tau^2 %.4f", method, fit$tau2), fit$beta[1],
fit$se, z))
}
fit_reml <- rma(yi, vi, data = trials, method = "REML")
tau_limits <- confint(fit_reml)$random
cat(sprintf("REML tau^2 = %.4f, 95%% CI [%.4f, %.4f]\n",
tau_limits["tau^2", "estimate"], tau_limits["tau^2", "ci.lb"],
tau_limits["tau^2", "ci.ub"]))
weight_random <- 1 / (trials$vi + het[["tau2"]])
se_knha <- sqrt(sum(weight_random * (trials$yi - random_dl[["estimate"]])^2) /
((k - 1) * sum(weight_random)))
fit_knha <- rma(yi, vi, data = trials, method = "REML", test = "knha")
cat(sprintf("Critical value: normal %.3f, t with %d df %.3f\n", z, k - 1,
qt(0.975, k - 1)))
cat(sprintf("With 5 trials the t value has 4 df: %.3f\n", qt(0.975, 4)))
cat(RatioLine("DL, Knapp-Hartung", random_dl[["estimate"]], se_knha,
qt(0.975, k - 1)),
RatioLine("REML, Knapp-Hartung", fit_knha$beta[1], fit_knha$se,
qt(0.975, k - 1)), sep = "")
DL by hand log RR -0.3066, SE 0.0720, RR 0.736 [0.639, 0.847]
DL, tau^2 0.0434 log RR -0.3066, SE 0.0720, RR 0.736 [0.639, 0.847]
REML, tau^2 0.0432 log RR -0.3066, SE 0.0719, RR 0.736 [0.639, 0.847]
PM, tau^2 0.0403 log RR -0.3063, SE 0.0703, RR 0.736 [0.641, 0.845]
REML tau^2 = 0.0432, 95% CI [0.0101, 0.1501]
Critical value: normal 1.960, t with 13 df 2.160
With 5 trials the t value has 4 df: 2.776
DL, Knapp-Hartung log RR -0.3066, SE 0.0703, RR 0.736 [0.632, 0.857]
REML, Knapp-Hartung log RR -0.3066, SE 0.0703, RR 0.736 [0.632, 0.857]

The random-effects risk ratio is 0.736 with a 95% interval from 0.639 to 0.847. The point estimate is close to the common-effect 0.741, and the standard error on the log scale is 0.0720 against 0.0397, because the model now counts the variation between trials. The true mean is 0.75.

The three estimates of tau squared are 0.0434, 0.0432 and 0.0403, against a true 0.040, and the hand calculation matches rma(method = "DL") and combine_effects(method_re="dl"). The Paule-Mandel values of the two packages agree as well. That closeness to the truth is luck. The 95% interval of the REML estimate runs from 0.0101 to 0.1501, a fifteenfold range, and 14 trials cannot narrow it.

With the Knapp-Hartung adjustment the interval is 0.632 to 0.857, wider than 0.639 to 0.847 at both ends. The adjusted standard error, 0.0703, is slightly smaller than the unadjusted 0.0720 here. The widening comes from the critical value, 2.160 in place of 1.960. With five trials that value would be 2.776, which is why the adjustment matters most in the small meta-analyses where tau squared is least known. The adjusted standard error can also fall well below the unadjusted one when the estimates agree by chance, and test = "adhoc" in metafor prevents that. The hand calculation from the DerSimonian-Laird fit, the REML fit of metafor and the statsmodels interval all give 0.632 to 0.857.

The prediction interval and the forest plot

Section titled “The prediction interval and the forest plot”

The confidence interval is about mu, the mean effect across trials. A clinician who plans to use the drug in a new setting asks a different question: what could the true effect be there? The prediction interval answers it. It combines the uncertainty about mu with the spread of the true effects, mu ± t × sqrt(tau squared + SE squared), where t is the 97.5th percentile of a t distribution with k − 2 degrees of freedom.

predict() in metafor returns a prediction interval with each random-effects fit. Its default uses a normal quantile, or a t quantile with k − 1 degrees of freedom when the fit used test = "knha", and predtype = "Riley" selects k − 2. combine_effects() returns no prediction interval, so the Python tab has the hand calculation only.

A forest plot shows every trial with its interval, the pooled estimate as a diamond whose width is the confidence interval, and the prediction interval beneath it. forest() in metafor draws it from a fitted model, here the REML fit with the Knapp-Hartung interval, and predstyle = "bar" adds the default prediction interval of predict() for that fit. The Python tab builds the figure in Matplotlib from the hand calculations, with squares sized by the random-effects weights.

half_width <- qt(0.975, k - 2) * sqrt(het[["tau2"]] + random_dl[["se"]]^2)
prediction <- exp(random_dl[["estimate"]] + c(-1, 1) * half_width)
cat(sprintf("By hand, DL: RR %.3f, 95%% prediction interval [%.3f, %.3f]\n",
exp(random_dl[["estimate"]]), prediction[1], prediction[2]))
package_prediction <- predict(fit_reml, transf = exp, predtype = "Riley")
cat(sprintf("predict(), REML: RR %.3f, CI [%.3f, %.3f], PI [%.3f, %.3f]\n",
package_prediction$pred, package_prediction$ci.lb,
package_prediction$ci.ub, package_prediction$pi.lb,
package_prediction$pi.ub))
default_prediction <- predict(fit_reml, transf = exp)
knha_prediction <- predict(fit_knha, transf = exp)
cat(sprintf("predict() default: PI [%.3f, %.3f]\n",
default_prediction$pi.lb, default_prediction$pi.ub))
cat(sprintf("predict() default, Knapp-Hartung fit: PI [%.3f, %.3f]\n",
knha_prediction$pi.lb, knha_prediction$pi.ub))
cat(sprintf("True 95%% range of the trial effects [%.3f, %.3f]\n",
0.75 * exp(-z * 0.2), 0.75 * exp(z * 0.2)))
png("outputs/meta-forest-r.png", width = 7.5, height = 5.5, units = "in",
res = 150)
forest(fit_knha, slab = trials$trial, atransf = exp, predstyle = "bar",
at = log(c(0.25, 0.5, 1, 2)), xlab = "Risk ratio (log scale)",
header = c("Trial", "Risk ratio [95% CI]"),
mlab = "Random effects, REML")
invisible(dev.off())
By hand, DL: RR 0.736, 95% prediction interval [0.455, 1.190]
predict(), REML: RR 0.736, CI [0.639, 0.847], PI [0.456, 1.189]
predict() default: PI [0.478, 1.133]
predict() default, Knapp-Hartung fit: PI [0.458, 1.182]
True 95% range of the trial effects [0.507, 1.110]

Forest plot drawn with metafor. Fourteen trials are listed with a square at each risk ratio and a horizontal line for its 95% interval. Most squares lie left of the vertical line at 1, and three lie on it or to its right. Below the trials a diamond marks the pooled risk ratio of 0.74 with an interval from 0.63 to 0.86, and a wide bar marks the prediction interval from 0.46 to 1.18, which crosses 1.

The prediction interval runs from 0.455 to 1.190 by hand and from 0.456 to 1.189 for the REML fit. The confidence interval of the mean, 0.639 to 0.847, lies well inside it. The two intervals answer two questions, and both answers hold: the mean effect across trials is a clear reduction in risk, and the true effect in a single new trial could be anywhere from a halving of the risk to an increase of 19%. A normal distribution with the true mean and tau puts 95% of the true risk ratios between 0.507 and 1.110, inside the prediction interval. The default of predict() with a normal quantile is narrower, 0.478 to 1.133, and for the Knapp-Hartung fit it is 0.458 to 1.182, which is the bar of the metafor figure. Name the method in a report.

In the forest plot the diamond is narrow and sits left of 1, and the bar under it is three times as wide on the log scale and crosses 1. Trials 10, 12 and 14 have risk ratios of 0.97 or above, Trials 09, 11 and 13 are near 0.5, and the bar has to cover both groups.

A report needs one sentence for each interval. For the mean: “Across 14 trials the drug reduced the risk of an exacerbation by about a quarter on average (risk ratio 0.74, 95% CI 0.63 to 0.86).” For the spread: “The effect varied between trials (tau = 0.21 on the log risk ratio scale), and the true risk ratio in a new trial of this kind is expected to lie between 0.46 and 1.19 (95% prediction interval).” The first sentence without the second suggests that every setting will see the average.

If small trials with unimpressive results are less likely to be published, the small trials that are published show larger effects than the large ones. A funnel plot looks for that pattern. It plots each estimate against its standard error, with the most precise trials at the top. Without selective publication the points scatter symmetrically around the pooled estimate, and widen toward the bottom.

Egger’s regression puts a test on the asymmetry. In its original form it regresses the estimate divided by its standard error on the reciprocal of the standard error and tests the intercept. A weighted regression of the estimate on its standard error, with weights 1/v, gives the same test as the slope, and that is the form in the code. In metafor it is regtest(model = "lm").

The fixture holds every trial, so the code then produces the damage of selective publication on purpose. Suppose that a trial with fewer than 150 patients per arm was published only when its result was statistically significant, and that every larger trial was published. The code removes the small trials whose 95% interval includes 1 and pools the rest with the same DerSimonian-Laird model.

trials$se <- sqrt(trials$vi)
egger_fit <- lm(yi ~ se, data = trials, weights = 1 / vi)
egger_row <- summary(egger_fit)$coefficients["se", ]
egger_package <- regtest(fit_reml, model = "lm")
cat(sprintf("Egger by hand: slope %.3f, t = %.3f on %d df, p = %.3f\n",
egger_row[["Estimate"]], egger_row[["t value"]],
egger_fit$df.residual, egger_row[["Pr(>|t|)"]]))
cat(sprintf("regtest(model = \"lm\"): t = %.3f on %d df, p = %.3f\n",
egger_package$zval, egger_package$dfs, egger_package$pval))
outside <- abs(trials$yi - random_dl[["estimate"]]) > z * trials$se
cat(sprintf("Outside the funnel: %s\n",
paste(trials$trial[outside], collapse = ", ")))
png("outputs/meta-funnel-r.png", width = 6, height = 4.5, units = "in",
res = 150)
funnel(fit_reml, atransf = exp, at = log(c(0.4, 0.6, 0.8, 1, 1.25)),
xlab = "Risk ratio (log scale)")
invisible(dev.off())
significant <- abs(trials$yi / trials$se) > z
small <- trials$n_treatment < 150
published <- trials[!(small & !significant), ]
tau2_published <- Heterogeneity(published$yi, published$vi)[["tau2"]]
pooled_published <- PoolEffects(published$yi, published$vi, tau2_published)
egger_published <- summary(
lm(yi ~ se, data = published, weights = 1 / vi)
)$coefficients["se", ]
cat(sprintf("Small trials %d, of which not significant %d\n", sum(small),
sum(small & !significant)))
cat(RatioLine(sprintf("%d published trials", nrow(published)),
pooled_published[["estimate"]], pooled_published[["se"]], z))
cat(sprintf("True RR 0.750, all trials %.3f, published trials %.3f\n",
exp(random_dl[["estimate"]]),
exp(pooled_published[["estimate"]])))
cat(sprintf("Egger test of the published trials: t = %.3f, p = %.3f\n",
egger_published[["t value"]], egger_published[["Pr(>|t|)"]]))
Egger by hand: slope -0.368, t = -0.251 on 12 df, p = 0.806
regtest(model = "lm"): t = -0.251 on 12 df, p = 0.806
Outside the funnel: Trial 10, Trial 11, Trial 12, Trial 14
Small trials 6, of which not significant 4
10 published trials log RR -0.3538, SE 0.0827, RR 0.702 [0.597, 0.826]
True RR 0.750, all trials 0.736, published trials 0.702
Egger test of the published trials: t = -1.016, p = 0.339

Funnel plot drawn with metafor. Fourteen points show the risk ratio of each trial against its standard error, with the standard error increasing downward. A white triangle marks the region where 95% of the points would fall without heterogeneity, around a vertical line at the pooled estimate. The points are spread on both sides of the line at every level of precision, and four lie outside the triangle.

The points of the funnel fall on both sides of the pooled estimate at every level of precision. Egger’s test gives a slope of -0.368 with t = -0.251 on 12 degrees of freedom and p = 0.806, and regtest() returns the same statistic. Nothing here suggests asymmetry, which is the right answer for a file in which no trial was hidden. Four trials lie outside the triangle. The triangle allows for sampling error only, so those points show the heterogeneity measured above and are no sign of bias.

A p-value of 0.806 does not show that publication bias is absent. The test has little power with 14 trials, and an asymmetric funnel has other causes than selective publication. For a risk ratio, the estimate and its standard error are computed from the same counts and are correlated by construction, which can produce asymmetry with no bias at all.

The simulated selection shows the other side. Six trials are small, and four of them were not significant. Without those four, the pooled risk ratio moves from 0.736 to 0.702, further from the true 0.750: the published record claims a 30% reduction in risk where the truth is 25%. The interval, 0.597 to 0.826, still contains 0.750, and a reader of the published trials alone has no way to see the shift. Egger’s test on the ten published trials gives p = 0.339. The selection happened, the estimate moved, and the test did not flag it. A funnel plot and a regression test can support a suspicion of small-study effects, and they cannot clear a meta-analysis of them. Trial registries, which list the trials that were started, are the direct check.

A meta-regression asks whether a trial-level variable explains part of the heterogeneity. It is a weighted regression of the effect estimates on the moderator, with weights 1 / (v + residual tau squared), where the residual tau squared is the between-trial variance that the moderator leaves unexplained. The dose is centered at 25 mg so that the intercept is the log risk ratio at the mean dose.

rma() takes the moderator in mods. The meta-analysis module of statsmodels has no meta-regression function. The Python tab computes the DerSimonian-Laird estimate of the residual tau squared by hand and passes the weights to WLS with the scale fixed at 1, which reproduces rma(method = "DL").

trials$dose_centered <- trials$dose_mg - 25
fit_dose <- rma(yi, vi, mods = ~ dose_centered, data = trials,
method = "REML", test = "knha")
print(round(coef(summary(fit_dose)), 4))
cat(sprintf("REML: residual tau^2 = %.4f, R^2 = %.1f%%\n", fit_dose$tau2,
fit_dose$R2))
fit_dose_dl <- rma(yi, vi, mods = ~ dose_centered, data = trials,
method = "DL")
cat(sprintf("DL: residual tau^2 = %.4f, slope %.4f, SE %.4f\n",
fit_dose_dl$tau2, fit_dose_dl$beta[2], fit_dose_dl$se[2]))
cat(sprintf("REML: RR per 10 mg %.3f [%.3f, %.3f], truth %.3f\n",
exp(10 * fit_dose$beta[2]), exp(10 * fit_dose$ci.lb[2]),
exp(10 * fit_dose$ci.ub[2]), exp(10 * -0.012)))
true_dose_variance <- 0.012^2 * mean(trials$dose_centered^2)
cat(sprintf("Truth: dose explains %.4f of the variance 0.0400, or %.1f%%\n",
true_dose_variance, 100 * true_dose_variance / 0.04))
estimate se tval df pval ci.lb ci.ub
intrcpt -0.3160 0.0687 -4.5968 12 0.0006 -0.4657 -0.1662
dose_centered -0.0082 0.0062 -1.3093 12 0.2149 -0.0218 0.0054
REML: residual tau^2 = 0.0334, R^2 = 22.8%
DL: residual tau^2 = 0.0295, slope -0.0084, SE 0.0058
REML: RR per 10 mg 0.922 [0.804, 1.056], truth 0.887
Truth: dose explains 0.0159 of the variance 0.0400, or 39.9%

The REML fit estimates a slope of -0.0082 per mg with a 95% interval from -0.0218 to 0.0054, and p = 0.2149. The true slope is -0.012. Per 10 mg the risk ratio is 0.922 with an interval from 0.804 to 1.056, against a true 0.887. The estimate has the right sign, its interval contains the truth, and its interval also contains no effect of dose at all. The DerSimonian-Laird fit agrees between metafor and the hand calculation in Python: a residual tau squared of 0.0295, a slope of -0.0084 and a standard error of 0.0058.

The dose effect in this simulation is real, and 14 trials did not detect it. The moderator has four distinct values, the regression has 12 residual degrees of freedom, and the residual tau squared, estimated at 0.0334 against a true 0.024, keeps every trial noisy. The model credits the dose with 22.8% of the heterogeneity, and the true share is 39.9%, the 0.0159 of the 0.0400 that the last line prints. Do not read p = 0.2149 as evidence that dose makes no difference.

A slope across trials is an association between trials. The dose was assigned to these simulated trials at random, so the slope estimates a causal effect of dose here. In a real set of trials the high-dose trials may also be the recent ones, or the ones in sicker patients, and the regression cannot separate those. A moderator that is an average of patient values, such as mean age, has a second problem: a relation between trial averages does not have to hold between patients inside a trial.

Do not choose between the common-effect and the random-effects model by the Q test. Q rejected here with p = 0.0003, but the test has little power when trials are few or small, and a large p-value would not make the true effects equal. Choose from the question. If the trials differ in dose, population and setting, and the result is meant for settings beyond these trials, the random-effects model is the one whose interval answers that question.

Do not read I squared as the size of the heterogeneity. The same tau of 0.208 gave an I squared of 32.3%, 65.6% or 88.4% as the trial size changed. Report tau and the prediction interval, which are on the scale of the effect.

Do not pool studies that estimate different quantities. Every row of this file is a risk ratio for the same outcome over 12 months. An odds ratio and a risk ratio from two otherwise identical trials are different numbers when the outcome is as common as it is here, as the risk measures page shows, and a weighted average of the two estimates nothing in particular. The same holds for different follow-up times and for outcomes that share a name and not a definition.

Do not fit a meta-regression with as many moderators as the trials allow. One moderator with a real effect was not detected in 14 trials. Several candidate moderators, each tried in turn, turn the analysis into a search, and the one that reaches significance is then reported without the others. That is the multiple testing problem with a handful of observations. Name the moderators before the analysis and keep them few.

Do not report the confidence interval of a random-effects model as the range of effects to expect. The interval 0.632 to 0.857 is about the mean. The range for one new trial is 0.455 to 1.190.

Viechtbauer, 2010 describes the metafor package for conducting meta-analyses in R.

DerSimonian and Laird, 1986 describe the random-effects approach to combining trials and the simple noniterative estimator that carries their names.

Higgins and Thompson, 2002 derive I squared as the proportion of the total variation in study estimates that is due to heterogeneity.

Knapp and Hartung, 2003 propose tests for random-effects meta-regression that rest on an improved estimator of the variance of the coefficients.

Higgins, Thompson, and Spiegelhalter, 2009 separate inference on the mean of the random-effects distribution from inference on the whole distribution and propose a simple prediction interval.

Egger and colleagues, 1997 propose the regression test of funnel plot asymmetry and warn that its capacity to detect bias is limited when a meta-analysis holds few small trials.

Thompson and Higgins, 2002 explain how to weight a meta-regression and why its associations are observational, in particular when the covariate is a trial average of patient characteristics.

The metafor website has worked analyses and plots for most models of the package. The documentation of predict() explains the choices behind its prediction interval.

The statsmodels meta-analysis notebook shows combine_effects(), its summary table and its forest plot.

Riley, Higgins, and Deeks, 2011 discuss the interpretation of random-effects meta-analyses for clinical readers.

IntHout, Ioannidis, and Borm, 2014 compare the error rates of the Hartung-Knapp-Sidik-Jonkman method and the DerSimonian-Laird method in simulated meta-analyses of 2 to 20 trials.

Sterne and colleagues, 2011 give recommendations for examining and interpreting funnel plot asymmetry in meta-analyses of randomized trials.

The risk measures page covers the risk ratio and its standard error for one table. The multiple testing page covers the cost of trying several hypotheses on one set of data.