'object "i' not found in r forloop. k-fold cross validation

Like the tittle says, I am trying to do k-fold cross validation. my coding skills are very basic, please explain as simply as possible.

"""
library(ISLR) install.packages("ISLR") library(ISLR) install.packages("boot") library(boot)

data <- attach(read.csv("TutWk7Data-1.csv",header=TRUE))

MSE = NULL

for (i in 1:7){
  model = glm(Y~poly(X,i),data=data)
  MSE[i] = cv.glm(data,model,K=10)$delta[1]
}

""" I get this error Error in poly(X,i):object "i" not found



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source