require(ISLR)
## Loading required package: ISLR
## Warning: package 'ISLR' was built under R version 3.1.2
require(boot)
## Loading required package: boot
## Warning: package 'boot' was built under R version 3.1.2
?cv.glm
## starting httpd help server ... done
plot(mpg~horsepower,data=Auto)
glm.fit<- span=""> glm(mpg~horsepower,data=Auto)
cv.glm(Auto,glm.fit)$delta ->
## [1] 24.23151 24.23114
loocv<- span=""> function(fit){
h<- span=""> lm.influence(fit)$h
mean((residuals(fit)/(1-h))^2)
}
loocv(glm.fit)->->
## [1] 24.23151
cv.error<- span=""> rep(0,5)
degree<- span=""> 1:5
for(d in degree){
glm.fit<- span=""> glm(mpg~poly(horsepower,d),data=Auto)
cv.error[d]<- span=""> loocv(glm.fit)
}
plot(degree,cv.error,type='b')
cv.error10<- span=""> rep(0,5)
for(d in degree){
glm.fit<- span=""> glm(mpg~poly(horsepower,d),data=Auto)
cv.error10[d]<- span=""> cv.glm(Auto,glm.fit,K=10)$delta[1]
}
lines(degree,cv.error10,type='b',col='red')->->->->->->->
alpha<- span=""> function(x,y){
vx<- span=""> var(x)
vy<- span=""> var(y)
cxy<- span=""> cov(x,y)
(vy-cxy)/(vx+vy-2+cxy)
}
alpha(Portfolio$X,Portfolio$Y)->->->->
## [1] 0.6413231
alpha.fn<- span=""> function(data,index){
with(data[index,],alpha(X,Y))
}
alpha.fn(Portfolio,1:100)->
## [1] 0.6413231
set.seed(1)
alpha.fn(Portfolio,sample(1:100,100,replace=T))
## [1] 0.6727862
boot.out<- span=""> boot(Portfolio,alpha.fn,R=1000)
boot.out->
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = Portfolio, statistic = alpha.fn, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 0.6413231 0.08995096 0.4226578
plot(boot.out)