The Wayback Machine - https://web.archive.org/web/20240720234040/https://www.geeksforgeeks.org/bootstrap-confidence-interval-with-r-programming/
Open In App

Bootstrap Confidence Interval with R Programming

Last Updated : 10 Jul, 2021
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Bootstrapping is a statistical method for inference about a population using sample data. It can be used to estimate the confidence interval(CI) by drawing samples with replacement from sample data. Bootstrapping can be used to assign CI to various statistics that have no closed-form or complicated solutions. Suppose we want to obtain a 95% confidence interval using bootstrap resampling the steps are as follows: 

  1. Sample n elements with replacement from original sample data.
  2. For every sample calculate the desired statistic eg. mean, median etc.
  3. Repeat steps 1 and 2 m times and save the calculated stats.
  4. Plot the calculated stats which forms the bootstrap distribution
  5. Using the bootstrap distribution of desired stat we can calculate the 95% CI

Illustration of the bootstrap distribution generation from sample: 

Bootstrap generation process

Implementation in R

In R Programming the package boot allows a user to easily generate bootstrap samples of virtually any statistic that we can calculate. We can generate estimates of bias, bootstrap confidence intervals, or plots of bootstrap distribution from the calculated from the boot package.

For demonstration purposes, we are going to use the iris dataset due to its simplicity and availability as one of the built-in datasets in R. The data set consists of 50 samples from each of the three species of Iris (Iris setosa, Iris Virginia, and Iris versicolor). Four features were measured from each sample: the length and the width of the sepals and petals, in centimeters. We can view the iris dataset using head command and note the features of interests. 

R




# View the first row
# of the iris dataset
head(iris, 1)


Output: 

 Sepal.Length    Sepal.Width    Petal.Length    Petal.Width    Species
     5.1             3.5              1.4             0.2      setosa

We want to estimate the correlation between Petal Length and Petal Width. 

Steps to Compute the Bootstrap CI in R:

1. Import the boot library for calculation of bootstrap CI and ggplot2 for plotting. 
 

R




# Import library for bootstrap methods
library(boot)
 
# Import library for plotting
library(ggplot2)


2. Create a function that computes the statistic we want to use such as mean, median, correlation, etc. 

R




# Custom function to find correlation
# between the Petal Length and Width
corr.fun <- function(data, idx)
{
  df <- data[idx, ]
 
  # Find the spearman correlation between
  # the 3rd and 4th columns of dataset
  c(cor(df[, 3], df[, 4], method = 'spearman'))
}


3. Using the boot function to find the R bootstrap of the statistic. 

R




# Setting the seed for
# reproducability of results
set.seed(42)
 
# Calling the boot function with the dataset
# our function and no. of rounds
bootstrap <- boot(iris, corr.fun, R = 1000)
 
# Display the result of boot function
bootstrap


Output: 

ORDINARY NONPARAMETRIC BOOTSTRAP


Call:
boot(data = iris, statistic = corr.fun, R = 1000)


Bootstrap Statistics :
     original       bias    std. error
t1* 0.9376668 -0.002717295 0.009436212

4. We can plot the generated bootstrap distribution using the plot command with calculated bootstrap. 

R




# Plot the bootstrap sampling
# distribution using ggplot
plot(bootstrap)


Output: 

output-plot

5. Using the boot.ci() function to get the confidence intervals. 

R




# Function to find the
# bootstrap Confidence Intervals
boot.ci(boot.out = bootstrap,
        type = c("norm", "basic",
                 "perc", "bca"))


Output: 

BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 1000 bootstrap replicates

CALL : 
boot.ci(boot.out = bootstrap, type = c("norm", "basic", "perc", 
    "bca"))

Intervals : 
Level      Normal              Basic         
95%   ( 0.9219,  0.9589 )   ( 0.9235,  0.9611 )  

Level     Percentile            BCa          
95%   ( 0.9142,  0.9519 )   ( 0.9178,  0.9535 )  
Calculations and Intervals on Original Scale

Inference for Bootstrap CI From the Output:

Looking at the Normal method interval of (0.9219, 0.9589) we can be 95% certain that the actual correlation between petal length and width lies in this interval 95% of the time. As we have seen the output consists of multiple CI using different methods according to the type parameter in function boot.ci. The computed intervals correspond to the (“norm”, “basic”, “perc”, “bca”) or Normal, Basic, Percentile, and BCa which give different intervals for the same level of 95%. The specific method to use for any variable depends on various factors such as its distribution, homoscedastic, bias, etc.

The 5 methods that boot package provides for bootstrap confidence intervals are summarized below:

  1. Normal bootstrap or Standard confidence limits methods use the standard deviation for calculation of CI. 
    • Use when statistic is unbiased.
    • Is normally distributed.
  2. Basic bootstrap or Hall’s (second percentile) method use percentile to calculate upper and lower limit of test statistic. 
    • When statistic is unbiased and homoscedastic.
    • The bootstrap statistic can be transformed to a standard normal distribution.
  3. Percentile bootstrap or Quantile-based, or Approximate intervals use quantiles eg 2.5%, 5% etc. to calculate the CI. 
    • Use when statistic is unbiased and homoscedastic.
    • The standard error of your bootstrap statistic and sample statistics are the same.
  4. BCa bootstrap or Bias Corrected Accelerated use percentile limits with bias correction and estimate acceleration coefficient corrects the limit and find the CI. 
    • The bootstrap statistic can be transformed to a normal distribution.
    • The normal-transformed statistic has a constant bias.
  5. Studentized bootstrap resamples the bootstrap sample to find a second-stage bootstrap statistic and use it to calculate the CI. 
    • Use when statistic is homoscedastic.
    • The standard error of bootstrap statistic can be estimated by second-stage resampling.

References : 
R bootstrap package Boot 
Bootstrapping Statistics Wikipedia 
Bootstrap for Confidence Interval
 



Previous Article
Next Article

Similar Reads

How to Plot a Confidence Interval in R?
In this article, we will discuss how to plot confidence intervals in the R programming language. Method 1: Plotting the confidence Interval using geom_point and geom_errorbar In this method to plot a confidence interval, the user needs to install and import the ggplot2 package in the working r console, here the ggplot2 package is responsible to plo
4 min read
How to Calculate a Binomial Confidence Interval in R?
In this article, we will discuss how to calculate a Binomial Confidence interval in R Programming Language. We can calculate Binomial Confidence Interval by using the below formulae: p +/- z*(√p(1-p) / n) where, p is for the proportion of successesz is the chosen valuen is the sample size We can calculate by using the below methods Method 1: Use th
2 min read
Draw Confidence Interval on Histogram with ggplot2 in R
A histogram is a graph that shows the distribution of a dataset. It can be used to estimate the probability distribution of a continuous variable, such as height or weight. To create a histogram, you need to divide the range of values into a series of intervals, called bins, and count the number of observations that fall into each bin. The bins sho
6 min read
Shading confidence intervals manually with ggplot2 in R
In this article, we will see how to generate Shading confidence intervals manually with ggplot2 in R Programming language. Let us first draw a regular curve and then add confidence intervals to it. Example: C/C++ Code # Load Packages library(&quot;ggplot2&quot;) # Create a DataFrame for Plotting DF &lt;- data.frame(X = rnorm(10), Y = rnorm(10)) # P
2 min read
Add Confidence Band to ggplot2 Plot in R
In this article, we will discuss how to add Add Confidence Band to ggplot2 Plot in the R programming Language. A confidence band is the lines on a scatter plot or fitted line plot that depict the upper and lower confidence bounds for all points on the range of data. This helps us visualize the error range for data whose exact values cannot be guara
3 min read
Plot Simultaneous and Pointwise Confidence Bands for Linear Regression
When performing linear regression analysis it can be useful to plot the confidence bands around the regression line to indicate the range of possible values for the response variable. There are two types of confidence bands that can be plotted: pointwise and simultaneous. Pointwise confidence bands indicate the uncertainty in the predicted response
3 min read
How to Find Confidence Intervals in R?
The confidence interval in R signifies how much uncertainty is present in statistical data. a fundamental statistical technique, confidence intervals offer a range of likely values for an unknown population parameter based on sample data. They are essential to decision-making, hypothesis testing, and statistical inference.In other words, it is defi
6 min read
Add confidence intervals to dotchart in R
In statistics, confidence intervals are a type of interval estimate used to provide an estimate of the range of values that a population parameter, such as a mean or proportion, is likely to fall within. These intervals are essential for interpreting the results of statistical analyses and providing a measure of uncertainty around point estimates.
9 min read
Search the Interval for Minimum and Maximum of the Function in R Programming - optimize() Function
optimize() or optimise() function in R Language is used to search the interval from lower to upper for a minimum or maximum of the function f with respect to its first argument. Syntax: optimize(f, interval, maximum)Parameters: f: the function to be optimized. The function is either minimized or maximized over its first argument depending on the va
2 min read
Calculate the Root of a Equation within an interval in R Programming - uniroot() Function
uniroot() function in R Language is used to calculate the root of an equation with the lower and upper range of the interval passed as arguments. Syntax: uniroot(fun, interval, lower, upper) Parameters: fun: function with the equation interval: with upper and lower range of root lower: lower end point of the interval upper: upper end point of the i
1 min read
three90RightbarBannerImg