# File "Rboot3". B = 1000 n = 30 g = 5 truncmean = function(x) { len = length(x) ord = order(x) s = 0 for (ii in (g+1):(len-g)) s = s + x[ord[ii]] return(s/(len-2*g)) } # Generate some data. x = runif(n) thetahatlist = NULL # Run the bootstrap, with B resamplings of n resamples each. for (b in 1:B) { # Do n bootstrap resamples. resampled = rep(0,n) for (i in 1:n) { j = floor( runif(1,1,n+1) ) # uniform on {1,2,...,n} resampled[i] = x[j] } # Compute thetahat for this resample. thetahat = truncmean(resampled) thetahatlist = c(thetahatlist, thetahat) } # Compute the bootstrap estimate of mean, thetastar. thetastar = sum(thetahatlist) / B # Compute the bootstrap estimate of variance. sqsum = 0 for (i in 1:B) { sqsum = sqsum + (thetahatlist[i] - thetastar)^2 } varest = sqsum / (B-1) print(varest)