Today we learned how to make R build us confidence intervals and how to interpret them. I’m simply going to go through and put in the code for everything we learned and interpret it as I go.
data(women)
attach(women)
mod <- lm(weight ~ height)
confint(mod, level = .9)
## 5 % 95 %
## (Intercept) -98.030599 -77.002734
## height 3.288603 3.611397
So this means that if we construct a simple linear regression model for women’s weight based on height, a 90 percent confidence interval for the intercept is (-98, -77) and for the slope it would be (3.29, 3.61).
cor(height, weight)
## [1] 0.9954948
This gives us a value for r from 0 to 1 with 0 being no correlation and 1 being perfect correlation.
summary(mod)
##
## Call:
## lm(formula = weight ~ height)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.7333 -1.1333 -0.3833 0.7417 3.1167
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -87.51667 5.93694 -14.74 1.71e-09 ***
## height 3.45000 0.09114 37.85 1.09e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.525 on 13 degrees of freedom
## Multiple R-squared: 0.991, Adjusted R-squared: 0.9903
## F-statistic: 1433 on 1 and 13 DF, p-value: 1.091e-14
As we can see from the summary of our regression model, the equation for predicting weight has an intercept of -87.51667 and goes up by 3.45 for every inch upwards.
newdata <- data.frame(height = 66)
This created a new data set based with only heights of 66. We can use this new data set to construct intervals for weight when height is 66.
predy <- predict(mod, newdata, interval="predict")
predy
## fit lwr upr
## 1 140.1833 136.775 143.5916
So for a woman who is 66 inches tall our 95% prediction interval is (136.775, 143.5916). Now we can set up a confidence interval for women with height of 66 inches, which would presumably be smaller.
confy <- predict(mod, newdata, interval='confidence')
confy
## fit lwr upr
## 1 140.1833 139.3102 141.0565
So our 95% confidence interval for women’s weight with height of 66 inches is (139.3102, 141.0565). While this interval is smaller, we can see that both the confidence interval and prediction interval are both centered at 140.1833.