statable Statistics for study and research

How often does a classification model hit? (Confusion matrix, accuracy, sensitivity, specificity)

In brief

The confusion matrix counts for the test data how often each true class was predicted as each class: rows are the true class, columns the prediction. The hits are on the diagonal. From it follow the accuracy (share of all correct), the error rate (1 minus accuracy) and for each class the share correctly identified (sensitivity, recall). With two classes, the share of correctly identified cases of the second class is also called specificity.

An example with real data

kNN with k = 15 predicts the sex of the 100 test penguins:

An example with real data
true \ predictedfemalemaletotal
female43952
male64248

Comparing with guessing

An accuracy needs a comparison. Whoever says "female" every time without any measure is right 52% of the time here, because 52 of the 100 test animals are female. So 85% is clearly better than guessing. If one class were rare, say 5 in 100, always naming the common class would already reach 95% without recognising a single rare case. That is why the shares per class always belong with it.

When do I use it?

For every classification that is checked on test data: kNN, logistic regression, decision tree, discriminant analysis. If the costs of the two kinds of error differ (missing a disease versus a false alarm), the shares per class count more than the accuracy.

Requirements

Only test data or predictions from cross-validation count. A confusion matrix from the training data looks too good.

How it works in Statable

  1. Open Understand > Learning lab > tab "Predicting (k-nearest neighbours)" (Ctrl+K: "predicting").
  2. Choose "Example: sex from bill depth and body mass". With seed 1 and 70% training data Statable splits the animals differently from R; the numbers in this article come from R and therefore differ a little. The picture stays the same.
  3. Move the slider k, switch "standardise measures" on and off and compare the sentences, the scatter plot, the confusion matrix and the error curve.

Whoever predicts a probability rather than a class, for example with logistic regression, can move the threshold. The ROC curve shows sensitivity and specificity for all thresholds; the area under it (AUC) sums them up in one number.

Reading and reporting the result

On 100 test animals the model correctly identified 82.7% of the female and 87.5% of the male penguins (accuracy 85%; always guessing the more common class: 52%).

Limits

With 52 and 48 cases per class a single animal moves the shares by about 2 percentage points. Small differences between two models are therefore often chance.

What is often misunderstood

Sources

James, G., Witten, D., Hastie, T. & Tibshirani, R. (2021). An Introduction to Statistical Learning (2nd ed.). Springer; free as PDF. Ch. 2.2 (classification, k-nearest neighbours, training and test error), ch. 4 (classification, confusion matrix, ROC), ch. 5.1 (cross-validation). Venables, W. N. & Ripley, B. D. (2002). Modern Applied Statistics with S (4th ed.). Springer (R package class, function knn). Data: Gorman, K. B., Williams, T. D. & Fraser, W. R. (2014). PLoS ONE, 9(3), e90081 (palmerpenguins, CC0).

Take away

  • Rows true, columns predicted, diagonal correct.
  • Always compare accuracy with guessing and with the shares per class.

Related

Open in Statable