If necessary, install the Mosaic package.

install.packages("mosaic")
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/4.0'
## (as 'lib' is unspecified)

We are going to follow Example 3 from Section 10.3A.

The “fun size” of a Snickers bar is supposed to weigh 20 grams. Because the penalty for selling candy bars under their advertised weight is severe, the manufacturer calibrates the machine so that the mean weight is 20.1 grams. The quality-control engineer at M&M-Mars, the Snickers manufacturer, is concerned about the calibration. He obtains a random sample of 11 candy bars, weighs them, and obtains the data shown. Should the machine be shut down and calibrated?

weight <- c(19.68, 20.66, 19.56, 21.5, 19.98, 20.65, 19.61, 19.74, 20.55, 20.36, 21.02)
Table1 <- data.frame("weight"=weight)
library(mosaic)
mean(~weight,data=Table1)
## [1] 20.30091

We are testing the following hypotheses:

\(H_0:\mu = 20.1\) grams
\(H_1:\mu \neq 20.1\) grams

The sample mean weight of the candy bars is 20.301 grams. To construct a null model, adjust the sample data so it is consistent with the statement in the null hypothesis. Namely, the data must have a mean equal to 20.1 grams. This can be accomplished by subtracting 0.201 gram (= 20.301 - 20.12) from each observation.

adj_weight <- c(19.479, 20.459, 19.359, 21.299, 19.779, 20.449, 19.409, 19.539, 20.349, 20.159, 20.819)
Table2 <- data.frame("adj_weight" =adj_weight) 
head(Table2,n=3)
##   adj_weight
## 1     19.479
## 2     20.459
## 3     19.359

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

set.seed(12)      #Use the same seed to get these results
bootstrap <- do(2000)*mean(~adj_weight,data=resample(Table2))  #Find 2000 bootstrap means
head(bootstrap,n=4)
##       mean
## 1 20.08627
## 2 20.15900
## 3 20.44900
## 4 19.92809

Now, find the proportion of bootstrap means that are at least 0.201 gram from the mean of 20.1. That is, the proportion of bootstrap means that are less than 19.899 grams or greater than 20.301 grams.

prop(~(mean <= 19.899),data=bootstrap)+prop(~(mean >= 20.301),data=bootstrap)
## prop_TRUE 
##     0.272

The estimated P-value is 0.272.

t.test(~weight,data=Table1,mu=20.1,alternative="two.sided")
## 
##  One Sample t-test
## 
## data:  weight
## t = 1.0406, df = 10, p-value = 0.3226
## alternative hypothesis: true mean is not equal to 20.1
## 95 percent confidence interval:
##  19.87071 20.73111
## sample estimates:
## mean of x 
##  20.30091

The P-value using Student’s t-distribution is 0.323.