### Code for Bootstrapping lecture ### uses the package 'boot' library(boot) help(boot) ###Bootstrap the slope of a regression line using code ###import the same data datum=read.csv(file.choose()) ### use example data from lecture 36 (first bootstap lecture) head(datum) results=lm(Y~X,data=datum) ### Run the Regression summary(results) coef(results)[2] ### Extract the Slopes SlopeTracker=rep(0,1000) #### Create a list to track the slope estimates SlopeTracker length(datum$X) ### Determine the size of the original dataset x=(1:100) ### Create a list of numbers equal to the size of the # original dataset x Test=sample(x,100,replace=TRUE) ### resample from x with replacement ### Creates a list of new sammples to include Test datumBoot=datum[Test,] ###Creates the new dataset datumBoot resultsTest=lm(Y~X,data=datumBoot) #ReRun the regression with the new dataset summary(resultsTest) coef(resultsTest)[2] ### Extract the new slope #### Now repeat the above 1000 times set.seed(123) #run this code to make sure results are always the same for(i in 1:1000){ Test=sample(x,100,replace=TRUE) ### resample from x with replacement datumBoot=datum[Test,] ###Creates the new dataset resultsTest=lm(Y~X,data=datumBoot) #ReRun the regression with the new dataset SlopeTracker[i]=coef(resultsTest)[2] ### Record new slope in SlopeTracker } SlopeTracker=sort(SlopeTracker) ### Sort the list of estimates SlopeTracker[25] ###Lower Confidence Limit SlopeTracker[975] ### Upper Confidence Limit ### Compare to confint from regression confint(results) #25th and 975th estimate were used because these give 95% C.I. ######Bootstrap the slope of a regression line using the 'boot function' ########################################################################## ### import the data datum=read.csv(file.choose()) ### Calculate results using standard parametric tests results=lm(Y~X,data=datum) summary(results) confint(results) # Create Function that calculates slope of bootstrapped sample lmFunc=function(bootData,repeats){ ### requires bootstrapped data-matrix and vector of numbers results=lm(Y~X,bootData[repeats,]) ### run regression beta=coef(results)[2] ###extract slope (intercept is first coefficient) return(beta) } boot.results=boot(datum,lmFunc,R=1000) boot.results boot.ci(boot.results) boot.results$t ### prints the vector of bootstrapped parameter values (not sorted) ### compare to parametric results results