【发布时间】:2017-03-20 00:20:30
【问题描述】:
我正在查看这个数据集:https://archive.ics.uci.edu/ml/datasets/Credit+Approval。我建了一个ctree:
myFormula<-class~. # class is a factor of "+" or "-"
ct <- ctree(myFormula, data = train)
现在我想将这些数据放入插入符号的confusionMatrix 方法中,以获取与混淆矩阵相关的所有统计信息:
testPred <- predict(ct, newdata = test)
#### This is where I'm doing something wrong ####
confusionMatrix(table(testPred, test$class),positive="+")
#### ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ####
$positive
[1] "+"
$table
td
testPred - +
- 99 6
+ 20 88
$overall
Accuracy Kappa AccuracyLower AccuracyUpper AccuracyNull AccuracyPValue McnemarPValue
8.779343e-01 7.562715e-01 8.262795e-01 9.186911e-01 5.586854e-01 6.426168e-24 1.078745e-02
$byClass
Sensitivity Specificity Pos Pred Value Neg Pred Value Precision Recall F1
0.9361702 0.8319328 0.8148148 0.9428571 0.8148148 0.9361702 0.8712871
Prevalence Detection Rate Detection Prevalence Balanced Accuracy
0.4413146 0.4131455 0.5070423 0.8840515
$mode
[1] "sens_spec"
$dots
list()
attr(,"class")
[1] "confusionMatrix"
所以 Sensetivity 是:
如果你拿我的混淆矩阵:
$table
td
testPred - +
- 99 6
+ 20 88
你可以看到这不加起来:Sensetivity = 99/(99+20) = 99/119 = 0.831928。在我的confusionMatrix 结果中,该值用于特异性。然而,Specificity 是 Specificity = D/(B+D) = 88/(88+6) = 88/94 = 0.9361702,即 Sensitivity 的值。
我已经尝试过这个confusionMatrix(td,testPred, positive="+"),但得到了更奇怪的结果。我做错了什么?
更新:我还意识到我的混淆矩阵与插入符号认为的不同:
Mine: Caret:
td testPred
testPred - + td - +
- 99 6 - 99 20
+ 20 88 + 6 88
如你所见,它认为我的 False Positive 和 False Negative 是倒退的。
【问题讨论】:
标签: r r-caret confusion-matrix