I managed to get the sensitivity and specificity with respect to cutoff value by help of stack overflow.The link is below.
Find a threshold value for confusion matrix in R
I made a data df
. My goal is to draw smooth graphs of sensitivity and specificity and find the intersecting point.
df<-data.frame(actual_class=c(0,1,0,0,1,1,1,0,0,1),
predicted_probability=c(0.51,0.3,0.2,0.35,0.78,0.69,0.81,0.31,0.59,0.12),
predicted_class=c(1,0,0,0,1,1,1,0,1,0))
library(dplyr)
df0<-df %>% filter(actual_class==0)
df1<-df %>% filter(actual_class==1)
probs <- seq(0, 1, by=.05)
names(probs) <- probs
results0 <- sapply(probs, function(x) 0 == as.integer(df0$predicted_probability > x))
colSums(results0)
A=as.data.frame(colSums(results0))
A$sensitivity<-A$`colSums(results0)`/nrow(df1) #sensitivity
results1 <- sapply(probs, function(x) 1 == as.integer(df1$predicted_probability > x))
colSums(results1)
B=as.data.frame(colSums(results1))
B$specificity<-B$`colSums(results1)`/nrow(df0) #specificity
But in order to find the intersecting point between sensitivity and specificity, the interval of the plots should be really small. Here, I set the interval as 0.05 by using the code probs <- seq(0, 1, by=.05)
How can I plot the sensitivity and specificity graph and find the intersecting point?
CodePudding user response:
You can plot the sensitivity and specificity on one plot as follows:
plot(probs, B$specificity, type = "l")
lines(probs, A$sensitivity, col = "r")
Note: Obviously, it is advised to add relevant labels, titles and legend information to the plot.
You can find the intersection points as follows:
which(A$sensitivity == B$specificity)
So, the 8th, 9th, 10th and 11th points are equal.