Meta-analysis in R gives researchers access to the most powerful, flexible, and freely available tools for synthesizing quantitative evidence. The metafor package, created by Wolfgang Viechtbauer, is the gold standard for meta-analysis in R, cited in over 15,000 peer-reviewed publications and supporting more than 40 different effect size measures.
This guide walks through every step of conducting a meta-analysis in R using metafor, from installing packages and preparing data to generating forest plot creation tool, assessing learn about heterogeneity, testing for publication bias, and running sensitivity analyses. Whether you are running your first meta-analysis or transitioning from other software, this tutorial covers the practical workflow with real code examples.
Setting Up R for Meta-Analysis
Before running any analysis, install the required packages. Open R or RStudio and run:
Install metafor (the core package): install.packages("metafor"). This installs metafor along with its dependencies. Load it with library(metafor).
Install meta (optional, for convenience functions): install.packages("meta"). The meta package provides higher-level wrapper functions like metabin() for binary outcomes and metacont() for continuous outcomes that some researchers find more intuitive.
Install dmetar (optional, for advanced diagnostics): This package by Mathias Harrer provides additional functions for outlier detection, influence analysis, and power calculations. Install from GitHub using devtools::install_github("MathiasHarrer/dmetar").
For forest plot customization, the base metafor forest() function is sufficient for most publications. If you need advanced visualizations, the ggplot2 and forestplot packages offer additional options.
Preparing Your Data for Meta-Analysis
Data preparation is the most critical and time-consuming step. Each included study must contribute a standardized effect size and its sampling variance (or standard error) for the analysis.
Common effect size types:
- Log odds ratio (OR): For binary outcomes (event vs. no event). Always use the log-transformed OR for analysis, then back-transform for interpretation
- Standardized mean difference (SMD/Hedges' g): For continuous outcomes measured on different scales across studies. Hedges' g includes a small-sample correction that is preferred over Cohen's d
- Mean difference (MD): For continuous outcomes measured on the same scale across all studies
- Correlation coefficient (r): For association studies. Use Fisher's z transformation during analysis, then back-transform
The escalc() function in metafor calculates effect sizes and sampling variances from summary statistics. For a two-group comparison with binary outcomes:
dat <- escalc(measure="OR", ai=events_treat, bi=nonevents_treat, ci=events_control, di=nonevents_control, data=mydata)
For continuous outcomes using means and standard deviations:
dat <- escalc(measure="SMD", m1i=mean_treat, sd1i=sd_treat, n1i=n_treat, m2i=mean_control, sd2i=sd_control, n2i=n_control, data=mydata)
This eliminates manual calculation errors and automatically computes the sampling variance (vi) needed for the model.
Running the Meta-Analysis Model
The core function in metafor is rma(), which fits meta-analytic models:
Random-effects model (most common): res <- rma(yi, vi, data=dat, method="REML"). The REML (restricted maximum likelihood) estimator is the default and recommended method for estimating between-study variance (tau-squared). Alternatives include DerSimonian-Laird (method="DL"), which is simpler but can underestimate tau-squared with few studies.
Fixed-effect model: res <- rma(yi, vi, data=dat, method="FE"). Use this only when you believe all studies estimate the same underlying true effect, which is rarely justified in practice.
Interpreting the output: The summary() or print() of the rma object reports the pooled effect estimate, its standard error, z-value, p-value, and 95% confidence interval. It also reports tau-squared (between-study variance), I-squared (percentage of total variability due to heterogeneity), and the Q-test for heterogeneity.
Need help choosing the right model or interpreting your results? Our biostatisticians conduct meta-analyses in R every week, from professional data extraction assistance through publication-ready output. get a free quote for your PRISMA compliance and let us handle the statistical analysis while you focus on the clinical interpretation, or explore our full our meta-analysis service services.