Practice 9 Calculating Confidence Intervals in R
9.1 Directions
In this practice exercise, you will calculate a confidence interval in R.
9.2 A closer look at the code
A confidence interval is an interval that contains the population parameter with probability . A confidence interval takes on the form: where is the value needed to generate an area of α/2 in each tail of a t-distribution with n-1 degrees of freedom and is the standard error of the mean.
9.2.1 Calculate a confidence interval
To calculate a confidence interval, we need the following steps
- Calculate the mean
- Calculate the standard error of the mean
- Find the t-score that corresponds to the confidence level
- Calculate the margin of error and construct the confidence interval
9.2.1.1 Step 1: Calculate the mean
Use the mean()
command to calculate the average mpg.
data("mtcars")
mean(mtcars$mpg)
sample.mean <-print(sample.mean)
## [1] 20.09062
9.2.1.2 Step 2: Calculate the standard error of the mean
The formula for the standard error of the mean is , and if we do not know the population standard deviation .
The sd()
command can be used to find the standard deviation. The length()
command can be use to determine the sample size.
length(mtcars$mpg)
sample.n <- sd(mtcars$mpg)
sample.sd <- sample.sd/sqrt(sample.n)
sample.se <-print(sample.se)
## [1] 1.065424
9.2.1.3 Step 3: Find the t-score that corresponds to the confidence level
We need to have probability in the lower and upper tails, we divide by two because there are two tails. The qt()
command will calculate the t-score, .
0.05
alpha = sample.n - 1
degrees.freedom = qt(p=alpha/2, df=degrees.freedom,lower.tail=F)
t.score =print(t.score)
## [1] 2.039513
9.2.1.4 Step 4. Calculate the margin of error and construct the confidence interval
The margin of error is
t.score * sample.se margin.error <-
The confidence interval is the mean +/- margin of error
sample.mean - margin.error
lower.bound <- sample.mean + margin.error
upper.bound <-print(c(lower.bound,upper.bound))
## [1] 17.91768 22.26357
9.3 R code used in the VoiceThread
data("mtcars")
mean(mtcars$mpg)
sample.mean <-print(sample.mean)
length(mtcars$mpg)
sample.n <- sd(mtcars$mpg)
sample.sd <- sample.sd/sqrt(sample.n)
sample.se <-print(sample.se)
0.05
alpha = sample.n - 1
degrees.freedom = qt(p=alpha/2, df=degrees.freedom,lower.tail=F)
t.score =print(t.score)
t.score * sample.se
margin.error <- sample.mean - margin.error
lower.bound <- sample.mean + margin.error
upper.bound <-print(c(lower.bound,upper.bound))
9.4 A much easier way:
Let’s use linear regression as a short cut
# Calculate the mean and standard error
lm(mpg ~ 1, mtcars)
l.model <-
# Calculate the confidence interval
confint(l.model, level=0.95)
## 2.5 % 97.5 %
## (Intercept) 17.91768 22.26357
9.5 Now you try
Use R to complete the following activities (this is just for practice you do not need to turn anything in).
Using the mtcars data set, find a 95% confidence interval for the average horsepower, hp
.