如何在R中使用metafor绘制含残差的漏斗图(无调节变量模型)
Got it, let's walk through exactly how to make this happen—from setting up your model to customizing the funnel plot with residuals on the x-axis, especially when you don't have any moderators in your meta-analysis model.
Step 1: Install and Load the metafor Package
First off, make sure you have the package installed and loaded. If you haven't installed it yet, run this:
install.packages("metafor")
Then load it into your R session:
library(metafor)
Step 2: Fit a Model Without Moderators
We'll use a built-in dataset from metafor (dat.bcg) as an example, but you can swap this out with your own data. For a model with no moderators, we'll fit a random-effects model (fixed-effects works similarly here) where we only estimate an overall effect:
# Load example data data(dat.bcg) # Calculate log risk ratios (yi) and corresponding variances (vi) dat <- escalc(measure = "RR", ai = tpos, bi = tneg, ci = cpos, di = cneg, data = dat.bcg) # Fit random-effects model with NO moderators (formula is yi ~ 1) mod <- rma(yi, vi, data = dat, method = "REML")
This model estimates a single overall effect size—no moderators means every study's predicted effect is this overall value.
Step 3: Extract Residuals
The residuals for each study are simply the observed effect size minus the predicted effect size (which is the overall effect from our model, since there are no moderators). You can extract these directly from the model object:
# Extract residuals residuals <- resid(mod) # Alternatively, you can access them via mod$resid
Step 4: Create the Funnel Plot with Residuals on the X-Axis
The default funnel() function in metafor uses effect sizes on the x-axis, but we can override this by specifying the x parameter with our residuals. We'll use standard error (square root of the variance vi) on the y-axis, which is standard for funnel plots:
# Generate the funnel plot funnel(x = residuals, y = sqrt(dat$vi), xlab = "*Residuals*", ylab = "Standard Error", main = "Funnel Plot: Residuals vs. Standard Error (No Moderators)", pch = 19, # Use solid circles for points col = "#2c3e50") # Custom dark blue color # Add a vertical line at residual = 0 (expected mean of residuals) abline(v = 0, lty = 2, col = "gray50")
What's happening here?
- By setting
x = residuals, we replace the default effect size axis with the residual values (each study's deviation from the overall effect). - The y-axis uses standard error, which makes sense because we expect larger residuals (more deviation) in studies with higher uncertainty (larger standard error) if there's no publication bias or unaccounted heterogeneity.
- The dashed line at
v=0is a reference: residuals should cluster around 0 if the model fits well.
Why Use Residuals for the X-Axis (No Moderators)?
When you don't have moderators, all studies share the same predicted effect. Residuals show how much each study's effect differs from this overall estimate. A symmetric funnel plot here suggests that the deviation from the overall effect is random (no publication bias or systematic heterogeneity), while asymmetry might indicate issues like publication bias or unmodeled factors.
内容的提问来源于stack exchange,提问作者NASCH

