# One-sample bootstrap test example using treatment group data from p. 11 of E&T. set.seed(321) treat <- c(94,197,16,38,99,141,23) # First I'll do Student's t test. (I'll do a two-tailed test, using 129 for the # null mean.) t.test( treat, mu = 129 ) # For a bootstrap test, first I'll compute the value of the test # statistic using the observed sample. t.stat <- sqrt( 7 )*( mean( treat ) - 129 )/sd( treat ) # Now I'll get replicates of the test statistic based on bootstrap samples drawn # from the shifted empirical distribution (shifted to be in agreement with the # null hypothesis that the mean is 129). treat.shift <- treat - mean( treat ) + 129 bss <- matrix( sample( treat.shift, 7*4000, replace=T ), nrow=4000 ) mean.rep <- apply( bss, 1, mean ) sd.rep <- apply( bss, 1, sd ) t.rep <- sqrt( 7 )*( mean.rep - 129 )/sd.rep # Now I'll obtain the estimated lower-tail probability, the estimated upper-tail # probability, and multiply the smaller of the two by 2 to obtain the p-value. lower.prob <- length( t.rep[ t.rep <= t.stat ] )/4000 upper.prob <- length( t.rep[ t.rep >= t.stat ] )/4000 p.value <- 2*min( lower.prob, upper.prob ) # Here is the upper-tail probability: upper.prob # Here is the lower-tail probability: lower.prob # The lower-tail probability is close to the value of 0.10 reported in E&T. # Here is the p-value for the two-tailed test: p.value # Johnson's modified t test results in a p-value of 0.2049.