插入符号如何计算重新采样的灵敏度和特异性?
How does caret calculate sensitivity and specificity in resamples?
最近,当我将 caret 包用于 运行 我的模型时,我发现其训练对象的重采样的灵敏度和特异性与每次折叠手动计算的灵敏度和特异性不同。
让我以 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直接输出的相比,数字完全不同!!其他折叠也会发生同样的事情。
我是不是做错了什么?还是插入符在做不同的事情?谢谢
请注意 data(GermanCredit)
的变量与您在 form
中保存的变量不同,您 post 一个可重现的示例将有助于以后的问题。此外,使用 set.seed()
会有所帮助。
然而,这里的问题是您需要考虑 mtry
,即随机森林模型中使用的 "Randomly Selected Predictors" 的数量。 See documentation and code here.
我调整了 GermanCredit
这样大家就可以 运行 原样了:
library(caret)
data("GermanCredit")
form = as.formula('Class~Amount+SavingsAccountBonds.100.to.500+SavingsAccountBonds.lt.100+SavingsAccountBonds.500.to.1000+
SavingsAccountBonds.lt.100+SavingsAccountBonds.gt.1000+SavingsAccountBonds.Unknown+
InstallmentRatePercentage+Age+Housing.ForFree+Housing.Own+Housing.Rent+NumberExistingCredits')
train.control <- trainControl(method="cv",
number=5,
summaryFunction = twoClassSummary,
classProbs = TRUE,
savePredictions='all')
set.seed(100)
rf <- train(form, data=GermanCredit, method = 'rf',
metric = 'ROC', trControl=train.control)
如果我们检查 rf
我们可以看到模型中使用的 mtry
的最终值为 mtry = 2
.
> rf
Random Forest
1000 samples
12 predictor
2 classes: 'Bad', 'Good'
No pre-processing
Resampling: Cross-Validated (5 fold)
Summary of sample sizes: 800, 800, 800, 800, 800
Resampling results across tuning parameters:
mtry ROC Sens Spec
2 0.6465714 0.06333333 0.9842857
7 0.6413214 0.31333333 0.8571429
12 0.6358214 0.31666667 0.8385714
ROC was used to select the optimal model using the largest value.
The final value used for the model was mtry = 2.
因此,通过在 rf$pred
中过滤 mtry = 2
,您将得到预期的结果。
resamp.1 <- rf$pred %>% filter(Resample=='Fold1' & mtry == 2)
cm <- confusionMatrix(resamp.1$pred, resamp.1$obs)
print(cm)
Confusion Matrix and Statistics
Reference
Prediction Bad Good
Bad 7 5
Good 53 135
Accuracy : 0.71
95% CI : (0.6418, 0.7718)
No Information Rate : 0.7
P-Value [Acc > NIR] : 0.4123
Kappa : 0.1049
Mcnemar's Test P-Value : 6.769e-10
Sensitivity : 0.1167
Specificity : 0.9643
Pos Pred Value : 0.5833
Neg Pred Value : 0.7181
Prevalence : 0.3000
Detection Rate : 0.0350
Detection Prevalence : 0.0600
Balanced Accuracy : 0.5405
'Positive' Class : Bad
cm$byClass[1:2] == rf$resample[1,2:3]
Sens Spec
TRUE TRUE
编辑:
您还可以通过选中 rf$resampledCM
来控制这一点,并查看不同单元格中不同 mtry
和折叠的观察次数。
最近,当我将 caret 包用于 运行 我的模型时,我发现其训练对象的重采样的灵敏度和特异性与每次折叠手动计算的灵敏度和特异性不同。 让我以 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直接输出的相比,数字完全不同!!其他折叠也会发生同样的事情。
我是不是做错了什么?还是插入符在做不同的事情?谢谢
请注意 data(GermanCredit)
的变量与您在 form
中保存的变量不同,您 post 一个可重现的示例将有助于以后的问题。此外,使用 set.seed()
会有所帮助。
然而,这里的问题是您需要考虑 mtry
,即随机森林模型中使用的 "Randomly Selected Predictors" 的数量。 See documentation and code here.
我调整了 GermanCredit
这样大家就可以 运行 原样了:
library(caret)
data("GermanCredit")
form = as.formula('Class~Amount+SavingsAccountBonds.100.to.500+SavingsAccountBonds.lt.100+SavingsAccountBonds.500.to.1000+
SavingsAccountBonds.lt.100+SavingsAccountBonds.gt.1000+SavingsAccountBonds.Unknown+
InstallmentRatePercentage+Age+Housing.ForFree+Housing.Own+Housing.Rent+NumberExistingCredits')
train.control <- trainControl(method="cv",
number=5,
summaryFunction = twoClassSummary,
classProbs = TRUE,
savePredictions='all')
set.seed(100)
rf <- train(form, data=GermanCredit, method = 'rf',
metric = 'ROC', trControl=train.control)
如果我们检查 rf
我们可以看到模型中使用的 mtry
的最终值为 mtry = 2
.
> rf
Random Forest
1000 samples
12 predictor
2 classes: 'Bad', 'Good'
No pre-processing
Resampling: Cross-Validated (5 fold)
Summary of sample sizes: 800, 800, 800, 800, 800
Resampling results across tuning parameters:
mtry ROC Sens Spec
2 0.6465714 0.06333333 0.9842857
7 0.6413214 0.31333333 0.8571429
12 0.6358214 0.31666667 0.8385714
ROC was used to select the optimal model using the largest value.
The final value used for the model was mtry = 2.
因此,通过在 rf$pred
中过滤 mtry = 2
,您将得到预期的结果。
resamp.1 <- rf$pred %>% filter(Resample=='Fold1' & mtry == 2)
cm <- confusionMatrix(resamp.1$pred, resamp.1$obs)
print(cm)
Confusion Matrix and Statistics
Reference
Prediction Bad Good
Bad 7 5
Good 53 135
Accuracy : 0.71
95% CI : (0.6418, 0.7718)
No Information Rate : 0.7
P-Value [Acc > NIR] : 0.4123
Kappa : 0.1049
Mcnemar's Test P-Value : 6.769e-10
Sensitivity : 0.1167
Specificity : 0.9643
Pos Pred Value : 0.5833
Neg Pred Value : 0.7181
Prevalence : 0.3000
Detection Rate : 0.0350
Detection Prevalence : 0.0600
Balanced Accuracy : 0.5405
'Positive' Class : Bad
cm$byClass[1:2] == rf$resample[1,2:3]
Sens Spec
TRUE TRUE
编辑:
您还可以通过选中 rf$resampledCM
来控制这一点,并查看不同单元格中不同 mtry
和折叠的观察次数。