R-整洁地增加置信区间 [英] R - tidy augment confidence interval
问题描述
我想知道如何使用扫帚
包计算置信区间。
I am wondering how I can compute confidence interval using the broom
package.
我要尝试的操作很简单且标准:
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)
使用 visreg
我可以使用 CI
来简单地绘制回归模型:
Using visreg
I can plot regression models with CI
very simply with :
library(visreg)
visreg(mod, 'x', overlay=TRUE)
我对使用 broom
和 ggplot2
重现此内容很感兴趣,到目前为止,我仅实现了这一点:
I am interesting in reproducing this using broom
and ggplot2
, so far I only achieved this :
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))
增补
函数不能计算 conf.int
。有什么线索可以添加平滑
的置信区间吗?
The augment
funciton doesn't compute conf.int
. Any clue how I can add some smooth
confidence invervals ?
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:
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()
我将 colour = y
移到了 geom_point()
,因为您不能对 geom_ribbon
应用颜色美学。
I moved colour=y
into geom_point()
because you can't apply a colour aesthetic to geom_ribbon
.
这篇关于R-整洁地增加置信区间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!