R General solution for creating a 2 * 2 mixing matrix

My question is related to this one on creating a mess matrix in R using a function table(). I am looking for a solution without using a package (e.g. carriages).

Let's say these are ours predictionsand labelsin binary classification:

predictions <- c(0.61, 0.36, 0.43, 0.14, 0.38, 0.24, 0.97, 0.89, 0.78, 0.86, 0.15,  0.52, 0.74, 0.24)
labels      <- c(1,    1,    1,    0,    0,     1,    1,    1,    0,     1,    0,    0,    1,    0)

For these values, the solution below works well to create a 2 * 2 mixing matrix, say threshold = 0.5:

# Confusion matrix for threshold = 0.5
conf_matrix <- as.matrix(table(predictions>0.5,labels))
  conf_matrix
     labels
       0 1
 FALSE 4 3
 TRUE  2 5

However, I do not get a 2 * 2 matrix if I select any value less min(predictions)or more than max(predictions), since the data will have neither FALSE nor TRUE appearance, for example:

conf_matrix <- as.matrix(table(predictions>0.05,labels))
  conf_matrix
     labels
       0 1
  TRUE 6 8

, 2 * 2 ( ) 0 1, . , table, 2 * 2?

+4
1

:

(conf_matrix <- as.matrix(table(factor(predictions>0.05, levels=c(F, T)), labels)))
#        labels
#         0 1
#   FALSE 0 0
#   TRUE  6 8
+4

All Articles