I am trying to get errors from step functions but I get an error :
library(boot)
library(ISLR)
attach(Wage)
set.seed(5082)
cv.error <- rep (0,12)
for (i in 2:13){
step.fit = glm(wage~cut(age,i), data = Wage)
cv.error[i] <- cv.glm(Wage ,step.fit, K= 10)$delta [1]
}
Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) :
cut(age, i) has new levels (17.9,43.5], (43.5,69.1]
I can get the error from cv.glm()$delta [1]
if instead of auto generating the cut()
index i use specific breaks:
fit <- glm(wage~cut(age,breaks=c(17.9,33.5,49,64.5,80.1)), data = Wage)
cv.error <- cv.glm(Wage ,step.fit, K= 10)$delta [1]'
Even though these are the exact same breaks cut(age,4)
makes.
Can anyone explain what is going on or how to fix the error.
My goal is to try to find errors from 12 different step models and pick the best one based on the cv.glm()$delta
error.