shrinkage<-function(fit,k=10){
require(bootstrap)
theta.fit<-function(x,y){lsfit(x,y)}
theta.predict<-function(fit,x){cbind(1,x)%*%fit$coef}
x<-fit$model[,2:ncol(fit$model)]
y<-fit$model[,1]
results<-crossval(x,y,theta.fit,theta.predict,ngroup=k)
r2<-cor(y,fit$fitted.values)^2
r2cv<-cor(y,results$cv.fit)^2
cat("Original R-square=",r2,"\n")
cat(k,"Fold Cross-Validated R-square=",r2cv,"\n")
cat("Change=",r2-r2cv,"\n")
}
fit1<-lm(Murder~Population+Income+Illiteracy+Frost,data=states)
library(bootstrap)
shrinkage(fit1)
> Original R-square= 0.5669502
> 10 Fold Cross-Validated R-square= 0.4174945
> Change= 0.1494558
fit2<-lm(Murder~Population+Illiteracy,data=states)
library(bootstrap)
shrinkage(fit2)
> Original R-square= 0.5668327
> 10 Fold Cross-Validated R-square= 0.5193488
> Change= 0.04748383