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 \(1-\alpha\). A confidence interval takes on the form: \[\bar X \pm {t_{\alpha /2,N - 1}}{S_{\bar X}}\] where \(t_{\alpha /2,N - 1}\) is the value needed to generate an area of α/2 in each tail of a t-distribution with n-1 degrees of freedom and \({S_{\bar X}} = \frac{s}{{\sqrt N }}\) is the standard error of the mean.

9.2.1 Calculate a confidence interval

To calculate a confidence interval, we need the following steps

  1. Calculate the mean
  2. Calculate the standard error of the mean
  3. Find the t-score that corresponds to the confidence level
  4. 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")
  sample.mean <- mean(mtcars$mpg)
  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 \({S_{\bar X}} = \frac{\sigma}{{\sqrt N }}\), and if we do not know the population standard deviation \({S_{\bar X}} = \frac{s}{{\sqrt N }}\).

The sd() command can be used to find the standard deviation. The length() command can be use to determine the sample size.

sample.n <- length(mtcars$mpg)
sample.sd <- sd(mtcars$mpg)
sample.se <- sample.sd/sqrt(sample.n)
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 \(\alpha/2\) probability in the lower and upper tails, we divide by two because there are two tails. The qt() command will calculate the t-score, \(t_{\alpha /2,N - 1}\).

alpha = 0.05
degrees.freedom = sample.n - 1
t.score = qt(p=alpha/2, df=degrees.freedom,lower.tail=F)
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_{\alpha /2,N - 1}}{S_{\bar X}}\)

  margin.error <- t.score * sample.se

The confidence interval is the mean +/- margin of error

  lower.bound <- sample.mean - margin.error
  upper.bound <- sample.mean + margin.error
  print(c(lower.bound,upper.bound))
## [1] 17.91768 22.26357

9.3 R code used in the VoiceThread


data("mtcars")

sample.mean <- mean(mtcars$mpg)
print(sample.mean)

sample.n <- length(mtcars$mpg)
sample.sd <- sd(mtcars$mpg)
sample.se <- sample.sd/sqrt(sample.n)
print(sample.se)

alpha = 0.05
degrees.freedom = sample.n - 1
t.score = qt(p=alpha/2, df=degrees.freedom,lower.tail=F)
print(t.score)

margin.error <- t.score * sample.se
lower.bound <- sample.mean - margin.error
upper.bound <- sample.mean + margin.error
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
l.model <- lm(mpg ~ 1, mtcars)

# 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.