metafor: Comprehensive Meta-Analysis in R

repository·master·Indexed 43 days ago

https://github.com/wviechtb/metafor

An R package for conducting meta-analyses, featuring tools for effect size calculation via `escalc()`, fitting fixed, random, and mixed-effects models with `rma()`, and performing meta-regressions. It supports multivariate/multilevel models, network meta-analyses, and specialized visualizations including forest, funnel, radial, L'Abbé, Baujat, bubble, and GOSH plots.

Tokens
1.8K
Snippets
5
Records
7
Agent score
6%

What's inside metafor

  1. Overview of metafor capabilities

    master

    The metafor package is a comprehensive suite for conducting meta-analyses in R. Its core capabilities include:

    • Effect Size Calculation: Functions to calculate various effect sizes or outcome measures.
    • Model Fitting: Support for equal-, fixed-, random-, and mixed-effects models.
    • Regression Analysis: Tools for moderator and meta-regression analyses.
    • Visualization: Creation of specialized meta-analytical plots including forest, funnel, radial, L'Abbé, Baujat, bubble, and GOSH plots.
    • Specialized Data Types: Specialized methods for binomial and person-time data (e.g., Mantel-Haenszel, Peto's method, and generalized linear mixed-effects models like logistic and Poisson regression).
    • Complex Structures: Support for multivariate/multilevel models (accounting for non-independent sampling errors or clustering), network meta-analyses, and models accounting for known correlation structures (e.g., phylogenetic relatedness).
  2. Install the metafor package

    master

    You can install the official stable release of metafor from CRAN or the development version directly from GitHub.

    Install stable version (CRAN)

    Use the standard R install.packages command.

    Install development version (GitHub)

    To install the latest development version, use the remotes package to install from the wviechtb/metafor repository. This builds the package from source.

    # Install stable version
    install.packages("metafor")
    
    # Install development version
    install.packages("remotes")
    remotes::install_github("wviechtb/metafor")
  3. Access documentation and learning resources for metafor

    master

    For users starting with metafor, the following resources are recommended:

    • Core Paper: Viechtbauer, W. (2010). Conducting meta-analyses in R with the metafor package. https://doi.org/10.18637/jss.v036.i03
    • Online Documentation: The full, formatted documentation (including equations and example outputs) is available at https://wviechtb.github.io/metafor/. Note that this site is based on the development version.
    • Key Functions: For standard workflows, study the help pages for:
      • escalc: For calculating effect sizes.
      • rma.uni: For fitting univariate models.
      • rma.mh, rma.peto, rma.glmm, or rma.mv: For specialized model types.
    • Package Website: https://www.metafor-project.org provides news, feature descriptions, analysis examples, and FAQs.
  4. Predict pooled estimates with `predict()`

    master

    After fitting a model with rma(), use predict() to obtain the predicted pooled effect size along with its confidence and prediction intervals. Use the transf argument to back-transform the results (e.g., from log scale to original scale using exp).

    # Predict pooled risk ratio with 95% confidence/prediction intervals
    predict(res, transf=exp, digits=2)
  5. Fit a random-effects meta-analysis model with `rma()`

    master

    The rma() function is used to fit various meta-analysis models. To fit a standard random-effects model, provide the effect size (yi) and sampling variance (vi) columns.

    Key options:

    • test: Specifies the method for testing the pooled estimate and constructing intervals. For example, test="knha" uses the Knapp & Hartung method.
    • mods: Used for meta-regression to include moderators (e.g., mods = ~ moderator_variable).
    • data: The dataframe containing the effect sizes and moderators.
    # Fit a random-effects model using Knapp & Hartung method
    res <- rma(yi, vi, data=dat, test="knha")
    
    # Fit a mixed-effects meta-regression model with a moderator
    res <- rma(yi, vi, mods = ~ ablat, data=dat, test="knha")
  6. Calculate effect sizes with `escalc()`

    master

    Use the escalc() function to transform raw study data (like 2x2 contingency tables) into effect size measures and their corresponding sampling variances. This is a prerequisite for most meta-analysis functions in metafor.

    Common parameters for 2x2 tables:

    • measure: The effect size type (e.g., "RR" for Risk Ratio).
    • ai, bi: Number of positive and negative cases in the treated group.
    • ci, di: Number of positive and negative cases in the control group.
    • slab: Optional study labels.

    The function adds two critical variables to your dataset:

    • yi: The observed effect size (e.g., log risk ratio).
    • vi: The corresponding sampling variance.
    # Example: calculating log risk ratios from a BCG vaccine dataset
    dat <- escalc(measure="RR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg,
                  slab=paste(author, year, sep=", "))
  7. Visualize meta-analysis results with plots

    master

    The metafor package provides several specialized plotting functions to visualize meta-analytic results:

    • forest(): Creates a forest plot to show individual study effects and the pooled estimate. Use atransf for back-transformation and at to specify axis breaks.
    • funnel(): Creates a funnel plot to check for publication bias. Use level to add contour-enhancement lines.
    • regplot(): Creates a bubble plot for meta-regression, showing the relationship between a moderator and the effect size.

    To test for funnel plot asymmetry (publication bias) statistically, use regtest().

    # Forest plot
    forest(res, atransf=exp, at=log(c(0.05, 0.25, 1, 4)), xlim=c(-16, 6),
           ilab=cbind(tpos, tneg, cpos, cneg), ilab.xpos=c(-9.5, -8, -6, -4.5),
           header="Author(s) and Year", ilab.lab=c("TB+", "TB-", "TB+", "TB-"),
           shade="zebra")
    
    # Funnel plot with contour-enhancement
    funnel(res, ylim=c(0, 0.8), level=c(90, 95, 99), refline=0, las=1, legend=TRUE)
    
    # Regression test for funnel plot asymmetry
    regtest(res)
    
    # Bubble plot for meta-regression
    regplot(res, mod="ablat", pi=TRUE, xlab="Absolute Latitude",
            xlim=c(0, 60), predlim=c(0, 60), transf=exp, refline=1,
            legend=TRUE, bty="l", las=1, digits=1)