0

我想使用 metafor::rma() 作为 ggplot 的平滑器。我已经尝试了各种方法来让它继续运行,但似乎都没有奏效。这是一个最小(非)工作示例:

# Libraries
library(metafor)
library(ggplot2)

# Some data preparation
dat <- escalc(measure="RR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg)

# Scatterplot of the data
figure1 <- ggplot(dat, aes(y = yi, x = ablat)) + geom_point()
figure1

# Various attempts that lead to various error messages :(
figure2a <- ggplot(dat, aes(y = yi, x = ablat)) +
           geom_point() + geom_smooth(method = metafor::rma())

figure2b <- ggplot(dat, aes(y = yi, x = ablat)) +
  geom_point() + geom_smooth(method = metafor::rma(y = yi, vi = vi))

figure2c <- ggplot(dat, aes(y = yi, x = ablat)) +
  geom_point() + geom_smooth(method = metafor::rma(y = dat$yi, vi = dat$vi))

figure2d <- ggplot(dat, aes(y = yi, x = ablat)) +
  geom_point() + geom_smooth(method = metafor::rma(yi = yi, vi = vi, data = dat), formula = yi ~ ablat)

figure2e <- ggplot(dat, aes(y = yi, x = ablat)) +
  geom_point() + geom_smooth(method = metafor::rma(), method.args = list(yi = dat$yi, vi = dat$vi, method = "EB"))

我究竟做错了什么?谢谢

4

1 回答 1

0

我短暂地尝试让它与 . 一起使用geom_smooth(),但我不是一个重度ggplot2用户并且没有成功。但正如@Tjebo 建议的那样,您可以计算预测值,predict()然后使用geom_line()geom_ribbon()添加元素。这是一个例子:

library(metafor)
library(ggplot2)

dat <- escalc(measure="RR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg)
res <- rma(yi, vi, mods = ~ ablat, data=dat)
pred <- as.data.frame(predict(res, newmods = seq(0,60,by=1), addx=TRUE))
names(pred)[8] <- "ablat"

ggplot(dat, aes(x = ablat)) +
   geom_point(aes(y = yi)) +
   geom_line(data = pred, aes(x = ablat, y = pred)) +
   geom_ribbon(data = pred, aes(ymin = ci.lb, ymax = ci.ub), fill = "blue", alpha = 0.2)
于 2020-03-31T18:07:31.543 回答