Professional Documents
Culture Documents
Binary Input####
# Loading library (if not yet)
library(metafor)
library(xlsx)
# Meta analysis when one or more zero value in 2x2 cells (Three choices: leave it
alone (reported as missing), add small value to all cells (ex: add 0.5 to all
cells), or add small value only to zero value cell)
MindfulRes3 <- rma(ai=A, bi=B, ci=C, di=D, measure="OR", method="DL", add=.5, to=
"all", data=MindfulDat)
MindfulRes3
MindfulRes4 <- rma(ai=A, bi=B, ci=C, di=D, measure="OR", method="DL", add=.5, to=
"only0", data=MindfulDat)
MindfulRes4
MindfulRes5 <- rma(ai=A, bi=B, ci=C, di=D, measure="OR", method="REML", add=.5, to=
"only0", data=MindfulDat)
MindfulRes5
MindfulRes6 <- rma(ai=A, bi=B, ci=C, di=D, measure="OR", method="FE", add=.5, to=
"only0", data=MindfulDat)
MindfulRes6
# Prediction Interval using Higgins formula (input the value based on the results
of previous analysis)
Higgins.PI(M, tausq, SEM, k)
# Read, print McDaniel data (I think it is with McLeod data, so load McLeod excel
file first if below code is not work)
dat.mcdaniel1994
# Trim and fill, trim & fill funnel plot, prediction interval, print it
McDanielRes2 <- trimfill(McDanielRes1)
McDanielRes2
funnel(McDanielRes2)
predict(McDanielRes2)
# EXAMPLE 1
# Load excel file of McLeod, print it
McLeodDat <- read.xlsx("C:\\Users\\Surya Dila\\Desktop\\R tutorial M Brannick\\3
and 10\\McLeod2007.xlsx", sheetName = "Data")
McLeodDat
# Meta analysis with ordered data, print it (overall summary should be the same, of
course!)
McLeodRes2 <- rma(ni=N, ri=r, method="DL", measure="ZCOR", data=McLeod.ES)
McLeodRes2
# Sorted by moderator (example, Dx), then ES, test for moderator; forest by
moderator & ES (with test results)
McLeod.Dx.ES <- McLeodDat[order(McLeodDat$Dx,McLeodDat$r), ]
McLeodRes4 <- rma(ni=N, ri=r, method="DL", measure="ZCOR", mods=~factor(Dx),
data=McLeod.ES)
McLeodRes4
# EXAMPLE 2
# Load libray (if not yet)
library(metafor)
library(xlsx)
# Cummulative analysis
RocksCU <- cumul(RocksRes1, order = order(RocksDat.Yr$Year))
forest(RocksCU)
####Tutorial 13: Forest Plots (Making them look the way you want)####
##I use McLeod data (correlation) instead of sleep data (binary) because I don't
have it. But I just want to practice how to labeling the forest plot##
# Load library (if not yet loaded)
library(metafor)
library(xlsx)
# Sort by Dx and r
McLeod.Dx.r <- McLeodDat[order(McLeodDat$Dx,McLeodDat$r), ]
McLeod.Dx.r
# Meta analysis
McLeodRes1 <- rma(ri=r, ni=N, method="DL", measure="ZCOR", data=McLeod.Dx.r)
McLeodRes1
# Forest plot
forest(McLeodRes1)
data(dat.bcg)
dat <- dat.bcg
########################################################################
# So, just use the text() function to add those column headings. With the ilab and
ilab.xpos arguments, you can add the information for those columns to the plot.
# Note: you have to play with the numbers for placement: trial and error; start
with zero, zero to be in the graph and work from there
# Load excel files that already organized as preferred input format, print it
S103Dat <- read.xlsx("D:\\Researcher\\Online Research Club (SuryaDila)\\Study
103\\Task 14 (data extraction period 1)\\Full assignments results\\Study 103 Data
Extraction\\Data extraction results\\Study 103.xlsx", sheetName="1-4mo")
S103Dat
# Trial forest
# decrease margins so the full space is used
par(mar=c(4,4,1,2))
# fit fixed-effect model (use slab argument to define study labels)
S103Res1 <- rma(ai=TEvent, bi=TNonEvent, ci=CEvent, di=CNonEvent, data=S103Dat,
measure="OR",
slab=paste(Author, Year, sep=", "), method="FE")
### set up forest plot (with 2x2 table counts added; rows argument is used
### to specify exactly in which rows the outcomes will be plotted)
forest(S103Res1, xlim=c(-16, 6), at=log(c(0.05, 0.25, 1, 4)), atransf=exp,
ilab=cbind(S103Dat$TEvent, S103Dat$TNonEvent, S103Dat$CEvent,
S103Dat$CNonEvent),
ilab.xpos=c(-9.5,-8,-6,-4.5), cex=0.75, ylim=c(-8, 8),
xlab="Odds Ratio", mlab="", psize=1)
### add text with Q-value, dfs, p-value, and I^2 statistic
text(-16, -1, pos=4, cex=0.75, bquote(paste("FE Model (Q = ",
.(formatC(S103Res1$QE, digits=2, format="f")), ", df = ", .(S103Res1$k -
S103Res1$p),
", p = ", .(formatC(S103Res1$QEp, digits=2, format="f")), "; ", I^2, " = ",
.(formatC(S103Res1$I2, digits=1, 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)
###################################################################################
################
windows(width=6.5, height=4.0, pointsize=10)
par(mar=c(4,0,4,0))
forest(S103Res1, slab=paste(S103Dat$Author, ", ", S103Dat$Year, sep=""),
xlim=c(-16,6), at=log(c(.05,.25,1,4)), atransf=exp,
ilab=cbind(S103Dat$tmalaria, S103Dat$tnomalaria, S103Dat$cmalaria,
S103Dat$cnomalaria),
ilab.xpos=c(-9.5,-8,-6,-4.5), cex=.8, ylim=c(-1.5,16), efac=1.8)
text(c(-9.5,-8,-6,-4.5), 14.7, c("malaria+", "malaria-", "malaria+", "malaria-"),
font=2, cex=.8)
text(c(-8.75,-5.25), 15.7, c("Treated", "Control"), font=2, cex=.8)
text(-16, 14.7, "Author(s) and Year", pos=4, font=2, cex=.8)
text(6, 14.7, "Odds Ratio [95% CI]", pos=2, font=2, cex=.8)
###################################################################################
#################
### set up forest plot (with 2x2 table counts added; rows argument is used
### to specify exactly 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.bcg$tpos, dat.bcg$tneg, dat.bcg$cpos, dat.bcg$cneg),
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),
xlab="Risk Ratio", mlab="", psize=1)
### 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")), "%)")))
### 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 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")), "%)")))