【问题标题】:Sensitivity and specificity values differ using two different R package( Caret and pROC)使用两个不同的 R 包(Caret 和 pROC),灵敏度和特异性值不同
【发布时间】:2019-12-22 01:55:59
【问题描述】:

我正在尝试计算敏感性和特异性。我使用了 caret 包和 pROC 包。但是,为什么我使用两个不同的包会得到不同的结果?

我已将数据转换为二进制形式(只要有正确的调用,它就被标记为 1 和 0 表示不正确的调用。即其余 39 个类中的任何一个)。

混淆矩阵如下:

 table(actual_labels,app_labels)
             app_labels
actual_labels    0    1
             0 1183   23
             1    5   18

方法一:使用caret

> sensitivity(table(actual_labels,app_labels))
[1] 0.9957912
> specificity(table(actual_labels,app_labels))
[1] 0.4390244

cutoff of =0.5

方法2:使用pROC

aa <- roc(actual_labels,app_labels)
Setting levels: control = 0, case = 1
Setting direction: controls < cases
> aa$sensitivities
[1] 1.0000000 0.7826087 0.0000000
> aa$specificities
[1] 0.0000000 0.9809287 1.0000000
> aa$thresholds
[1] -Inf  0.5  Inf

为什么我使用两个不同的包会得到不同的结果?

【问题讨论】:

    标签: r r-caret roc


    【解决方案1】:

    您确定您正确使用了 Caret 中的功能吗?

    手动计算敏感性和特异性,您将得到 pROC 的结果。自己试试吧:

    sensitivity = TP / (TP + FN)
    specificity = TN / (TN + FP)
    

    【讨论】:

    • 是的,我使用了正确的功能。 library(caret) &gt; caret::sensitivity(table(actual_disease,app_disease)) [1] 0.9957912 &gt; caret::specificity(table(actual_disease,app_disease)) [1] 0.4390244 .[ statinfer.com/…
    • 但是,使用上面的公式我得到了正确的结果
    • @Dhwani Dholakia 可能会添加正类:caret::sensitivity(table(actual_disease,app_disease), positive = "1"),因为插入符号默认使用 rownames(data)[1],这可能是您的负类。
    • @usr 感谢您的帮助。你能提供你对这篇文章的建议吗?这对我真的很有帮助。谢谢 [stackoverflow.com/questions/57520855/…
    【解决方案2】:

    根据the documentation of the sensitivity function in caretDetails 部分中的表格应如下所示:

                Reference   
    Predicted   Event   No Event
    Event       A       B
    No Event    C       D 
    

    请注意,真正的标签在列中,而预测的标签在行中,这与您所拥有的相反。要获得正确的表格,您应该使用:

     table(app_labels, actual_labels)
    

    这种语法很容易出错,最好使用替代方法(这是以前版本的插入符号中唯一可用的语法):

    sensitivity(data, reference, ...)
    

    与:

    data       for the default functions, a factor containing the discrete measurements. [...]
    reference  a factor containing the reference values
    

    所以在你的情况下是这样的;

    sensitivity(app_labels, actual_labels)
    

    【讨论】:

    猜你喜欢
    • 2019-04-21
    • 2019-12-22
    • 1970-01-01
    • 2014-09-22
    • 2021-04-21
    • 2016-06-14
    • 2013-02-22
    • 2019-01-01
    • 1970-01-01
    相关资源
    最近更新 更多