I have been trying to calculate some measures about specificity and sensitivity.
The SDMTools R package worked great to get the overall estimates.
library(SDMTools)
a <- c(1,1,1,1,0,0,0,1,0,1,1,1) #observed
b <- c(1,1,0,0,1,0,0,1,0,0,1,1) #predicted
accuracy(a, b)
# threshold AUC omission.rate sensitivity specificity prop.correct Kappa
#1 0.5 0.6875 0.375 0.625 0.75 0.6666667 0.3333333
Unfortunately, I was not able to get the raw numbers for TRUE POSITIVE,TRUE NEGATIVE, FALSE POSITIVE, FALSE NEGATIVE. I would like to have them in four different columns. Does anyone know a package or a function that can do that? The result I would like to get is below.
#TP TN FP FN
#5 3 1 3
You could use a custom function like this one below to calculate the true / false classification counts:
accuracy_table <- function(obs, pred){
data.frame(TP = sum(obs == 1 & pred == 1),
TN = sum(obs == 0 & pred == 0),
FP = sum(obs == 0 & pred == 1),
FN = sum(obs == 1 & pred == 0))
}
accuracy_table(a, b)
This function relies on observations and predictions being coded with 0 as the negative case and 1 as the positive case, so it won't work as desired in other cases.