Bias Variance Trade-Off

Fit various polynomial models to compute mpg as a function of the other four variables acceleration, weight, horsepower, and displacement using glm function.

#load the data
data <- suppressWarnings(read.table("https://raw.githubusercontent.com/RobertSellers/dataWarehouse/master/auto-mpg.data"))
names(data) <- c("mpg", "displacement", "horsepower","weight","acceleration")
#Requiring libraries
suppressWarnings(require(boot))
suppressWarnings(require(stats))
set.seed(9)
cv.err5 <- c()
for(i in 1:9){
  model<-glm(mpg~poly(displacement+horsepower+weight+acceleration,i), data=data)
  cv.err5[i]<-cv.glm(data,model,K=5)$delta[1]
}

Plot the crossvalidation error function.

plot(1:9,cv.err5, type='b',xlab = "Polynomial Degree", ylab = "Cross Validation Error", main = "Bias Variance Trade-Off")