Calculate the 'optimal' (=max sensitivity and specificity) cut-off value using ROCR
<p>I would like to calculate the optimal cut-off value, in my case the intersection of maximum sensitivity and specificity to define a decision rule for a logistic regression classification approach. Looking for a solution in stack overflow I found a suggested solution to <a href="https://stackoverflow.com/questions/32171702/calculate-cut-off-that-max-sensitivity-vs-specificity-using-rocr">calculate cut-off that max sensitivity vs specificity using ROCR</a>. </p>

<p>However, when I'm plotting the specificity and the sensitivity values (y-axis) on a joint scale as a function of cut-off values (x-values) of my prediction-object (calculated by the eRm package) with the ROCR package, I got the following figure (see below). </p>

<p>Now, if I calculate the point of intersection of both functions, where specificity and sensitivity were maximized, as suggested in the prior thread, I got a value that lies elsewhere next to the point, which I would visually detect as point of intersection.</p>

<p>My question is rather simple: Could someone show me a way to calculate the point of intersection of both functions to get an 'optimal' cut-off point in R?</p>

<p><a href="https://i.stack.imgur.com/mLgGN.png" rel="nofollow noreferrer">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</a></p>

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

#ROCR
pred.model.RM &lt;- gof.model.RM$predobj
Sens.model.RM &lt;- performance(pred.model.RM,  measure="sens", x.measure="cutoff")
Spec.model.RM &lt;- 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 &lt;- performance(pred.model.RM,  "sens", "spec")
CP&lt;-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
</code></pre>