我在想如何使用broom
包來計算置信區間。R - 整齊增強置信區間
我所試圖做的是簡單的標準:
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
信心invervals?
geom_smooth(data=dt, aes(x, y, ymin=lcl, ymax=ucl), size = 1.5,
colour = "red", se = TRUE, stat = "smooth")
爲什麼你不使用'geom_smooth(方法= 「LM」)'用原來的數據框添加CI的迴歸線? –
原因是我需要添加許多不同的線條和做更復雜的東西,所以我想知道是否有一個簡單的方法來繪製'掃帚'。 'geom_smooth(method =「lm」)'當你有很多變量並且你想控制哪一行被繪製時,它會變得複雜嗎? – giacomo