Reproducibility is no longer optional in research synthesis. Journal editors, peer reviewers, and systematic review protocols increasingly require that every analytical decision be documented in code that others can audit, re-run, and verify. If you have been running your meta-analysis through a graphical interface, you already understand the statistics. What you need now is the R equivalent, line by line.
Our free tools at Research Gold auto-generate ready-to-run metafor R code the moment you enter your data. This guide explains what every function does, how to customize the output, and how to move from point-and-click to a fully documented, shareable R script.
Why Reproducibility Has Become Non-Negotiable
The replication crisis has made method transparency a gatekeeping criterion at major journals. The PRISMA 2020 statement emphasizes transparency of analytical choices, and many Cochrane protocols mandate that R or Stata code be deposited alongside the final review.
Beyond publication requirements, reproducible code protects you. When a reviewer asks you to exclude a single study and re-run the analysis, a well-commented R script lets you answer in minutes.
Understanding the metafor Package Architecture
metafor, developed by Wolfgang Viechtbauer, is the most widely cited R package for meta-analysis. Understanding its four core functions is the key to reading and customizing auto-generated code.
The rma() Function: Your Model Engine
Every meta-analysis in metafor starts with rma(). A basic call:
library(metafor) res <- rma(yi = effect_size, vi = variance, data = dat, method = "REML") summary(res)
Each argument: yi is your effect size vector. vi is the variance vector (not standard error). data points to your data frame. method specifies the estimator ("REML" is best practice).
To add a moderator for meta-regression: res_mod <- rma(yi, vi, mods = ~ publication_year + sample_size, data = dat, method = "REML")
Try our free Forest Plot Generator to auto-generate this exact code block with your own data.
The forest() Function: Plotting Results
forest(res, slab = dat$study_label, header = "Study", xlab = "Standardized Mean Difference", refline = 0)
Key arguments: slab sets study labels. header adds column headers. xlab labels the x-axis. refline draws the null reference line. order sorts studies. addpred = TRUE displays prediction intervals.
The funnel() Function: Visualizing Publication Bias
funnel(res, xlab = "Standardized Mean Difference", yaxis = "sei", shade = c("white", "gray75", "gray55"))
Generate a configured funnel plot script with our free Funnel Plot Generator.
The metabias() Function: Formal Bias Tests
metabias(res, method = "linreg") for Egger's test. metabias(res, method = "rank") for Begg's rank correlation. metabias(res, method = "peters") for Peters' test.
These tests have low power with fewer than ten studies. Always report the method, statistic, and p-value.
Customizing Auto-Generated Code
The most common customizations: changing the effect measure (add measure = "OR" for odds ratios), running subgroup analysis (separate rma() calls filtered by subgroup), and adding prediction intervals (addpred = TRUE in forest()).
Running a Complete Reproducible Workflow
A complete script follows this structure: load libraries, import data, compute effect sizes with escalc(), fit the model with rma(), run sensitivity checks with leave1out(), generate forest and funnel plots, run metabias(), and if asymmetry is detected, run trimfill().
For the sensitivity step, our Sensitivity Analysis Tool identifies influential studies visually.
Deposit the final script alongside your manuscript as supplementary material. Include the output of sessionInfo() at the end.
Key Takeaways
- rma() fits the meta-analytic model. The method argument controls the heterogeneity estimator, with "REML" as best practice.
- forest() produces the visualization. Customize with slab, order, refline, and addpred.
- funnel() visualizes potential publication bias. The yaxis and shade arguments give you control.
- metabias() provides formal asymmetry tests. Report the method, statistic, and p-value.
- Our free tools auto-generate complete, runnable metafor scripts from your data with one-click copy.
- A reproducible script deposited in a public repository satisfies PRISMA 2020 transparency requirements.
FAQ
What is the difference between rma() and rma.mv() in metafor?
rma() fits models for independent effect sizes, one per study. rma.mv() is the multivariate extension for datasets where studies contribute multiple correlated effect sizes. If your meta-analysis has one effect size per study, use rma().
Which heterogeneity estimator should I use in rma()?
REML is currently recommended. DerSimonian-Laird remains common but consistently underestimates tau-squared in small samples. Paule-Mandel performs well when the number of studies is small.
Can I use metafor for meta-regression?
Yes. Add the mods argument using standard R formula syntax. Meta-regression requires at least ten studies per moderator variable to avoid overfitting.
How do I handle missing variance data in my dataset?
Compute the variance from the confidence interval: vi = ((upper_ci - lower_ci) / (2 * 1.96))^2. Or use escalc() to compute from raw data.
Does auto-generated code from your tools work with all effect size types?
The scripts assume pre-computed effect sizes and variances. If you need to compute from raw data, add an escalc() step before rma().
Need help with your systematic review or meta-analysis? Get a free quote from our team of PhD researchers.