[英]R - tidy augment confidence interval
I am wondering how I can compute confidence interval using the broom
package. 我想知道如何使用
broom
包计算置信区间。
What I am trying to do is simple and standard : 我正在尝试做的是简单和标准的:
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)
Using visreg
I can plot regression models with CI
very simply with : 使用
visreg
我可以使用CI
非常简单地绘制回归模型:
library(visreg)
visreg(mod, 'x', overlay=TRUE)
I am interesting in reproducing this using broom
and ggplot2
, so far I only achieved this : 在使用
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))
The augment
funciton doesn't compute conf.int
. augment
函数不计算conf.int
。 Any clue how I can add some smooth
confidence invervals ? 有什么线索可以添加
smooth
置信区间吗?
geom_smooth(data=dt, aes(x, y, ymin=lcl, ymax=ucl), size = 1.5,
colour = "red", se = TRUE, stat = "smooth")
Using the broom
output, you can do something like this: 使用
broom
输出,您可以执行以下操作:
ggplot(data = dt, aes(x, y)) +
geom_ribbon(aes(ymin=.fitted-1.96*.se.fit, ymax=.fitted+1.96*.se.fit), alpha=0.2) +
geom_point(aes(colour = y)) +
geom_line(aes(x, .fitted, colour = .fitted)) +
theme_bw()
I moved colour=y
into geom_point()
because you can't apply a colour aesthetic to geom_ribbon
. 我将
colour=y
移至geom_point()
因为您无法将颜色美学应用于geom_ribbon
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.