Confidence Interval
In the last exercise you found that there is quite a wide prediction interval. This meant that the range of possible amounts that someone from our sample would like us if we gave them 3 units of money was quite large.
Let's have a look at the range of mean predicted scores if we gave people 3 units of money in the population through calculating the confidence interval. The formula for a confidence interval is $$\hat{y} \pm margin of error$$ , where the margin of error is $$ 2 * \frac{s_{res}}{ \sqrt n} $$. Again, R provides us with a way to do this in one go! We do this in exactly the same way as our prediction interval, except instead of interval = "predict"
, we use interval = "confidence"
. For example, for a 90% confidence interval we might put: predict(model, newdata, level = 0.9, interval = "confidence")
.
Let's try to find the 95% confidence interval for our prediction that if you give someone 3 units of money, they will like you an amount of 3.835.
This exercise is part of the course
Inferential Statistics
Exercise instructions
- In your script, add a line of code to calculate the 95% confidence interval for the amount someone from our sample would like us if we gave them 3 units of money.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Vector containing the amount of money you gave participants
money <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
# Vector containing the amount the participants liked you
liking <- c(2.2, 2.8, 4.5, 3.1, 8.7, 5.0, 4.5, 8.8, 9.0, 9.2)
# Fitted model
mod1 <- lm(liking~money)
# Value for which you would like a prediction
nd <- data.frame(money=3)
# Find the confidence interval