- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 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 中找到它?
提前致谢!
最佳答案
Caret 已经具有汇总函数来输出您提到的所有指标:
defaultSummary
输出准确度和 KappatwoClassSummary
输出 AUC(ROC 曲线下的面积 - 参见答案的最后一行)、敏感性和特异性prSummary
输出精度和召回率
为了获得组合指标,您可以编写自己的汇总函数,该函数结合了这三个的输出:
library(caret)
MySummary <- function(data, lev = NULL, model = NULL){
a1 <- defaultSummary(data, lev, model)
b1 <- twoClassSummary(data, lev, model)
c1 <- prSummary(data, lev, model)
out <- c(a1, b1, c1)
out}
让我们尝试声纳数据集:
library(mlbench)
data("Sonar")
在定义列车控制时,设置classProbs = TRUE
非常重要,因为其中一些指标(ROC 和 prAUC)无法根据预测类别计算,而是根据预测概率计算。
ctrl <- trainControl(method = "repeatedcv",
number = 10,
savePredictions = TRUE,
summaryFunction = MySummary,
classProbs = TRUE)
现在适合您选择的模型:
mod_fit <- train(Class ~.,
data = Sonar,
method = "rf",
trControl = ctrl)
mod_fit$results
#output
mtry Accuracy Kappa ROC Sens Spec AUC Precision Recall F AccuracySD KappaSD
1 2 0.8364069 0.6666364 0.9454798 0.9280303 0.7333333 0.8683726 0.8121087 0.9280303 0.8621526 0.10570484 0.2162077
2 31 0.8179870 0.6307880 0.9208081 0.8840909 0.7411111 0.8450612 0.8074942 0.8840909 0.8374326 0.06076222 0.1221844
3 60 0.8034632 0.6017979 0.9049242 0.8659091 0.7311111 0.8332068 0.7966889 0.8659091 0.8229330 0.06795824 0.1369086
ROCSD SensSD SpecSD AUCSD PrecisionSD RecallSD FSD
1 0.04393947 0.05727927 0.1948585 0.03410854 0.12717667 0.05727927 0.08482963
2 0.04995650 0.11053858 0.1398657 0.04694993 0.09075782 0.11053858 0.05772388
3 0.04965178 0.12047598 0.1387580 0.04820979 0.08951728 0.12047598 0.06715206
在此输出中ROC实际上是ROC曲线下的面积——通常称为AUC
和AUC 是所有截止值的精确召回曲线下的面积。
关于r - 插入符号中的其他指标 - PPV、灵敏度、特异性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57146032/
我已经生成了一些分数来帮助预测某事是 (1) 还是否 (0),假设数据包括: scores = c(10:20) response = c(0,0,1,0,1,0,1,1,0,1,1) mydata
我在 R 中使用插入符号进行逻辑回归: ctrl <- trainControl(method = "repeatedcv", number = 10, repeats = 10,
我在 R 中使用插入符号进行逻辑回归: ctrl <- trainControl(method = "repeatedcv", number = 10, repeats = 10,
我使用以下代码使用 RandomForest 作为分类器计算敏感性、特异性、NPV 和 PPV。 suppressMessages(require(randomForest)); clas
我一直在使用 R 中的 pROC 包为对应于特定阈值的 SP 和 SN 值生成自举置信区间。但是,我一直无法找到一种方法来为 PPV 和 NPV 值生成 CI。 pROC 中是否存在这样的函数? 最佳
我使用以下代码使用 RandomForest 作为分类器计算敏感性、特异性、NPV 和 PPV。 suppressMessages(require(randomForest)); clas
我正在使用 PyML用于 SVM 分类。但是,我注意到当我使用 LOO 评估多类分类器时,结果对象不报告灵敏度和 PPV 值。相反,它们是 0.0: from PyML import * from P
我是一名优秀的程序员,十分优秀!