Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • test for normal distribution in r
    Test for Normal Distribution in R-Quick Guide R
  • How to Rank by Group in R?
    How to Rank by Group in R? R
  • How to Label Outliers in Boxplots in ggplot2
    How to Label Outliers in Boxplots in ggplot2? R
  • Subset rows based on their integer locations
    Subset rows based on their integer locations-slice in R R
  • Two-Way ANOVA Example in R
    How to perform a one-sample t-test in R? R
  • Arrange the rows in a specific sequence in R
    Arrange the rows in a specific sequence in R R
  • Convert multiple columns into a single column
    Convert multiple columns into a single column-tidyr Part4 R
  • How Do Online Criminals Acquire Sensitive Data
    How Do Online Criminals Acquire Sensitive Data Machine Learning
How to Perform Bootstrapping in R

How to Perform Bootstrapping in R

Posted on December 17December 17 By Jim No Comments on How to Perform Bootstrapping in R
Tweet
Share
Share
Pin

How to Perform Bootstrapping in R, Bootstrapping is a method for estimating the standard error of any statistic and generating a confidence interval for the statistic.

The basic bootstrapping procedure is as follows:

Take k repeated replacement samples from a given dataset.

Calculate the statistic of interest for each sample.

These yields k different estimates for a given statistic, which you can then use to calculate the statistic’s standard error and create a confidence interval.

We can perform bootstrapping in R by calling the following boot library functions:

1. Generate bootstrap samples.

boot(data, statistic, R, …)

where:

data: A vector, matrix, or data frame

statistic: A function that produces the statistic(s) to be bootstrapped

R: Number of bootstrap replicates

2. Create a confidence interval using the bootstrap method.

boot.ci(bootobject, conf, type)

where:

bootobject: An object returned by the boot() function

conf: The confidence interval to be computed. The default value is 0.95.

type: The type of confidence interval to compute. Options include “norm”, “basic”, “stud”, “perc”, “bca” and “all” – Default is “all”

The examples below demonstrate how to use these functions in practice.

How to test the significance of a mediation effect (datasciencetut.com)

Bootstrapping a Single Statistic

The code below demonstrates how to compute the standard error for the R-squared of a simple linear regression model:

set.seed(123)
library(boot)

Now we can define a function to calculate R-squared

rsq_function <- function(formula, data, indices) {
  d <- data[indices,] #allows boot to select sample
  fit <- lm(formula, data=d)
  return(summary(fit)$r.square)
}

Let’s perform bootstrapping with 3000 replications

reps <- boot(data=mtcars, statistic=rsq_function, R=3000, formula=mpg~disp)

Ready to view the results of bootstrapping

How to Analyze Likert Scale Data? – Data Science Tutorials

reps
ORDINARY NONPARAMETRIC BOOTSTRAP
Call:
boot(data = mtcars, statistic = rsq_function, R = 3000, formula = mpg ~
    disp)
Bootstrap Statistics :
     original      bias    std. error
t1* 0.7183433 0.003027851  0.06410851

We can see from the results:

This regression model’s estimated R-squared is 0.7183433.

This estimate has a standard error of 0.06513426.

We can also quickly see the distribution of the bootstrapped samples:

Similarity Measure Between Two Populations-Brunner Munzel Test – Data Science Tutorials

plot(reps)

We can also use the following code to compute the 95% confidence interval for the model’s estimated R-squared:

Adjusted bootstrap percentile (BCa) interval calculation

boot.ci(reps, type="bca")
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 3000 bootstrap replicates
CALL :
boot.ci(boot.out = reps, type = "bca")
Intervals :
Level       BCa         
95%   ( 0.5474,  0.8160 ) 

We can see from the output that the 95% bootstrapped confidence interval for the true R-squared values is (.5350, .8188).

How to Use Italic Font in R – Data Science Tutorials

Tweet
Share
Share
Pin
R

Post navigation

Previous Post: Error in solve.default(mat) :  Lapack routine dgesv: system is exactly singular: U[2,2] = 0
Next Post: Credit Card Fraud Detection in R

Related Posts

  • Detecting and Dealing with Outliers
    Detecting and Dealing with Outliers: First Step R
  • droplevels in R with examples
    droplevels in R with examples R
  • Extract patterns in R
    Extract patterns in R? R
  • Augmented Dickey-Fuller Test in R
    Augmented Dickey-Fuller Test in R R
  • Find the Maximum Value by Group in R
    Find the Maximum Value by Group in R R
  • How to Calculate Lag by Group in R
    How to Calculate Lag by Group in R? R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Defensive Programming Strategies in R
  • Plot categorical data in R
  • Top Data Modeling Tools for 2023
  • Ogive Graph in R
  • Is R or Python Better for Data Science in Bangalore

Check your inbox or spam folder to confirm your subscription.

  • Data Scientist Career Path Map in Finance
  • Is Python the ideal language for machine learning
  • Convert character string to name class object
  • How to play sound at end of R Script
  • Pattern Searching in R
  • How to put margins on tables or arrays in R?
    How to put margins on tables or arrays in R? R
  • Gamma distribution in R
    Gamma distribution in R R
  • Error in solve.default(mat)  Lapack routine dgesv system is exactly singular
    Error in solve.default(mat) :  Lapack routine dgesv: system is exactly singular: U[2,2] = 0 R
  • How to use image function in R
    How to use the image function in R R
  • Box Cox transformation in R
    Box Cox transformation in R R
  • Hypothesis Testing in R
    Hypothesis Testing in R R
  • Get the first value in each group in R
    Get the first value in each group in R? R
  • How to Join Data Frames for different column names in R
    How to Join Data Frames for different column names in R R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme