Here is the video where I describe the code and the method.
As with most of the videos here, I am merely describing the application. You should understand why you want to bootstrap before you consider running this code (unless you just want to see a for loop in action).
Here is the code I used:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## ------------------------ ## | |
## Introduction to Loops ## | |
## and how to Bootstrap ## | |
## ------------------------ ## | |
set.seed(5151) ## Setting the Seed | |
## ---------- ## | |
## Read Data ## | |
## ---------- ## | |
ads.df = read.csv("C://R//ads.csv",header=T) | |
summary(ads.df) | |
N = length(ads.df[,1]) # Counts the number of observations | |
B = 200 # Number of times to recompute estimate (user specified) | |
## Sample with replacement ## | |
sample(1:10, 7, replace=TRUE) | |
## Bootstrap sample with replacement ## | |
sample(1:10, 10, replace=TRUE) | |
## --------------------------------- ## | |
## Bootstrap sample with replacement ## | |
## for this data set ## | |
## --------------------------------- ## | |
idx = sample(1:N, N, replace = TRUE) | |
newdata.df = ads.df[idx,] ## Bootstrap Data | |
## --------------------------------- ## | |
## Calculations with Bootstrap Data ## | |
## --------------------------------- ## | |
clicks.lm = lm(ClicksPPC~ImpressionsPPC, data=ads.df) ## Clicks = a + b*Impressions | |
clicks.lm2 = lm(ClicksPPC~ImpressionsPPC, data=newdata.df) ## Clicks = a + b*Impressions | |
stor.r2 = rep(0,B) ## Storage Vector | |
## The Loop that does the bootstrap on R-squared ## | |
for(i in 1:B){ | |
idx = sample(1:N, N, replace = TRUE) | |
newdata.df = ads.df[idx,] | |
clicks.boot = lm(ClicksPPC~ImpressionsPPC, data=newdata.df) ## Clicks = a + b*Impressions | |
stor.r2[i] = summary(clicks.boot)$r.squared | |
} | |
hist(stor.r2) ## Plot the histogram. |
I am not going to post the data on this one, but the code should be easy to modify to work with any data set you use.