【问题标题】:How to send a confusion matrix to caret's confusionMatrix?如何将混淆矩阵发送到插入符号的混淆矩阵?
【发布时间】: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


    【解决方案1】:

    更新:我发现发送数据比将表作为参数发送要好得多。来自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
    

    Caret 的混淆矩阵:

            td
    testPred  -  +
           - 99  6
           + 20 88
    

    虽然现在它说$positive: "-",所以我不确定这是好是坏。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 2015-12-17
      • 2020-10-01
      • 2012-01-20
      • 2019-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多