To
find the least-squares regression model, use the lm() command. From that
result, we can find the standard error.
We
will use the cholesterol data from Section 14.1, Table 1.
Age <- c(25, 25, 28, 32, 32, 32, 38, 42, 48, 51, 51, 58, 62, 65)
Cholesterol <- c(180, 195, 186, 180, 210, 197, 239, 183, 204, 221, 243, 208, 228, 269)
Table1 <- data.frame('Age'=Age, "Cholesterol"=Cholesterol)
head(Table1)
## Age Cholesterol
## 1 25 180
## 2 25 195
## 3 28 186
## 4 32 180
## 5 32 210
## 6 32 197
Find
the least-squares regression model and save it as an object.
lm_object <- lm(Cholesterol ~ Age, data=Table1)
To
create a confidence interval for the mean value of the response variable, use
the predict() command with interval = ‘confidence’.
predict(lm_object,newdata=data.frame('Age'=42),interval='confidence',level=0.95)
## fit lwr upr
## 1 210.1144 198.7704 221.4583
# NOTE: If the data is in a data frame, use the above code. If the data is in a list (vector), use newdata=list('Age'=42). If a confidence interval for a different age is desired, change the value of '42' in the code to a different age. If a different level of confidence is desired, change the level.
We
are 95% confident that the mean total cholesterol of all 42-year-old females is
bewteen 198.8 mg/dL and 221.4 mg/dL.
To
create a prediction interval for an individual response, change the interval to
‘prediction’.
predict(lm_object,newdata=data.frame('Age'=42),interval='prediction',level=0.95)
## fit lwr upr
## 1 210.1144 166.1801 254.0486
We
are 95% confident that the total cholesterol of a randomly selected 42-year-old
female is between 166.2 mg/dL and 254.0 mg/dL.