【问题标题】:Change confidence interval format in package metafor forest graph?更改包 metafor 森林图中的置信区间格式?
【发布时间】:2016-08-10 04:40:58
【问题描述】:

假设下面的代码(如 Viechtbauer, 2010 中给出的):

library(metafor)
data("dat.bcg", package = "metafor")
dat <- escalc(measure = "RR", ai = tpos, bi = tneg, ci = cpos, di = cneg, data = dat.bcg, append = TRUE)
res <- rma(ai = tpos, bi = tneg, ci = cpos, di = cneg, data = dat, measure = "RR")
forest(res, slab = paste(dat$author, dat$year, sep = ", "), 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)
op <- par(cex = 0.75, font = 2)
text(c(-9.5, -8, -6, -4.5), 15, c("TB+", "TB-", "TB+", "TB-"))
text(c(-8.75, -5.25), 16, c("Vaccinated", "Control"))
text(-16, 15, "Author(s) and Year", pos = 4)
text(6, 15, "Relative Risk [95% CI]", pos = 2)
par(op)

这给出了如下的森林图:

那么如何更改图表中置信区间的格式?是否可以用括号替换括号并使用“to”而不是“,”?使用“-”或长连字符代替“,”怎么样?这应该改变,即 [0.13, 1.26] 到 (0.13 to 1.26) 或 (0.13 – 1.26)

【问题讨论】:

  • 我会考虑将此添加为可选功能(将括号更改为括号和/或 CI 的“分隔”符号)。但是各种forest() 函数已经有这么多参数,我有点犹豫要添加更多。
  • 其实这不是我个人的喜好,但现在有些期刊要求这样做!
  • 我还有两个问题,如果你不介意给他们一些提示: 1- 通过哪个选项我可以增加 CI 行的长度,这样它们就不会显示为箭头? 2- 我使用dev.copy() 来保存森林图。保存的图表在列之间有很大的空间,除非我更改dev.copy() 中的比率。是否有任何选项可以更改森林图中的空间?
  • 您是否尝试过将pngpdf 与宽度和高度参数一起使用?
  • 1.如果 CI 边界超出轴限制,则会绘制箭头。因此,通过使用更宽的 at 值(或 alim)来增加轴限制。 2. 我不太明白你在问什么。我从不使用dev.copy()。正如@42 所建议的那样,使用pngpdf(或您喜欢的任何设备)并设置widthheight 参数。

标签: r plot confidence-interval


【解决方案1】:

您需要对forest.rma 的代码进行一些修改。几个步骤:

通过键入函数名称显示当前版本的代码后:

 forest.rma   # Copy the name and the code and paste into the console 
              #  Add an assignment operator `<-`
              # leave off the bytecode and environment notations at the bottom

或者您可以在编辑器中执行此操作,这可能是首选方法,因为您可能希望将此代码保存到 .Rprofile 文件中。

1) 将参数添加到参数列表中:

forest.rma <- 
function (x, annotate = TRUE, addfit = TRUE, addcred = FALSE, 
    showweights = FALSE, xlim, alim, clim, ylim, at, steps = 5, 
    level = x$level, digits = 2, refline = 0, xlab, slab, mlab, 
    ilab, ilab.xpos, ilab.pos, order, transf, atransf, targs, 
    rows, efac = 1, pch = 15, psize, col, border, lty, cex, cex.lab, 
    cex.axis, annosep = " , ", bkt=c("[", "]"), ...) 
{  #  ....not showing all the _long_ function body 
   # Scroll down to almost the bottom of the function body

2) 查找并更改 annotext cbind-assignment 的参数。有几个地方可能会构造annotext,但其中只有一个与您的“格式目标”匹配。找到一个看起来像这样的:

# annotext <- cbind(annotext[, 1], " [ ", annotext[, 
#                2], " , ", annotext[, 3], " ]")

改成这样:

annotext <- cbind(annotext[, 1], bkt[1], annotext[, 
                 2], annosep, annotext[, 3], bkt[2] )
# hit enter to get the modification to hold in your workspace

3) 现在为函数分配正确的环境,这样它就可以和它的兄弟一起玩了:

environment(forest.rma) <- environment(forest.default)
# if you forget this step you see this error:

forest.rma(res,slab = paste(dat$author,dat$year,sep = ", "), 中的错误: 找不到函数“.setlab”

并使用您选择的新参数调用它:

png(); forest(res, slab = paste(dat$author, dat$year, sep = ", "), 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, annosep=" to ", bkt = c( "(", ")" ) )
op <- par(cex = 0.75, font = 2)
text(c(-9.5, -8, -6, -4.5), 15, c("TB+", "TB-", "TB+", "TB-"))
text(c(-8.75, -5.25), 16, c("Vaccinated", "Control"))
text(-16, 15, "Author(s) and Year", pos = 4)
text(6, 15, "Relative Risk [95% CI]", pos = 2)
dev.off()

【讨论】:

    【解决方案2】:

    这是一个不需要更改forest.rma() 函数代码的解决方案。我使用annotate=FALSE,因此该函数不会注释森林图,而是自己添加这些注释。

    library(metafor)
    data("dat.bcg", package = "metafor")
    dat <- escalc(measure = "RR", ai = tpos, bi = tneg, ci = cpos, di = cneg, data = dat.bcg, append = TRUE)
    
    res <- rma(ai = tpos, bi = tneg, ci = cpos, di = cneg, data = dat, measure = "RR")
    
    ### note the use of annotate=FALSE in forest()
    forest(res, slab = paste(dat$author, dat$year, sep = ", "), 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, annotate=FALSE) 
    
    op <- par(cex = 0.75, font = 2)
    text(c(-9.5, -8, -6, -4.5), 15, c("TB+", "TB-", "TB+", "TB-"))
    text(c(-8.75, -5.25), 16, c("Vaccinated", "Control"))
    text(-16, 15, "Author(s) and Year", pos = 4)
    text(6, 15, "Relative Risk [95% CI]", pos = 2)
    
    ### add annotations manually
    tmp <- summary(dat, transf=exp)[,c("yi","ci.lb","ci.ub")] ### for the individual studies
    tmp <- rbind(tmp, with(predict(res, transf=exp), c(pred, ci.lb, ci.ub))) ### add model estimate and CI bounds
    sav <- apply(tmp, 2, formatC, format="f", digits=2)
    annotext <- apply(sav, 1, function(x) {paste0(x[1], " (", x[2], " to ", x[3], ")")})
    text(6, c(res$k:1, -1), annotext, pos=2, font=1)
    
    par(op)
    

    【讨论】:

    • 感谢简洁的代码。实际上这不是我个人的偏好,但现在有些期刊要求这样做。如果您不介意为此提供一些提示,我还有两个问题:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    相关资源
    最近更新 更多