Sensitivity and Specificity are general enough concepts that they can be applied to widely to binary classification (without regard for the modelling methodology to achieve). Though depending on context it may be more or less 'relevant' compared to other possible stats. Here is an example
# model purpose is to predict if setosa , we will make this more difficult by only using Petal.Length to decide it.
library(tidyverse)
(myiris <- iris %>% mutate(
is_setosa=case_when(Species != "setosa" ~ FALSE,
TRUE ~ TRUE)) %>% select(-Species)
)
(lm1 <- lm(is_setosa ~ Petal.Length, data = myiris))
myiris$pred <- predict(lm1,newdata = myiris)
hist(myiris$pred)
# pick a threshold(s)
mythresh <- 0:5/10
#to keep the function short. I assume that confusion matrix will be dimension 2x2
# (which might not be the case for a threshold that pushes every value to a single class)
# also if someone could double check my mapping from the matrix to the TN/TP/FN/FP definitons that would help :)
analyse_at_thresh <- function(thr){
pred_guess <- ifelse(myiris$pred < thr,FALSE,TRUE)
conf <- table(myiris$is_setosa,pred_guess)
TN <- conf[1,1]
FP <- conf[1,2]
FN <- conf[2,1]
TP <- conf[2,2]
Sensitivity <- TP / (TP+FN)
Specificity <- TN / (TN+FP)
#https://en.wikipedia.org/wiki/Sensitivity_and_specificity#Confusion_matrix
list(
threshhold = thr,
conf_matrix = conf,
Sensitivity=Sensitivity,
Specificity=Specificity
)
}
purrr::map(mythresh,
~analyse_at_thresh(.)
)