【发布时间】:2019-03-03 18:07:32
【问题描述】:
最近,当我使用 caret 包运行我的模型时,我发现其 train 对象的重新采样的敏感性和特异性与每次折叠手动计算的敏感性和特异性不同。 让我以 GermanCredit 数据为例。
library(caret)
data("GermanCredit")
form = as.formula('credit_risk~amount+savings+installment_rate+age+housing+number_credits')
train.control <- trainControl(method="cv",
number=5,
summaryFunction = twoClassSummary,
classProbs = TRUE,
savePredictions='all')
rf = train(form, data=GermanCredit, method = 'rf',
metric = 'ROC', trControl=train.control)
print(rf$resample)
我们得到:
ROC Sens Spec Resample
0.6239881 0.9428571 0.13333333 Fold1
0.6603571 0.9714286 0.08333333 Fold2
0.6622619 0.9642857 0.06666667 Fold5
0.6502381 0.9928571 0.10000000 Fold4
0.7072619 0.9714286 0.16666667 Fold3
如您所见,对于折叠 1,灵敏度和特异性分别为 0.94 和 0.13。
现在,如果我们只从 Fold1 中重新采样,并使用confusionMatrix 计算指标,我们得到以下结果:
resamp.1 = rf$pred %>% filter(Resample=='Fold1')
cm=confusionMatrix(resamp.1$pred, resamp.1$obs)
print(cm)
Confusion Matrix and Statistics
Reference
Prediction good bad
good 366 135
bad 54 45
Accuracy : 0.685
95% CI : (0.6462, 0.722)
No Information Rate : 0.7
P-Value [Acc > NIR] : 0.8018
Kappa : 0.1393
Mcnemar's Test P-Value : 5.915e-09
Sensitivity : 0.8714
Specificity : 0.2500
Pos Pred Value : 0.7305
Neg Pred Value : 0.4545
Prevalence : 0.7000
Detection Rate : 0.6100
Detection Prevalence : 0.8350
Balanced Accuracy : 0.5607
'Positive' Class : good
如您所见,敏感性和特异性分别为 0.87 和 0.25。与直接resamples输出的数字相比,数字完全不同!其他折叠也会发生同样的情况。
我做错了吗?或者插入符号做了不同的事情?谢谢。
【问题讨论】: