2016-11-06 128 views
2

我正在查看這個資料列表: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是:

enter image description here(從插入符號的混淆矩陣DOC)

如果你把我的困惑矩陣:

$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 

正如你所看到的,它認爲我的假陽性和假陰性是倒退。

回答

0

UPDATE:我發現發送數據比發送數據好很多。從confusionMatrix文檔:

參考
類的因素被用作真實結果

我把這個意思什麼符號構成了積極的成果。在我的情況下,這將是一個+。然而,「參考」是指數據集的實際結果,也就是因變量。所以我應該使用confusionMatrix(testPred, test$class)。如果您的數據出於某種原因失序,它會將其轉換爲正確的順序(所以正面和負面的結果/預測在混淆矩陣中正確對齊。

不過,如果你所擔心的結果是正確的因素,安裝plyr庫,並使用revalue改變的因素:

install.packages("plyr") 
library(plyr) 
newDF <- df 
newDF$class <- revalue(newDF$class,c("+"=1,"-"=0)) 
# You'd have to rerun your model using newDF 

我不知道爲什麼這個工作,但我只是刪除了正參數:

confusionMatrix(table(testPred, test$class)) 

我混淆矩陣:

 td 
testPred - + 
     - 99 6 
     + 20 88 

插入符的混淆矩陣:

 td 
testPred - + 
     - 99 6 
     + 20 88 

雖然現在說$positive: "-"所以我不知道這是好還是壞。