If necessary, install the Mosaic package.

install.packages("mosaic")

Constructing a Confidence Interval for a Population Mean from Raw Data

Let’s construct a confidence interval for the population mean miles per gallon of a 2014 Toyota Camry. This follows Example 4 in Section 9.2. First, let’s enter the data in Table 3.

mpg <- c(25.9, 26.2, 32.5, 32.0, 27.2, 30.0, 25.5, 26.4, 27.4, 27.4, 27.0, 28.0, 31.2, 25.1, 30.6, 27.2)
Table3 <- data.frame("mpg"=mpg)   #Create a data frame and name column "mpg"
head(Table3,n=4)
##    mpg
## 1 25.9
## 2 26.2
## 3 32.5
## 4 32.0

Now, let’s determine a single bootstrap mean.

library(mosaic)
mean(~mpg,data=resample(Table3))
## [1] 27.96875

Let’s determine another bootstrap mean.

library(mosaic)
mean(~mpg,data=resample(Table3))
## [1] 27.71875

Now, let’s find 2000 resamples and display the first four bootstrap means.

bootstrap <- do(2000)*mean(~mpg,data=resample(Table3))  #Find 2000 bootstrap means
head(bootstrap,n=4)
##      mean
## 1 26.9250
## 2 28.4375
## 3 27.6500
## 4 27.8500

Now, we find the 2.5th and 97.5th percentiles to find the lower and upper bound for a 95% confidence interval.

qdata(~mean,c(0.025,0.975),data=bootstrap)  
##     2.5%    97.5% 
## 26.98734 29.28766

Now, let’s compare the result above to that obtained using Student’s t-distribution.

library(mosaic)
confint(t.test(~mpg,data=Table3,conf.level=0.95))
##   mean of x    lower    upper level
## 1      28.1 26.83183 29.36817  0.95

We are 95% confident the mean miles per gallon of a 2014 Toyota Camry is between 26.83 and 29.37.