我正在查看這個資料列表:https://archive.ics.uci.edu/ml/datasets/Credit+Approval。我建了一個ctree:如何將混淆矩陣發送給caret的confusionMatrix?
myFormula<-class~. # class is a factor of "+" or "-"
ct <- ctree(myFormula, data = train)
現在我想將數據放到插入符的混淆矩陣方法來獲取與混淆矩陣相關聯的所有數據:
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
,靈敏度的值。
我試過這個confusionMatrix(td,testPred, positive="+")
,但得到了更加奇怪的結果。我究竟做錯了什麼?
更新:我也意識到,我的混淆矩陣是比插入符認爲這是不同的:
Mine: Caret:
td testPred
testPred - + td - +
- 99 6 - 99 20
+ 20 88 + 6 88
正如你所看到的,它認爲我的假陽性和假陰性是倒退。