【问题标题】:reproducing caret confusionMatrix sensitivity and specificity (they seem to be swapped) and PPV (seems to be labled NPV)重现插入符号混淆矩阵敏感性和特异性(它们似乎被交换)和 PPV(似乎被标记为 NPV)
【发布时间】:2020-05-21 15:33:02
【问题描述】:

我无法从caret 混淆矩阵值中重现灵敏度和特异性参数。 (提前道歉,这是不可重现的,但我希望我显示足够的代码以清晰;除了pROC 参考之外,它应该能够被任何(0,1)真值和预测向量重现)。

来自https://en.wikipedia.org/wiki/Sensitivity_and_specificity:

  • 敏感性(又名召回 aka TPR)= TP / (TP + FN)
  • 特异性(又名选择性,又名 TNR)= TN / (TN + FP)
  • 精度(也称为阳性预测值或 PPV)​​= TP / (TP + FP)

我的混淆矩阵:

> cm = confusionMatrix(factor(y_pred),factor(y_test))
> cm$table
          Reference
Prediction    0    1
         0 8883  374
         1 1440 1640

我从中获取

> TP=1640; FP=1440; FN=374; TN=8883
> cm$byClass
         Sensitivity          Specificity       Pos Pred Value       Neg Pred Value 
           0.8605057            0.8142999            0.9595981            0.5324675 
           Precision               Recall                   F1           Prevalence 
           0.9595981            0.8605057            0.9073544            0.8367512 
      Detection Rate Detection Prevalence    Balanced Accuracy 
           0.7200292            0.7503445            0.8374028 
> TP / (TP + FN)  #sensitivity aka Recall aka TPR
[1] 0.8142999
> TN / (TN + FP)  #Specificity 
[1] 0.8605057
> TP / (TP + FP)  #precision aka PPV
[1] 0.5324675
> TN / (TN + FN)  # NPV
[1] 0.9595981

看起来返回的数据标记错误 - 特异性和灵敏度已交换,Pos Pred 值和 Neg Pred 值也已交换。

pROC交叉检查:

> best_thr = coords(pROC_obj,x='best',input='threshold',transpose=TRUE)
> best_thr
  threshold specificity sensitivity 
  0.2204595   0.8605057   0.8142999

似乎验证了confustionMatrix Sensitivity 和 Specificity 值已被交换。

对此最担心的是,当我尝试验证时,我要检索的 F1 值已经偏离了:

 > cm$byClass[7]
       F1 
0.9073544 
 > TP / (TP + (FP+FN)/2)   # F1
[1] 0.6438948

我正在使用带有(新安装的)pROCcaret 软件包的 R 版本 3.5.3。 我的计算是否有问题,还是 caret confusionMatrix 实际上返回了错误的值(尤其是 F1)? (我认为我更有可能是错的,但pROC coords 同意我的观点,不同意confusionMatrix。)

【问题讨论】:

  • 我注意到如果您的结果被标记为一个单词并且正例的名称在字母表中低于负例(如 case=positive 和 control=negative),则会发生此问题;插入符号自动将肯定类指定为出现在字母表中的第一个单词。试试cm = confusionMatrix(factor(y_pred),factor(y_test), positive=positive)positive=whatever_name
  • @PleaseHelp 如果您想将此评论添加为答案,我会接受!谢谢 - 詹姆斯

标签: r r-caret confusion-matrix


【解决方案1】:

“y_pred”和“y_test”中有两个值(0 和 1)。 我想预测1s。 例如:

y_pred = c(0,1,0,0,1,1,0)
y_test = c(0,0,0,0,1,1,0)

当我执行时:

cm = confusionMatrix(factor(y_pred),factor(y_test), positive="1")

输出是正确的。

【讨论】:

    猜你喜欢
    • 2017-05-17
    • 2019-03-12
    • 1970-01-01
    • 2014-09-22
    • 2019-03-04
    • 2019-01-01
    • 2021-11-12
    • 2019-03-03
    相关资源
    最近更新 更多