[R] Variance of the sample mean by bootstrapping
thebudget72 m@iii@g oii gm@ii@com
thebudget72 m@iii@g oii gm@ii@com
Thu Apr 8 18:10:16 CEST 2021
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]]
More information about the R-help
mailing list