This problem is to be done in R. When computing the bootstrap, there are two extremes
we talked about in class: either completely enumerating all of the possible cases (which is computationally
infeasible most of the time), or drawing of a few samples at random with possibility of
repetition. In between these is an algorithm known as the Balanced Bootstrap. The algorithm goes
as follows:
Generate a list of B repetitions of each of the observations in our original data set, x1; ; xn,
that is, x1; ; x1 (B times), x2; ; xx (again, B times), and so on to xn; ; xn (B times). This
is a total of nB elements.
Permute this list completely at random.
Take the first n elements as bootstrap replicate 1, the second n elements as bootstrap replicate 2,
and so on, obtaining B bootstrap replicates.
For this question, implement a function balanced boot which takes two arguments: dat (for
the inputs) and B (for the number of replicates). In this function, implement a Balanced Bootstrap
estimate of the mean. Return the set of replicates.
Test this function using n = 15 and n = 30 samples drawn from a standard normal (N(0; 1)) and
estimate the standard error in both cases. Compare these quantities to the theoretical values they are
estimating.
R Program
set.seed(1423)
balanceBoot <- function(dat,B){
n <-length(dat)
bootIndex <- rep(seq(1:n),B)
bootIndexR <-sample(bootIndex)
bootSample <-dat[bootIndexR]
bootReplica <- matrix(bootSample,nrow=n)
bootMean <- colMeans(bootReplica)
bootSE <- sd(bootMean)
return(list(Replicates=bootReplica,Means=bootMean,StdError=bootSE))
}
boot15 <-balanceBoot(rnorm(15),10000)
boot30 <-balanceBoot(rnorm(30),10000)
Theoretical Std error is
theorySE30<-1/sqrt(30)
theorySE15 <-1/sqrt(15)
> boot15$StdError
[1] 0.2137169
> theorySE15
[1] 0.2581989
> boot30$StdError
[1] 0.1833562
> theorySE30
[1] 0.1825742
Get Answers For Free
Most questions answered within 1 hours.