Open In App

How to Calculate a Bootstrap Standard Error in R?

Last Updated : 04 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will be looking at the different approaches to calculate a bootstrap standard error using various packages and their functionalities in the R programming language.

Bootstrap Standard Error:

The standard deviation of the bootstrap samples (also known as the bootstrap standard error) is an estimate of the standard deviation of the sampling distribution of the mean.

Steps to calculate the bootstrap standard error of given data:

  • 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.

Method 1: Using the boot() function from the boot package

In this method, the user first needs to install and import the boot package in the working R console, then the user needs to call the boot() function with the required data passed into it as its parameter, which will further return the Bootstrap Standard Error of the given data in R programming language.

Syntax to install and import the boot package:

install.packages('boot')
library('boot')

Boot() function: This function provides a simple front-end to the boot function in the boot package that is tailored to bootstrapping based on regression models. 

Syntax: Boot(object, f=coef, labels=names(f(object)),R=999, …)

Parameters:

  • object: An object of the class.
  • f: A function whose one argument is the name of a regression object that will be applied to the updated regression object to compute the statistics of interest.
  • labels: Provides labels for the statistics computed by f.
  • R: Number of bootstrap samples.

Example:

In this example, we will be using the boot() function from the boot package to the bootstrap standard error of the vector containing 10 elements and using 100 bootstrapped samples in the R programming language.

R




library(boot)
  
# Create data
gfg <- c(244,753,596,645,874,141,639,465,999,654)
  
# Calculating the mean 
m <- function(gfg,i){mean(gfg[i])}
  
# Calculate standard error using 100
# bootstrapped samples
boot(gfg, m, 100)


Output:

ORDINARY NONPARAMETRIC BOOTSTRAP


Call:
boot(data = gfg, statistic = m, R = 100)


Bootstrap Statistics :
    original  bias    std. error
t1*      601  -3.231    80.02706

Method 2: Calculating Bootstrap Standard Error using the formula

In this method to calculate the bootstrap standard error, the user needs to use the direct formula to get the same, simply without any use of any packages in the R programming language.

Example:

In this example, we will be using the direct formula to get the bootstrap standard error of the vector containing 10 elements in the R programming language.

R




# Create data
gfg <- c(244,753,596,645,874,141,639,465,999,654)
  
# Calculating the bootstrap standard error 
mean(replicate(100, sd(sample(
  gfg, replace=T))/sqrt(length(gfg))))


Output:

[1] 78.53055


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads