Confusion matrix and classification metrics
A confusion matrix counts correct and incorrect predictions at a threshold, and metrics such as precision, recall and specificity summarise different kinds of error.
- actual positives predicted positive / negative
- actual negatives predicted negative / positive
- total number of cases
- the same as recall (true positive rate)
When to use
Evaluating a classifier (logistic regression, a scoring rule, a screening test) at a chosen threshold — preferably on data not used to fit it.
How to interpret
- Recall/sensitivity: share of actual positives caught. Specificity: share of actual negatives correctly cleared.
- Precision (PPV): share of predicted positives that are truly positive — depends strongly on prevalence.
- Moving the threshold trades recall against specificity; choose it from the costs of each error type.
- A metric whose denominator is 0 is reported as missing (null), never as 0 or 1.
Common mistakes
- Judging by accuracy on imbalanced data: predicting "negative" for everyone scores 95% accuracy when 5% are positive.
- Evaluating on the training data (optimistic) instead of a test set or cross-validation.
- Confusing precision with recall, or assuming precision transfers to a population with different prevalence.
Example
Ten cases (4 positive) scored 0.9, 0.8, 0.55, 0.3 (positives) and 0.6, 0.4, 0.35, 0.2, 0.1, 0.05 (negatives); threshold 0.5.
- TP = 3, FN = 1, FP = 1, TN = 5.
- Accuracy 0.80, precision 0.75, recall 0.75, specificity 0.833, F₁ 0.75, balanced accuracy 0.792.