R logistic回归的交叉验证
我有一些问题需要在R中对逻辑回归进行10倍交叉验证 我使用了cv.glm函数,但它显示了错误。然而,我对ISLR包中的Smarket数据使用了这个函数,它没有显示任何错误。我的逻辑回归中的预测因子是二元的R logistic回归的交叉验证,r,R,我有一些问题需要在R中对逻辑回归进行10倍交叉验证 我使用了cv.glm函数,但它显示了错误。然而,我对ISLR包中的Smarket数据使用了这个函数,它没有显示任何错误。我的逻辑回归中的预测因子是二元的 # 10-Fold Cross-Validation for Logistic Regression cv.errorlog7 <- cv.glm(p, logit7, K=10)$delta[1] 我遇到了一个非常类似的错误: > set.seed(100) > cv.
# 10-Fold Cross-Validation for Logistic Regression
cv.errorlog7 <- cv.glm(p, logit7, K=10)$delta[1]
我遇到了一个非常类似的错误:
> set.seed(100)
> cv.lm(data = catering1, form.lm = model, m=3) # 3 fold cross-validation
Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) :
factor Month has new levels July
# Reset seed
> set.seed(1000)
> cv.lm(data = catering1, form.lm = model, m=3) # 3 fold cross-validation
Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) :
factor Month has new levels July
正如你所看到的,我甚至重置了种子并再次尝试。不走运。然而,当我增加折叠时,我只是不断地增加1,直到我得到5的响应,代码才起作用。然而,我确实得到了一个错误和一个警告
> cv.lm(data = catering, form.lm = model, m=5) # 5 fold cross-validation
Response.... Anova table....
Error in which.min(xval) :
'list' object cannot be coerced to type 'double'
In addition: Warning message:
In cv.lm(data = catering, form.lm = model, m = 5) :
As there is >1 explanatory variable, cross-validation
predicted values for a fold are not a linear function
of corresponding overall predicted values. Lines that
are shown for the different folds are approximate
所以,我会尝试增加折叠。尤其是由于数据集相对较小,因此不会对性能造成太大影响。数据集有多大?如果你没有任何NA或第三个性别因素,听起来像是你的一个褶皱随机选择了一个性别。我会尝试一种新的种子进行交叉验证,或者减少折叠次数。如果您能提供reprex,那将非常有帮助。谢谢。我有143个主题和18个变量。数据不包含任何NA,我也没有使用性别来拟合逻辑回归。我试过换种子,但也犯了同样的错误。
> cv.lm(data = catering, form.lm = model, m=5) # 5 fold cross-validation
Response.... Anova table....
Error in which.min(xval) :
'list' object cannot be coerced to type 'double'
In addition: Warning message:
In cv.lm(data = catering, form.lm = model, m = 5) :
As there is >1 explanatory variable, cross-validation
predicted values for a fold are not a linear function
of corresponding overall predicted values. Lines that
are shown for the different folds are approximate