【发布时间】:2016-11-10 17:02:34
【问题描述】:
我想知道如何使用 broom 包计算置信区间。
我想做的是简单而标准的:
set.seed(1)
x <- runif(50)
y <- 2.5 + (3 * x) + rnorm(50, mean = 2.5, sd = 2)
dat <- data.frame(x = x, y = y)
mod <- lm(y ~ x, data = dat)
使用visreg,我可以非常简单地使用CI绘制回归模型:
library(visreg)
visreg(mod, 'x', overlay=TRUE)
我对使用broom 和ggplot2 重现这个很感兴趣,到目前为止我只实现了这个:
library(broom)
dt = lm(y ~ x, data = dat) %>% augment(conf.int = TRUE)
ggplot(data = dt, aes(x, y, colour = y)) +
geom_point() + geom_line(data = dt, aes(x, .fitted, colour = .fitted))
augment 函数不计算 conf.int。有什么线索可以添加一些smooth 置信度反转吗?
geom_smooth(data=dt, aes(x, y, ymin=lcl, ymax=ucl), size = 1.5,
colour = "red", se = TRUE, stat = "smooth")
【问题讨论】:
-
为什么不使用
geom_smooth(method="lm")和原始数据框来添加带有 CI 的回归线? -
原因是我需要添加许多不同的行并做更复杂的事情,所以我想知道是否有一种简单的方法可以使用
broom进行绘图。geom_smooth(method="lm")当您有很多变量并且想要控制绘制哪条线时,它是否复杂?