0

I am trying to make a forest plot with three subgroups with the 'metafor' package. I tried implementing the code the metafor site provides that uses subgroups.
My subgroups are for example A ,B, and C.
When I run the code it tells me:

"Error in forest.rma(meta.random.model, xlim = c(-16, 6), at = log(c(0.05,: Number of outcomes does not correspond to the length of the 'rows' argument."

I am really lost and will need help. I wanted to know if somebody can help wit the code.

For Example, mydata for the diagnostics looks something like this:

Author <- c('Study 1', 'Study 2', 'Study 3', 'Study 4','Study 5', 'Study 6', 'Study 7', 'Study 8', 'Study 9', 'Study 10', 'Study 11', 'Study 12', 'Study 13', 'Study 14', 'Study 15', 'Study 16', 'Study 17', 'Study 18', 'Study 19','Study 20', 'Study 21', 'Study 22', 'Study 23', 'Study 24', 'Study 25', 'Study 26', 'Study 27', 'Study 28', 'Study 29', 'Study 30',  'Study 31', 'Study 32', 'Study 33') 

Year <- c(2010,  2010,    2012, 2012,  2004, 2004,    2004,    2006,    2003, 2003,    2003,    2012,    2012,    2004,    2016,    2001,    2006,    2001, 2006,    2006,    2002,    2001,    2001,    2008,    2008,    2008,   2008, 2008, 2008, 2006,    2006,    2006,    2006)


modality<- c('A','A','A','A','A','A','A','A','A','A','A','A','A','A','B','B','B','B','C','C','C','C','C','C','C','C','C','C','C','C','C','C','C')

tp<-c(80, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71 ,72, 73, 74, 75, 76, 77, 78, 79, 80,  81, 82,  83  ,84,85 ,86  ,87 ,88 ,89,90, 91)

fp<-c(6,  2,  3,   4, 5, 6,  7, 8, 9,  10,  11, 12, 13, 14,   15, 16, 17, 18,  19,  20,  21, 22,  23,  24, 25, 26,   27,  28, 29,  30, 31, 32, 33)

fn<-c(2,  6, 7, 8, 9, 10, 11, 12, 13, 14,  15, 16, 17, 18, 19, 20, 21,  22, 23, 24, 25, 26, 27, 28, 29, 30,  31, 32, 33, 34, 35, 36, 37)  

tn<-c(23, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,  53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66)

The picture of the Forest Plot I was attempting to mimic can be found at this location: How do I adjust the rows to my data set for the forest plot?.

However, I have no use for the middle columns. I planned on removing that part of the code after getting the subgroups to work.

I found this code online and seems not to work for me:

library(metafor)

### decrease margins so the full space is used
par(mar=c(4,4,1,2))   

### fit random-effects model (use slab argument to define study labels)
res <- rma(ai=tpos, bi=tneg, ci=cpos, di=cneg,
data=dat.bcg, measure="RR",slab=paste(author, year, sep=", "), method="REML")

### 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 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"))   
text(-16,  26, "Author(s) and Year",  pos=4)   
text(6, 26, "Risk Ratio [95% CI]", pos=2)

### set par back to the original settings
par(op)

### fit random-effects model in the three subgroups
res.s <- rma(ai=tpos, bi=tneg, ci=cpos, di=cneg,
data=dat.bcg, measure="RR",  
subset=(alloc=="systematic"), method="REML")

res.r <- rma(ai=tpos, bi=tneg, ci=cpos, di=cneg,
data=dat.bcg, measure="RR",             
subset=(alloc=="random"), method="REML")


res.a <- rma(ai=tpos, bi=tneg, ci=cpos, di=cneg,
data=dat.bcg, measure="RR",
subset=(alloc=="alternate"), method="REML")    

### 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")), "%)")))
Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • Could you please format your code as code, and reduce your code to the minimum amount necessary to reproduce the error? Right now, your question has huge amount of unformatted code, which makes it tedious for anyone trying to help you to identify the problem and find a solution. See [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve) – Jan Boyer Jul 03 '18 at 18:15
  • I tried to improve the formatting, but the original code was a disaster, Please verify that this is still correct – Dave2e Jul 03 '18 at 20:00
  • Hi Dave, Thanks so much for taking a look at my question. I am still having difficulty formatting my code. Could you please walk me through. Thanks – Fred Antwi Jul 04 '18 at 02:12
  • Yes, I just checked the formatted code and is correct. Thanks so much, Dave – Fred Antwi Jul 04 '18 at 02:19

0 Answers0