只有savePredictions = TRUE
参数缺失于ctrl
(这也适用于其他重采样方法):
library(caret)
library(mlbench)
data(Sonar)
ctrl <- trainControl(method="cv",
summaryFunction=twoClassSummary,
classProbs=T,
savePredictions = T)
rfFit <- train(Class ~ ., data=Sonar,
method="rf", preProc=c("center", "scale"),
trControl=ctrl)
library(pROC)
# Select a parameter setting
selectedIndices <- rfFit$pred$mtry == 2
# Plot:
plot.roc(rfFit$pred$obs[selectedIndices],
rfFit$pred$M[selectedIndices])
也许我错过了一些东西,但一个小问题是train
估计的 AUC 值总是与plot.roc
and pROC::auc
(绝对差 twoClassSummary uses pROC::auc
来估计 AUC。Edit:我认为发生这种情况是因为 ROC 来自train
是使用单独的 CV 集的 AUC 平均值,这里我们同时计算所有重采样的 AUC,以获得总体 AUC。
Update由于这引起了一些关注,这里有一个使用的解决方案plotROC::geom_roc()
for ggplot2
:
library(ggplot2)
library(plotROC)
ggplot(rfFit$pred[selectedIndices, ],
aes(m = R, d = factor(obs, levels = c("R", "M")))) +
geom_roc(hjust = -0.4, vjust = 1.5) + coord_equal()