特异性和敏感性测量

Specificity and sensitivity measures

我一直在尝试计算一些关于特异性和敏感性的指标。
SDMTools R 包在获得总体估计方面效果很好。

library(SDMTools)

a <- c(1,1,1,1,0,0,0,1,0,1,1,1) #observed
b <- c(1,1,0,0,1,0,0,1,0,0,1,1) #predicted

accuracy(a, b)
#       threshold       AUC omission.rate sensitivity specificity prop.correct     Kappa
#1            0.5    0.6875         0.375       0.625        0.75    0.6666667 0.3333333

不幸的是,我无法获得真阳性、真阴性、假阳性、假阴性的原始数据。我想将它们放在四个不同的栏中。有谁知道可以做到这一点的包或功能?我想得到的结果如下。

#TP  TN  FP  FN
#5    3   1   3

您可以使用像下面这样的自定义函数来计算真/假分类计数:

accuracy_table <- function(obs, pred){
  data.frame(TP = sum(obs == 1 & pred == 1),
             TN = sum(obs == 0 & pred == 0),
             FP = sum(obs == 0 & pred == 1),
             FN = sum(obs == 1 & pred == 0))
}

accuracy_table(a, b)

此函数依赖于用 0 作为否定案例和 1 作为肯定案例编码的观察和预测,因此在其他情况下它不会按预期工作。