I'd like to bootstrap test a hypothesis (two sample Student's t-test). In Efron and Tibshirani 1993 p.224 there is explicit code for that: for each observation, subtract its group mean and add the overall mean where the overall mean is the mean of the combined samples. They claim that we should bootstrap distributions under null hypothesis and that's the reason why we should do this.
However, I also learnt that it's possible to bootstrap from the samples directly without modifying them. I tried both methods: Efron's steps (using the function boot_t_F
) and also without transforming the observations (using function boot_t_B
).
The resulting bootstrapped p-values (as the proportion of those bootstrapped test statistics that exceed original test statistic) should be exactly the same, but they are not.
Why is this?
My two functions are below:
boot_t_B<-function(x,y){
print(t.test(x, y, var.equal=TRUE)) #original test statistics
t.est<-abs(t.test(x, y, var.equal=TRUE)$statistic) #Student's t-test
grand_mean<-mean(c(x,y), na.rm=T) #global mean
x1<-x #-mean(x, na.rm=T)+grand_mean it's not subtracted/added here
y1<-y #-mean(y, na.rm=T)+grand_mean
B <- 10000 #number of bootstrap samples
t.vect <- vector(length=B) #vector for bootstrapped t-statistics
for(i in 1:B){
boot.c <- sample(x1, size=length(x), replace=T)
boot.p <- sample(y1, size=length(y), replace=T)
t.vect[i] <- t.test(boot.c, boot.p, var.equal=TRUE)$statistic
}
return(mean(t.vect>t.est)) #bootstrapped p-value
}
boot_t_F<-function(x,y){
print(t.test(x, y, var.equal=TRUE)) #original test statistics
t.est<-abs(t.test(x, y, var.equal=TRUE)$statistic) #Student's t-test
grand_mean<-mean(c(x,y), na.rm=T) #global mean
x1<-x-mean(x, na.rm=T)+grand_mean
y1<-y-mean(y, na.rm=T)+grand_mean
B <- 10000 #number of bootstrap samples
t.vect <- vector(length=B) #vector for bootstrapped test-statistics
for(i in 1:B){
boot.c <- sample(x1, size=length(x), replace=T)
boot.p <- sample(y1, size=length(y), replace=T)
t.vect[i] <- t.test(boot.c, boot.p, var.equal=TRUE)$statistic
}
return(mean(t.vect>t.est)) #bootstrapped p-value
}
set.seed(1678)
boot_t_B(rnorm(25,0,10), rnorm(25,5,10))
[1] 4e-04
set.seed(1678)
boot_t_F(rnorm(25,0,10), rnorm(25,5,10))
[1] 0.0507
Note: I chose 'randomly' the (normal) distribution of the samples.
boot_t_B
your arguments are bothrnorm
, but in your call toboot_t_F
they arerunif
andrexp
. $\endgroup$