The metafor Package

A Meta-Analysis Package for R

User Tools

Site Tools


plots:forest_plot_with_subgroups

Table of Contents

Forest Plot with Subgroups

Description

Below is an example of a forest plot with three subgroups. The results of the individual studies are shown grouped together according to their subgroup. Below each subgroup, a summary polygon shows the results when fitting a random-effects model just to the studies within that group. The summary polygon at the bottom of the plot shows the results from a random-effects model when analyzing all 13 studies.

Plot

Code

library(metafor)
 
### copy BCG vaccine meta-analysis data into 'dat'
dat <- dat.bcg
 
### calculate log risk ratios and corresponding sampling variances (and use
### the 'slab' argument to store study labels as part of the data frame)
dat <- escalc(measure="RR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat,
              slab=paste(author, year, sep=", "))
 
### fit random-effects model
res <- rma(yi, vi, data=dat)
 
### set up forest plot (with 2x2 table counts added; the 'rows' argument is
### used to specify in which rows the outcomes will be plotted)
forest(res, xlim=c(-16, 6), at=log(c(0.05, 0.25, 1, 4)), atransf=exp,
       ilab=cbind(dat$tpos, dat$tneg, dat$cpos, dat$cneg),
       ilab.xpos=c(-9.5,-8,-6,-4.5), cex=0.75, ylim=c(-1, 27),
       order=order(dat$alloc), rows=c(3:4,9:15,20:23),
       xlab="Risk Ratio", mlab="", psize=1, header="Author(s) and Year")
 
### add text with Q-value, dfs, p-value, and I^2 statistic
text(-16, -1, pos=4, cex=0.75, bquote(paste("RE Model for All Studies (Q = ",
     .(formatC(res$QE, digits=2, format="f")), ", df = ", .(res$k - res$p),
     ", p = ", .(formatC(res$QEp, digits=2, format="f")), "; ", I^2, " = ",
     .(formatC(res$I2, digits=1, format="f")), "%)")))
 
### fit meta-regression model to test for subgroup differences
res <- rma(yi, vi, mods = ~ alloc, data=dat)
 
### add text for the test of subgroup differences
text(-16, -1.8, pos=4, cex=0.75, bquote(paste("Test for Subgroup Differences: ",
     Q[M], " = ", .(formatC(res$QM, digits=2, format="f")), ", df = ", .(res$p - 1),
     ", p = ", .(formatC(res$QMp, digits=2, format="f")))))
 
### set font expansion factor (as in forest() above) and use bold italic
### font and save original settings in object 'op'
op <- par(cex=0.75, font=4)
 
### add text for the subgroups
text(-16, c(24,16,5), pos=4, c("Systematic Allocation",
                               "Random Allocation",
                               "Alternate Allocation"))
 
### switch to bold font
par(font=2)
 
### add column headings to the plot
text(c(-9.5,-8,-6,-4.5), 26, c("TB+", "TB-", "TB+", "TB-"))
text(c(-8.75,-5.25),     27, c("Vaccinated", "Control"))
 
### set par back to the original settings
par(op)
 
### fit random-effects model in the three subgroups
res.s <- rma(yi, vi, subset=(alloc=="systematic"), data=dat)
res.r <- rma(yi, vi, subset=(alloc=="random"), data=dat)
res.a <- rma(yi, vi, subset=(alloc=="alternate"), data=dat)
 
### add summary polygons for the three subgroups
addpoly(res.s, row=18.5, cex=0.75, atransf=exp, mlab="")
addpoly(res.r, row= 7.5, cex=0.75, atransf=exp, mlab="")
addpoly(res.a, row= 1.5, cex=0.75, atransf=exp, mlab="")
 
### add text with Q-value, dfs, p-value, and I^2 statistic for subgroups
text(-16, 18.5, pos=4, cex=0.75, bquote(paste("RE Model for Subgroup (Q = ",
     .(formatC(res.s$QE, digits=2, format="f")), ", df = ", .(res.s$k - res.s$p),
     ", p = ", .(formatC(res.s$QEp, digits=2, format="f")), "; ", I^2, " = ",
     .(formatC(res.s$I2, digits=1, format="f")), "%)")))
text(-16, 7.5, pos=4, cex=0.75, bquote(paste("RE Model for Subgroup (Q = ",
     .(formatC(res.r$QE, digits=2, format="f")), ", df = ", .(res.r$k - res.r$p),
     ", p = ", .(formatC(res.r$QEp, digits=2, format="f")), "; ", I^2, " = ",
     .(formatC(res.r$I2, digits=1, format="f")), "%)")))
text(-16, 1.5, pos=4, cex=0.75, bquote(paste("RE Model for Subgroup (Q = ",
     .(formatC(res.a$QE, digits=2, format="f")), ", df = ", .(res.a$k - res.a$p),
     ", p = ", .(formatC(res.a$QEp, digits=2, format="f")), "; ", I^2, " = ",
     .(formatC(res.a$I2, digits=1, format="f")), "%)")))
plots/forest_plot_with_subgroups.txt ยท Last modified: 2020/06/03 07:36 by Wolfgang Viechtbauer