[英]Need to apply confusionMatrix from caret to an already computed confusion matrix
[英]How to send a confusion matrix to caret's confusionMatrix?
我正在查看以下数据集: 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"
所以敏感性是:
如果您采用我的混淆矩阵:
$table
td
testPred - +
- 99 6
+ 20 88
您可以看到这并没有加在一起: Sensetivity = 99/(99+20) = 99/119 = 0.831928
。 在我的confusionMatrix结果中,该值是针对特异性的。 但是,特异性是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
如您所见,它认为我的误报和误报是倒退的。
更新 :我发现发送数据要比发送表作为参数要好得多。 从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: "-"
所以我不确定这是好是坏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.