我想使用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"))我做错了什么?谢谢
发布于 2020-04-01 02:07:32
我曾短暂地尝试过让它与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)https://stackoverflow.com/questions/60922582
复制相似问题