# How to Calculate a Bootstrap Standard Error in R

Bootstrapping is a method that can be used to estimate the standard error of a mean.

The basic process for calculating a bootstrapped standard error is as follows:

• Take k repeated samples with replacement from a given dataset.
• For each sample, calculate the standard error: s/√n
• This results in k different estimates for the standard error. To find the bootstrapped standard error, take the mean of the k standard errors.

The following examples explain two different methods that can be used to calculate a bootstrapped standard error in R.

### Method 1: Use the Boot Package

One way to calculate a bootstrap standard error in R is to use the boot() function from the boot library.

The following code shows how to calculate a bootstrap standard error for a given dataset in R:

```#make this example reproducible
set.seed(10)

library(boot)

#define dataset
x <- c(12, 14, 14, 15, 18, 21, 25, 29, 32, 35)

#define function to calculate mean
meanFunc <- function(x,i){mean(x[i])}

#calculate standard error using 100 bootstrapped samples
boot(x, meanFunc, 100)

Bootstrap Statistics :
original  bias    std. error
t1*     21.5   0.254    2.379263```

The “original” value of 21.5 shows the mean of the original dataset. The “std. error” value of 2.379263 shows the bootstrap standard error of the mean.

Note that we used 100 bootstrapped samples to estimate the standard error of the mean in this example, but we could have used 1,000 or 10,000 or any number of bootstrapped samples we’ d like.

### Method 2: Write Your Own Formula

Another way to calculate a bootstrapped standard error is to write our own function.

The following code shows how to do so:

```#make this example reproducible
set.seed(10)

library(boot)

#define dataset
x <- c(12, 14, 14, 15, 18, 21, 25, 29, 32, 35)

mean(replicate(100, sd(sample(x, replace=T))/sqrt(length(x))))

[1] 2.497414
```

The bootstrapped standard error turns out to be 2.497414.

Notice that this standard error is quite similar to the one calculated in the previous example.

## One Reply to “How to Calculate a Bootstrap Standard Error in R”

1. Chong Li says:

Hi Zach!

I have a question regards on your two methods for bootstrapping. I found the first method is to calculate the SE for the 100 means from 100 replicates of bootstrapping. While the second method actually calculates the mean of the 100 SEs from 100 rounds of bootstrap. That’s why I use both methods to calculate the same data but didn’t get similar results. I think from the goal of the bootstrap standard error, the first one is the right method. Please let me know whether my thought is right. Thank you!