0

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

  • Try re-running the command ```attach(data)```after ```MSE = NULL```, & then run your loop. Does this work? – Andy Apr 13 '22 at 08:52
  • Welcome to SO! Can you post a sample of your data (by copying/pasting the output of `dput(data )` for example) or create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Your code works well with the in-built dataset `mtcars`, so it is not possible to reproduce your error – Elia Apr 13 '22 at 08:53
  • The following objects are masked from data (pos = 3): X, Y The following objects are masked from read.csv("TutWk7Data-1.csv", header = TRUE) (pos = 4): X, Y The following objects are masked from data (pos = 5): X, Y The following objects are masked from read.csv("TutWk7Data-1.csv", header = TRUE) (pos = 6): X, Y The following objects are masked from read.csv("TutWk7Data-1.csv", header = TRUE) (pos = 7): X,Y ...@Andy this is what i get after running the attach(data) line but in the end I do get my 7 mse... should I be worried ? @Elia thank – Tintswalo Shivambe Apr 13 '22 at 09:08
  • also why do i get 7 mean square errors... should it not be (10x7) since every model has k=10 and I am modeling 7 models...or does r automatically show me the smallest mse for each of the seven models? – Tintswalo Shivambe Apr 13 '22 at 09:21
  • What is `attach` good for if you actually want to read data from a file? `attach` does not even return a data.frame, so `data` does not contian the data from the file. remove the call to `attach` and see if this solves your issue. – cdalitz Apr 13 '22 at 12:31

0 Answers0