The code inside the for loop is:
linear_model=glm(mpg ~ poly(horsepower, i), data=Auto) + cv_error_5[i]=cv.glm(data=Auto, linear_model)$delta[1]
But it doesn't make sense to "add" these two operations using +, which is causing the error. You can reproduce this if you run the following specific case in a clean R session:
library(ISLR)
library(boot)
linear_model = glm(mpg ~ poly(horsepower, 2), data=Auto) + cv_error_5=cv.glm(data=Auto, linear_model)$delta[1]
Error in cv.glm(data = Auto, linear_model) :
object 'linear_model' not found
It looks like the code should be the following:
cv_error_5=rep(NA, 5)
for (i in 1:5) {
linear_model=glm(mpg ~ poly(horsepower, i), data=Auto)
cv_error_5[i]=cv.glm(data=Auto, linear_model)$delta[1]
}
cv_error_5
[1] 24.23151 19.24821 19.33498 19.42443 19.03321
Note that the loop goes from 1 to 5, rather than 1 to 10, to conform to the length of cv_error_5 and run models up to a 5th degree polynomial fit.