liveBook · Manning

Confusion matrix

As explained in Statistics Slam Dunk

A confusion matrix is a table that cross-classifies predicted and observed classes to count correct and incorrect classifications.

A confusion matrix evaluates a binary classification model by cross-classifying observed outcomes against predicted outcomes. In an NBA playoff example, it counts teams correctly and incorrectly predicted to qualify for postseason play. section 14.6.2

The four cells describe the model’s decisions:

  • True positive: the model correctly predicts playoff qualification; the example contains 40. section 14.6.2
  • True negative: the model correctly predicts that a team will miss the postseason; the example contains 19. section 14.6.2
  • False positive: the model predicts qualification when the team does not qualify; the example contains 11. section 14.6.2
  • False negative: the model predicts non-qualification when the team qualifies; the example contains 10. section 14.6.2

Building the matrix in R

Convert predicted probabilities into binary labels before comparing them with binary actual outcomes. With a 0.50 cutoff, table(actuals$actual_values, predictions$predicted_values2) returns rows and columns for 0 and 1: 19 true negatives, 11 false positives, 10 false negatives, and 40 true positives. These counts yield sensitivity of 0.80, specificity of 0.6333, misclassification error of 0.2625, and accuracy of 0.7375. section 14.6.2

confusion_matrix <- table(actuals$actual_values,     predictions$predicted_values2)
print(confusion_matrix)
##    0  1
## 0 19 11
## 1 10 40

View the executable R output showing the confusion-matrix layout and its four cell counts. section 14.6.2

How Statistics Slam Dunk covers it

How deep it goes
Explains four outcome types, requires binary inputs, and derives sensitivity, specificity, error, and accuracy.
Examples
Uses NBA playoff predictions, a 0.50 probability cutoff, R's table() function, and an 80-observation classification result.
What it assumes
Assumes binary observed outcomes and predictions while teaching how probabilities become class labels and performance counts.
What you can do after
Diagnose model performance with R by constructing a confusion matrix and calculating key classification measures.
How it connects
Connects binary classification, logistic regression, sensitivity, specificity, accuracy, misclassification error, and ROC curves.

Also explained in

Compare how 14 books explain confusion matrix →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Slam Dunk. The section links point to the parts of the book it draws on.