Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object "i' not found within an R for loop (k-fold cross validation)

Tags:

r

k-fold

Like the title 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
like image 410
Tintswalo Shivambe Avatar asked Oct 25 '25 04:10

Tintswalo Shivambe


1 Answers

The problem arises from your use of attach. According to the help page for attach,

The environment is returned invisibly with a "name" attribute.

You stored that environment in the data object. On the other hand, the help page for cv.glm states the "data" argument must be a "matrix or data frame containing the data." You can check this at run time:

> inherits(data, "data.frame")
[1] FALSE

Because data is not a data frame (even though it can behave remarkably like one!), all bets are off when you pass it to cv.glm.

As remarked by a commenter, delete the attach call: it's superfluous (and has invisible side-effects anyway). Here is a working example of what you likely intended:

library(ISLR)
library(boot)
data <- data.frame(X = seq_len(100), Y = rnorm(100)) # Reproducible example

MSE <- sapply(seq_len(7), \(i) {
  model <- glm(Y ~ poly(X,i), data = data)
  cv.glm(data, model, K = 2)$delta[1]
})
like image 170
whuber Avatar answered Oct 26 '25 16:10

whuber