R中的交叉验证步骤函数 [英] Cross Validating step functions in R

查看:354
本文介绍了R中的交叉验证步骤函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从步骤函数中获取错误,但出现错误:

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]

如果我使用特定的中断而不是自动生成cut()索引,则可以从cv.glm()$delta [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]'

即使这些是与cut(age,4)完全相同的突破.

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.

我的目标是尝试从12种不同的步骤模型中发现错误,并根据cv.glm()$delta错误选择最佳模型.

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.

推荐答案

问题是cut(age, i)仅作为glm()中的内联创建而存在,并且不属于您用于Wage数据集的一部分验证.我们可以这样解决:

The problem was that cut(age, i) existed only as an inline creation within your glm() and was not a part of the Wage data set you used for validation. We can fix that like this:

library(boot)
library(ISLR)
data(Wage) # using attach is a bad practice
set.seed(5082)
cv.error <- rep (0,12)
for (i in 2:13){
  Wage$tmp <- cut(Wage$age,i)
  step.fit = glm(wage~tmp, data = Wage)
  cv.error[i] <- cv.glm(Wage ,step.fit, K= 10)$delta [1]
}

cv.error

[1] 0.000 1733.815 1682.731 1637.200 1631.049 1623.069 1613.099 1600.413 1613.127 1603.581 1603.601 1604.730 1602.462

[1] 0.000 1733.815 1682.731 1637.200 1631.049 1623.069 1613.099 1600.413 1613.127 1603.581 1603.601 1604.730 1602.462

请注意,第一个值为0只是因为i的值从2开始,所以没有任何内容写入第一个元素.

Note that the first value is 0 only because the values of i start at 2 so nothing was ever written to the first element.

这篇关于R中的交叉验证步骤函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆