The metafor Package

A Meta-Analysis Package for R

User Tools

Site Tools


plots:forest_plot_with_subgroups

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
plots:forest_plot_with_subgroups [2020/03/19 13:31] Wolfgang Viechtbauerplots:forest_plot_with_subgroups [2020/06/03 07:36] Wolfgang Viechtbauer
Line 14: Line 14:
 library(metafor) library(metafor)
  
-### decrease margins so the full space is used +### copy BCG vaccine meta-analysis data into 'dat' 
-par(mar=c(4,4,1,2))+dat <- dat.bcg
  
-### fit random-effects model (use slab argument to define study labels) +### calculate log risk ratios and corresponding sampling variances (and use 
-res <- rma(ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg, measure="RR"+### the 'slabargument to store study labels as part of the data frame
-           slab=paste(author, year, sep=", "), method="REML")+dat <- escalc(measure="RR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat, 
 +              slab=paste(author, year, sep=", "))
  
-### set up forest plot (with 2x2 table counts added; rows argument is used +### fit random-effects model 
-### to specify exactly in which rows the outcomes will be plotted)+res <- rma(yi, vi, data=dat) 
 + 
 +### set up forest plot (with 2x2 table counts added; the 'rowsargument 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, forest(res, xlim=c(-16, 6), at=log(c(0.05, 0.25, 1, 4)), atransf=exp,
-       ilab=cbind(dat.bcg$tpos, dat.bcg$tneg, dat.bcg$cpos, dat.bcg$cneg),+       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),        ilab.xpos=c(-9.5,-8,-6,-4.5), cex=0.75, ylim=c(-1, 27),
-       order=order(dat.bcg$alloc), rows=c(3:4,9:15,20:23),+       order=order(dat$alloc), rows=c(3:4,9:15,20:23),
        xlab="Risk Ratio", mlab="", psize=1, header="Author(s) and Year")        xlab="Risk Ratio", mlab="", psize=1, header="Author(s) and Year")
  
Line 34: Line 38:
      ", p = ", .(formatC(res$QEp, digits=2, format="f")), "; ", I^2, " = ",      ", p = ", .(formatC(res$QEp, digits=2, format="f")), "; ", I^2, " = ",
      .(formatC(res$I2, digits=1, format="f")), "%)")))      .(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 ### set font expansion factor (as in forest() above) and use bold italic
Line 55: Line 67:
  
 ### fit random-effects model in the three subgroups ### fit random-effects model in the three subgroups
-res.s <- rma(ai=tposbi=tnegci=cpos, di=cneg, data=dat.bcg, measure="RR", +res.s <- rma(yivi, subset=(alloc=="systematic"), data=dat
-             subset=(alloc=="systematic"), method="REML"+res.r <- rma(yivi, subset=(alloc=="random"), data=dat
-res.r <- rma(ai=tposbi=tnegci=cpos, di=cneg, data=dat.bcg, measure="RR", +res.a <- rma(yivi, subset=(alloc=="alternate"), data=dat)
-             subset=(alloc=="random"), method="REML"+
-res.a <- rma(ai=tposbi=tnegci=cpos, di=cneg, data=dat.bcg, measure="RR", +
-             subset=(alloc=="alternate"), method="REML")+
  
 ### add summary polygons for the three subgroups ### add summary polygons for the three subgroups
plots/forest_plot_with_subgroups.txt · Last modified: 2023/09/21 19:09 by Wolfgang Viechtbauer