Installing and/or Loading the boot Package

First off, we are going to use the boot package. Thus, you may have to run install.packages("boot") first.

require(boot)

Bootstrapping Example

The following data gives percentages of individuals with education beyond primary school for 47 French-speaking provinces of Switzerland in 1888 (Tukey, 1977).

SwEdu = swiss$Education

hist(SwEdu,prob=TRUE,breaks=15)

Find a bootstrap CI for the population variance \(\sigma^2\). As our point estimate, we will use the sample variance \[S^2 = {\frac{1}{N-1}\sum_{i=1}^N (X_i - \overline{X})^2}, \hspace{4in}\] which can be found in R via var().

Provide the following:

var_fun=function(x,ind){var(x[ind])}
ci=boot(SwEdu,statistic=var_fun,R=1000)
ci
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = SwEdu, statistic = var_fun, R = 1000)
## 
## 
## Bootstrap Statistics :
##     original    bias    std. error
## t1* 92.45606 -4.328758    38.47242
resamps <- replicate(1000, sample(SwEdu, 47, TRUE), simplify = FALSE)
xvarstar <- sapply(resamps, var, simplify = TRUE)
hist(xvarstar)

var_boot=boot(SwEdu,statistic=var_fun,R=1000)
cis = boot.ci(var_boot,type=c("perc","norm","bca"))
cis
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 1000 bootstrap replicates
## 
## CALL : 
## boot.ci(boot.out = var_boot, type = c("perc", "norm", "bca"))
## 
## Intervals : 
## Level      Normal             Percentile            BCa          
## 95%   ( 14.81, 172.67 )   ( 32.04, 180.55 )   ( 42.55, 252.63 )  
## Calculations and Intervals on Original Scale
## Some BCa intervals may be unstable