我在R中使用插入符号进行逻辑回归:
ctrl <- trainControl(method = "repeatedcv", number = 10, repeats = 10,
savePredictions = TRUE)
mod_fit <- train(Y ~ ., data=df, method="glm", family="binomial",
trControl = ctrl)
print(mod_fit)
打印的默认指标是准确度和Cohen kappa . 我想提取匹配的指标,如敏感性,特异性,阳性预测值等,但我找不到一个简单的方法来做到这一点 . 提供了最终的模型,但它对所有数据进行了训练(据我从文档中可以看出),所以我不能用它来重新预测 .
混淆矩阵计算所有必需参数,但将其作为汇总函数传递不起作用:
ctrl <- trainControl(method = "repeatedcv", number = 10, repeats = 10,
savePredictions = TRUE, summaryFunction = confusionMatrix)
mod_fit <- train(Y ~ ., data=df, method="glm", family="binomial",
trControl = ctrl)
Error: `data` and `reference` should be factors with the same levels.
13.
stop("`data` and `reference` should be factors with the same levels.",
call. = FALSE)
12.
confusionMatrix.default(testOutput, lev, method)
11.
ctrl$summaryFunction(testOutput, lev, method)
除了准确性和kappa之外,有没有办法提取这些信息,或者以某种方式在插入符号列车返回的train_object中找到它?
提前致谢!
1 回答
Caret已经有汇总功能来输出你提到的所有指标:
defaultSummary
输出准确度和KappatwoClassSummary
输出AUC(ROC曲线下面积 - 见最后一行答案),灵敏度和特异性prSummary
输出精度和召回率为了获得组合的指标,你可以编写自己的汇总函数,它结合了这三个的输出:
让我们试试Sonar数据集:
在定义列车控制时,重要的是设置
classProbs = TRUE
,因为这些度量中的一些(ROC和prAUC)不能基于预测的类别而是基于预测的概率来计算 .现在适合您选择的型号:
在此输出中,ROC实际上是ROC曲线下的区域 - 通常称为AUC
和AUC是所有截止值下精确回忆曲线下的面积 .