Problem during cross-validation

Hi all,

I don't understand what's wrong with this code. I'm trying to create a vector with the test MSE values for 5 degrees of polynomial linear function when I am getting this error. It should be quite straightforward, but maybe there's something wrong with the loop itself. I don;t understand why it says it can't find the model. Any help will be appreciated

library(ISLR)
library(boot)
#> Warning: package 'boot' was built under R version 3.5.2
cv_error_5=rep(0,5)
for (i in 1:10) {linear_model=glm(mpg ~ poly(horsepower, i), data=Auto) + cv_error_5[i]=cv.glm(data=Auto, linear_model)$delta[1]}
#> Error in cv.glm(data = Auto, linear_model): object 'linear_model' not found

Created on 2019-10-12 by the reprex package (v0.3.0)

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.

Thanks! When I saw the code in the paper that I was reading it had the + between the two functions, which is the code showing up in the console when being run. When doing it from the script without the +, works beautifully. Thanks again