How to find the confidence and prediction intervals when using broom.
I am trying to understand prediction intervals and confidence intervals.
broom::augment() has a .sigma column, which has one value per data.
I understood this to be a confidence interval.
mod <- lm(dist ~ speed - 1,data=cars)
mod
#augment(mod)
augment(mod) %>%
ggplot(aes(x=speed,y=dist))+
geom_point()+
geom_line(aes(x=speed,y=.fitted))+
geom_ribbon(aes(ymin=.fitted-.sigma*1.96,ymax=.fitted+.sigma*1.96),alpha=0.7)
glance() has a sigma column, which is one whole data.
I understood that the range obtained by this is the prediction interval.
glance(mod)
augment(mod) %>%
ggplot(aes(x=speed,y=dist))+
geom_point()+
geom_line(aes(x=speed,y=.fitted))+
geom_ribbon(aes(ymin=.fitted- (16.3*1.96),ymax=.fitted+(16.3*1.96)),alpha=0.7)
The result of the predict("predict") function matches the figure.
predict(mod,cars,interval = "predict") %>%
as_tibble() %>%
cbind(cars) %>%
ggplot(aes(x=speed,y=dist))+
geom_point()+
geom_line(aes(x=speed,y=fit))+
geom_ribbon(aes(ymax=upr,ymin=lwr),color="red",alpha=0.7)
However, it does not match predict("confidence").
Am I making a mistake here?
predict(mod,cars,interval = "confidence") %>%
as_tibble() %>%
cbind(cars) %>%
ggplot(aes(x=speed,y=dist))+
geom_point()+
geom_line(aes(x=speed,y=fit))+
geom_ribbon(aes(ymax=upr,ymin=lwr),color="blue",alpha=0.7)
how to make confidence interval with predict() and broom::??