用R中的pROC获得至少75%灵敏度的最佳阈值



我有一个有两列的数据框:score1numeric, truth1boolean。我想用score1来预测truth1。要做到这一点,我需要一个简单的线性模型,然后要求一个好的阈值,即一个阈值,使我在ROC曲线中灵敏度达到75%。因此,我这样做:

roc_curve = roc(truth1 ~ score1 , data = my_data)
coords(roc=roc_curve, x = 0.75, input='sensitivity', ret='threshold')

我的问题是,坐标返回'NA',因为0.75的灵敏度没有出现在ROC曲线中。所以我的问题是:我怎样才能得到一个阈值,使我的灵敏度至少为0.75,并具有最大的特异性?

选项1:筛选结果

my.coords <- coords(roc=roc_curve, x = "all", transpose = FALSE)
my.coords[my.coords$sensitivity >= .75, ]

选项2:您可以通过请求75%到100%之间的部分AUC来欺骗pROC:

roc_curve = roc(truth1 ~ score1 , data = my_data, partial.auc = c(1, .75), partial.auc.focus="sensitivity")

所有pROC的方法将遵循这个请求,并只在这个感兴趣的区域给出结果:

coords(roc=roc_curve, x = "local maximas", ret='threshold', transpose = FALSE)

要详细说明Calimo的出色回答,这里有一个可概括的代码片段:

# Specify SENSITIVITY criteria to meet.
Sn.upper <- 1.0
Sn.lower <- 0.5
# Specify SPECIFICITY criteria to meet.
Sp.upper <- 1.0
Sp.lower <- 0.6
# Extract all coordinate values from the ROC curve.
my.coords <- coords(roc=auc, x = "all", transpose = FALSE)
# Identify and print all points on the ROC curve that meet the JOINT sensitivity AND specificity criteria.
my.coords[(my.coords$specificity >= Sp.lower & my.coords$specificity <= Sp.upper & 
            my.coords$sensitivity >= Sn.lower & my.coords$sensitivity <= Sn.upper),]

示例输出:

       threshold specificity sensitivity
all.46    10.950   0.5000000   0.7073171
all.47    11.080   0.5138889   0.7073171
all.48    11.345   0.5138889   0.6829268
all.49    11.635   0.5138889   0.6585366
all.50    11.675   0.5138889   0.6341463
all.51    11.700   0.5277778   0.6341463
all.52    11.725   0.5277778   0.6097561
all.53    11.850   0.5416667   0.6097561
all.54    12.095   0.5555556   0.6097561

最新更新