首页 > 解决方案 > 使用 cv.glm(在引导包中)和 train(在插入符号包中)的不同结果

问题描述

我想使用 R 中逻辑回归模型的留一交叉验证来获得测试错误。

我使用两种方法做到了这一点。1) 在引导包中使用 cv.glm 2) 使用 caret 包

使用 cv.glm 的结果和代码如下,

require(ISLR)
require(boot)
require(caret)

str(Smarket)
fit1 <- glm(Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Lag5 + Volume, 
            family = binomial, data = Smarket)

cv.glm(Smarket,fit1)$delta 


> cv.glm(Smarket,fit1)$delta
[1] 0.2518003 0.2517992

所以遗漏交叉验证误差约为 0.25

然后我使用了 caret 包。结果和使用插入符号的代码如下,

fitControlcv <- trainControl( method = "LOOCV")
mod_fit <- train(Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Lag5 + Volume,
                 data=Smarket, method="glm", family="binomial",
                 trControl = fitControlcv)

mod_fit


> mod_fit
Generalized Linear Model 

1250 samples
   6 predictor
   2 classes: 'Down', 'Up' 

No pre-processing
Resampling: Leave-One-Out Cross-Validation 
Summary of sample sizes: 1249, 1249, 1249, 1249, 1249, 1249, ... 
Resampling results:

  Accuracy  Kappa      
  0.4976    -0.02588095

在这种情况下,留一法交叉验证测试错误率约为 0.50 (1-0.4976)

我使用不同的软件包得到不同答案的原因可能是什么?

我在这里做错了吗?

如果有人可以帮助我,我真的很感激

标签: rcross-validationr-caret

解决方案


推荐阅读