Sampling with replacement for bootstrapping [R]

Viewed 173

I am relatively new to R, so I am just trying to see what I am missing. I have a few questions about the process of doing this correctly.

My sample space is {2,4,9,12}. I need to verify that the mean of this sample is 6.75, which it is if I input

s <- c(2,4,9,12)
mean(s)

I also need to calculate the variance of s, but when I calculate the variance var(s) = 20.91667. The book I am reading, Statistical Inference, Casella & Berger, shows that the variance is 3.94. Now, I know since I am bootstrapping there is a different method, where instead of the sample variance being divided by (n-1), it is divided by ((n^n)-1) and it needs to be summed from i = 1 all they way up to (n^n). For easier understanding, screenshot of this formula will be posted here:

Bootstrap variance formula

Question 1

Is there a function implemented within R that finds the bootstrap mean and variance that can help me verify this answer? If so, can an example be provided on how to do so?

Question 2

For my own curiosity. Let's say that I wanted to bootstrap that sample space (not sure if that makes too much sense). How do I find the bootstrap mean and variance of a sample WITH replacement and loop that over for 10,000 samples?

Bear with me on this, because like I had mentioned, I am new to R and this concept of bootstrapping. Please correct me if my thought process is not exactly right. So, for example, when I input sample(c(2,4,9,12), replace = T, 10000), it gives me 10,000 elements, but I want 10,000 vectors of length of 4. In other words, when I input sample(c(2,4,9,12), replace = T, 1), it only gives one value, but I would like it to be a vector of 4 with any order of those four values WITH replacement. Now, once all 10,000 vectors (assuming that it is not the SAME vector repeated are created, I want to be able to find the bootstrap mean and variance of each vector. Once I have these means and variances, I want to be able to create a distribution graph to observe the CLT visually.

I am aware that my conceptual understanding of bootstrapping may be flawed, so please, I urge you to give me pointers to solidify my understanding.

Thank you ahead of time.

1 Answers

First thing's first: The variance of the sample mean and the variance of the sample are two different quantities. When you do var(s) and get 20.91, you're calculating the variance of the sample, not of the sample mean. So now, on to your questions:

I'm sure there are functions to run the bootstrap, but my advice would be to do it from scratch. It's only about 10 lines of code. Something like this might work:

s <- c(2,4,9,12)

N <- 10**5
sample_means <- numeric(N)

for (i in 1:N) {
    resample <- sample(s, replace = TRUE)
    sample_means[i] <- mean(resample)
}

mean(sample_means)
#> [1] 6.752375
var(sample_means)
#> [1] 3.941898

Created on 2021-08-18 by the reprex package (v2.0.1)

You could also use purrr for a more elegant solution (in my opinion, at least):

library(purrr)

s <- c(2,4,9,12)

N <- 10**5
sample_means <- rerun(
    .n = N,
    sample(s, replace = TRUE)
) %>%
    map_dbl(mean)

sample_means %>%
    mean()
#> [1] 6.75615

sample_means %>%
    var()
#> [1] 3.965604

Created on 2021-08-18 by the reprex package (v2.0.1)

Then, if you wanted to use the code above to make a histogram of the sample means, you'd just do

hist(sample_means)
Related