计算“最佳”' (=最大灵敏度和特异性)使用ROCR的截止值

时间:2016-03-01 19:17:18

标签: r optimization

我想计算最佳截止值,在我的情况下,最大灵敏度和特异性的交集来定义逻辑回归分类方法的决策规则。在寻找堆栈溢出的解决方案时,我找到了calculate cut-off that max sensitivity vs specificity using ROCR的建议解决方案。

然而,当我在关节尺度上绘制特异性和灵敏度值(y轴)作为我的预测对象的截止值(x值)的函数时(通过eRm计算)使用ROCR包,我得到了下图(见下文)。

现在,如果我计算两个函数的交点,其中特异性和灵敏度最大化,如前一个线程中所建议的那样,我得到一个位于该点旁边的值,我会在视觉上将其视为点交集。

我的问题很简单:有人可以告诉我一种方法来计算两个函数的交点,以获得最佳的' R?中的截止点

Figure 1: Example Plot of sensitivity and specificity as a function of probability cutoff. The line indicates the 'optimal' cutoff value deviating from the visually detected optimal threshold. The calculation of the 'optimal' cutoff value has been done as suggested in an earlier stack overflow thread

library(ROCR)
library(eRm)
set.seed(1)
data <- sim.rasch(30, 300) # simulate Rasch homogenous data 
model.RM<-RM(data, se=T)#estimate Rasch model
PPAR.X <-person.parameter(model.RM)
#Goodness-of-fit test (see Mair et al. 2008)
gof.model.RM<-gofIRT(PPAR.X)
#summary(gof.model.RM) 

#ROCR
pred.model.RM <- gof.model.RM$predobj
Sens.model.RM <- performance(pred.model.RM,  measure="sens", x.measure="cutoff")
Spec.model.RM <- performance(pred.model.RM,  measure="spec", x.measure="cutoff")

#Identify the 'optimal' cutoff that yields the highest sensitivity and specificity according to prior stack overflow thread:
SensSpec.model.RM <- performance(pred.model.RM,  "sens", "spec")
CP<-SensSpec.model.RM@alpha.values[[1]][which.max(SensSpec.model.RM@x.values[[1]]+SensSpec.model.RM@y.values[[1]])]
# [1] 0.5453864 # 'optimal' cutoff value

#Plot
plot(Sens.model.RM, type="l", col="red",xlab="",ylab="")
par(new=TRUE)
plot(Spec.model.RM, type="l", col="blue", xlab="Probability cutoff (threshold)",ylab="Sensitivity/Specificity")
abline(v = CP, col = "black", lty = 3)#add a line indicating the suggested 'optimal' cutoff value differing from the visually expected one

1 个答案:

答案 0 :(得分:3)

如果您想找到最大的金额,您可以

best.sum <- which.max(Sens.model.RM@y.values[[1]]+Spec.model.RM@y.values[[1]])
Sens.model.RM@x.values[[1]][best.sum]
# [1] 0.5453863

如果你想找到最近的交叉路口,你可以

both.eq <- which.min(abs(Sens.model.RM@y.values[[1]]-Spec.model.RM@y.values[[1]]))
Sens.model.RM@x.values[[1]][both.eq]
# [1] 0.5380422