【问题标题】:Regarding the Caret package in R when apply K fold cross validation关于应用 K 折交叉验证时 R 中的 Caret 包
【发布时间】:2019-12-17 16:52:49
【问题描述】:

我已经使用 R 中的 caret 包拟合了一个逻辑回归模型(使用 ISLR 包中的 Smarket 数据集)。然后我使用(总体测试误差)K 折交叉验证(K=5)计算了总的误分类误差为接下来,

require(ISLR)
require(caret)
fitControl <- trainControl(method = "cv",number = 5)
mod_fit <- train(Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Lag5 + Volume,
                 data=Smarket, method="glm",trControl = fitControlcv)

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

从上面的输出中,我能够计算出总的未命中分类错误,因为,

总未命中分类=1- 准确度。

有没有什么方法可以使用 K 折交叉验证从插入符号包中提取敏感性和特异性(类特定错误)?

我能够通过创建用户定义的函数来计算 K 折交叉验证中的敏感性和特异性:https://youtu.be/AFg2MvhFeho

但我想知道是否可以使用 caret 包轻松完成。

谢谢

【问题讨论】:

  • 你去过rdrr.io/cran/caret/man/sensitivity.html>吗?

标签: r cross-validation r-caret


【解决方案1】:

可以通过如下交叉表的观察值和预测值来完成,

table((mod_fit$pred)$obs,(mod_fit$pred)$pred)
      Down  Up
  Down  125 477
  Up    151 497

所以

overall missclassification = (125+497)/250 = 0.4976

sensitivity =  497/(151+497) = 0.7770 

【讨论】:

    【解决方案2】:

    您是否尝试过使用

    confusionMatrix(data = predictions, reference = observations)
    

    应该给你你正在寻找的东西和更多。您还可以查看更多详情here

    【讨论】:

    • 据我所知,如果您应用了 k 折交叉验证,这将无法直接工作。
    猜你喜欢
    • 2016-02-01
    • 2019-12-18
    • 2018-08-29
    • 2016-11-15
    • 2020-11-30
    • 2016-02-17
    • 2016-01-15
    • 2017-06-09
    • 1970-01-01
    相关资源
    最近更新 更多