Bootstrap

Feb 4, 2013

Assume you have the following sample:

set.seed(123)
n <- 35
smp <- rnorm(n,mean=0,sd=3);    # hence the true variance is 3^2=9.
smp
##  [1] -1.6814 -0.6905  4.6761  0.2115  0.3879  5.1452  1.3827 -3.7952
##  [9] -2.0606 -1.3370  3.6722  1.0794  1.2023  0.3320 -1.6675  5.3607
## [17]  1.4936 -5.8999  2.1041 -1.4184 -3.2035 -0.6539 -3.0780 -2.1867
## [25] -1.8751 -5.0601  2.5134  0.4601 -3.4144  3.7614  1.2794 -0.8852
## [33]  2.6854  2.6344  2.4647

Now, take this as the sample you collcet. Assume you don't know neither of mean and variance and you want to estimate them.

muhat <- mean(smp)
muhat
## [1] 0.1126
varhat <- var(smp)
varhat
## [1] 8.059

After you have the estimates, you want to see how precision those estimates are. You will apply the Bootstrap as follows.

B <- 200
mean.save <- var.save <- rep(0, B)

for (i in 1:B) {
    work.index <- sample(1:n, n, replace = TRUE)
    work.sample <- smp[work.index]
    mean.save[i] <- mean(work.sample)
    var.save[i] <- var(work.sample)
}

Now the (estimated) standard error for sample mean is:

se.muhat <- sd(mean.save)
se.muhat
## [1] 0.4427

We can compare with the number we get by formula that \[ \hat{\sigma}^2_{\bar{X}} = S^2/n \]

sqrt(varhat/n)
## [1] 0.4798

And the (estimated) standard error for sample variance is:

se.varhat <- sd(var.save)
se.varhat
## [1] 1.626