## ------------------------ ##

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