Hello,

I'm studying bootstrapping.

I devised a code that draw a sample of heights, and then compute the 
sample mean of those heights.

I want to compute the variance of the sample mean by bootstrapping.

I am comparing this with the "real" variance of the sample meanand with 
an "estimated" variance of the sample mean.

Is my code correct? The results looks very similar in all three cases.

I just want to be sure that I made everything right.

Thanks in advance!

#!/usr/bin/env R

its <- 10000
mean <- 180
sd <- 50
N <- 10000

heights <- rnorm(N, mean, sd)

sample.mean <- mean(heights)

print(paste0("Real mean: ", mean))
print(paste0("Sample mean: ", sample.mean))

# unbiased sample variance
# the var() function already uses the unbiased estimator
#sample.var <- (N/(N-1))*var(heights)
sample.var <- var(heights)

mean.var.real <- (sd^2)/N
print(paste0("Actual variance of the sample mean: ", mean.var.real))

mean.var.sample <- sample.var/N
print(paste0("Estimated variance of the sample mean: ", mean.var.sample))

# Compute variance by bootstrap
boot.means <- rep(NA, its)
for(it in 1:its) {
   boot.h <- sample(heights, N, replace=T)
   boot.means[it] <- mean(boot.h)
}

mean.var.boot <- var(boot.means)
print(paste0("Bootstrapped variance of the sample mean: ", mean.var.boot))


        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to