 # How to find the confidence and prediction intervals when using broom

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::??

Will this suffice?

mod <- lm(dist ~ speed - 1,data=cars)
predict(mod, newdata = cars, interval = "predict")
predict(mod, newdata = cars, interval = "confidence")

the documentation says
.sigma
Estimated residual standard deviation when corresponding observation is dropped from model.
it also shows you can get a
.lower
Lower bound on interval for fitted values.
and
.upper
Upper bound on interval for fitted values.

but to do this you need to choose (just as you did with predict() the interval type you want.
i.e. you can do

``````#augment(mod)
augment(mod,interval = "prediction") %>%
ggplot(aes(x=speed,y=dist))+
geom_point()+
geom_line(aes(x=speed,y=.fitted))+
geom_ribbon(aes(ymin=.lower,ymax=.upper),alpha=0.7)

augment(mod,interval = "confidence") %>%
ggplot(aes(x=speed,y=dist))+
geom_point()+
geom_line(aes(x=speed,y=.fitted))+
geom_ribbon(aes(ymin=.lower,ymax=.upper),alpha=0.7)``````

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.