# using bootstrap to calculate t-test p-value and CI in r

I have a data named best and i want to run bootstrap based on these hypothesis to get p-value and CI:

H0:mu<=1600 and H1:mu>1600

What I've tried is:

best <- c(1660, 1820, 1590,1440, 1730,1680,1750, 17201,900,1570,
1700,1900,1800,1770,2010,1580,1620,1690)
ttest <- c(1:1000)
B      <- 1000
for(i in 1:B){
boot.c <- sample(best, size=18, replace=T)
ttest[i]  <- t.test(boot.c, mu = 1600)$statistic } p <- (1 + sum(abs(ttest[i]) > abs(t.test(best,m=1600)$statistic))) / (1000+1)
p


Is it right? is that so, how can i get CI?

In question we got 𝑦̅=1,718.3 And 𝑠=137.8 to do the t-test without r but when i do it like:

 t.test(best, mu = 1600)


Obtained CI and p-value from code above are so different from what I've calculated with hand. why?

You can run bootstrap using the boot package, and there are a few options for you to construct the confidence interval. First, the boot package can be used like this:

library(boot)
bo = boot(best,function(dat,ind)mean(dat[ind]),R=999)


We can calculate confidence interval like this:

boot.ci(bo)

BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 999 bootstrap replicates

CALL :
boot.ci(boot.out = bo)

Intervals :
Level      Normal              Basic
95%   ( 810, 4188 )   ( 718, 3478 )

Level     Percentile            BCa
95%   (1568, 4327 )   (1614, 6024 )
Calculations and Intervals on Original Scale
Some BCa intervals may be unstable
Warning message:
In boot.ci(bo) : bootstrap variances needed for studentized intervals


And the mighty p-value like (btw @BruceET is correct):

(sum(bo$t>1600)+1)/(999+1) [1] 0.941  Surprising result, 95% of our bootstrapped means are more than 1600? let's check how you bootstrapped mean looks like: hist(bo$t,br=100,main="Histogram of bootstrapped means",xlab="mean")


This looks quite strange, and it is definitely so because you have n=18 and the 8th observation is 17201, ~10x more then the rest. We can look at this:

plot(boot.array(bo)[,8],bo\$t,
xlab = "No of times column 8 is used",ylab="bootstrapped mean")


This may not be the answer you are looking for, but be careful when you have distributions like this. Bootstrap doesn't solve the problem but it does show you have shaky the estimation of the mean can be. The truth is you don't know if you try to measure these "best" values again, will you get an outlier like 17200, so I would say it doesn't make sense try to calculate p-values and 95% ci.

There's not enough samples to perform the t approximation in the ttest, so by choosing bootstrap you can draw samples of your target statistics instead of approximate it with a t distribution. Run boot.c = replicate(1000,mean(sample(best, size=18, replace=T))-1600) will get you the bootstrapped samples of your target statistics. With the target statistics samples, you shouldn't do ttest on them, instead: P value: sum(boot.c<0)/1000 95% confidence interval: quantile(boot.c,c(0.025,0.975))

• Like this there would be so much difference between p-value and CI obtained by t-test and the ones obtained by bootstrap is it acceptable? Apr 20, 2020 at 5:52
• Of course, ttest requires enough samples to approximate the distribution of the target statistics with a t distribution, when there's not enough samples, the t approximation will be bad, thus generate very different result. Apr 20, 2020 at 6:09
• Code for P-value seems wrong. And I get about 95%. Can you explain? Apr 20, 2020 at 6:47
• The t-test comes with a number of assumptions about the data, bootstrapping comes with different assumptions. If both had consistently the same results, there wouldn't be much point in learning both. You need to see, whether you tend to believe, that your data comes from a process that involves normality or whether you tend to believe, that your 18 observations describe the possible data close enough to draw conclusions from only them. Apr 20, 2020 at 6:52
• Bootstrap is a computational method, not an information gathering method. You cannot acquire additional information by re-sampling from existing data.. Apr 20, 2020 at 14:39