How do I plot the specificity vs sensitivity graph in R

logistic_regression

#1

Hello,

I am using logistic regression for one classification problem and want to find the optimum value of probability according to which I can assign values “yes”/“no” to the class.
How do I plot the specificity vs sensitivity graph in R like the one below:

Like in this diagram if the value is above the cutoff(7) I will assign “yes”.
Is there any package for this or any inbuilt function.
All the posts I have gone through on this topic talk of using the ROCR package:

This is all that is being displayed.How do I use this package for this problem(If at all this is the package for it). :stuck_out_tongue:


Finding Thresold in logistic regression
#3

Hi,

The plot between Sensitivity and Specificity can help you out here

For the following code

dat1 <- data.frame(val=c(1,2,-1,1,2,3),label=c(1,0,1,0,0,0))
pred <- prediction(dat1$val,dat1$label)
perf = performance(pred,measure="tpr",x.measure="fpr")
plot(perf)

We have the following graph. This is essentially Sensitivity vs (1- Specificity )

Now, in order to find the most optimal binning, we need to find the point that is closes to (0,1) in the graph ( Euclidean distance ). I have written a small script that will find out this value

dist <- 100
alphaNumber <- 0
for(i in 1:length(perf@x.values[[1]]))
{
  xVal <- perf@x.values[[1]][i]
  yVal <- perf@y.values[[1]][i]
  if(is.infinite(xVal) && xVal < 0)
  {
    xVal <- 0
  }
  if(is.infinite(xVal) && xVal > 0)
  {
    xVal <- 1
  }
  if(is.infinite(yVal) && yVal < 0)
  {
    yVal <- 0
  }
  if(is.infinite(yVal) && yVal > 0)
  {
    yVal <- 1
  }
  
  dist1 <- ((xVal -0)^2 + (yVal -1)^2)^0.5
  print(dist1)
  print(xVal)
  print(yVal)
  if(dist1 < dist)
  {
    alphaNumber <- i
    dist <- dist1
  }
}

print(perf@alpha.values[[1]][alphaNumber])

I tried it on the ROCR,Simple dataset which has the following graph

The value that came out was

> print(perf@alpha.values[[1]][alphaNumber])
[1] 0.5014893

Let me know if this helps