Stock Portfolio example: * alpha represents the fraction of funds invested in X (and thus 1-alpha invested in Y). * The variance of the total investment, var(alpha*X+(1-alpha)*Y), is minimized at alpha = [var(Y)-cov(X,Y)]/[var(X)+var(Y)-2*cov(X,Y)] * Estimating alpha is simple, but an estimate of SE(alpha) is difficult without a resampling procedure such as the bootstrap. #================================================= #excerpt from ISLR_ALL-Labs.txt Chapter 5 (with additions) #The Bootstrap library(ISLR) library(boot) #SET RandomNumberGenerator TYPE TO PRE-3.6.0 DEFAULT IN ORDER TO MATCH TEXT RESULTS RNGversion('3.5.3') alpha.fn=function(data,index){ X=data$X[index] Y=data$Y[index] return((var(Y)-cov(X,Y))/(var(X)+var(Y)-2*cov(X,Y))) } dim(Portfolio) alpha.fn(Portfolio,1:100) #0.576 set.seed(1) alpha.fn(Portfolio,sample(100,100,replace=T)) #0.596 boot(Portfolio,alpha.fn,R=1000) #------------------------------------------------- set.seed(2) temp = boot(Portfolio,alpha.fn,R=1000) print(temp) attributes(temp) alpha.fn(Portfolio,1:100) temp$t0 #mean of original data hist(temp$t) #histogram of Bootstrap replicates mean(temp$t) #Bootstrap estimate (mean of Bootstrap samples) mean(temp$t) - temp$t0 #Bias hist(temp$t) text(temp$t0,0,"X") #place an "X" at the original mean #------------------------------------------------- # Estimating the Accuracy of a Linear Regression Model data(Auto) boot.fn=function(data,index) return(coef(lm(mpg~horsepower,data=data,subset=index))) boot.fn(Auto,1:392) set.seed(1) boot.fn(Auto,sample(392,392,replace=T)) boot.fn(Auto,sample(392,392,replace=T)) boot(Auto,boot.fn,1000) summary(lm(mpg~horsepower,data=Auto))$coef boot(Auto,boot.fn,1000) boot(Auto,boot.fn,1000) boot.fn=function(data,index) coefficients(lm(mpg~horsepower+I(horsepower^2),data=data,subset=index)) set.seed(1) boot(Auto,boot.fn,1000) mod=lm(mpg~horsepower+I(horsepower^2),data=Auto) summary(mod)$coef #=================================================