statable Statistics for study and research

Is my model just memorising? (Overfitting, training and test error, cross-validation)

In brief

A model is overfitted when it also learns chance features of the training data: on the training data it does very well, on new cases clearly worse. This shows as the gap between training error and test error. The setting of the model, for kNN the number k, is chosen with cross-validation within the training data. The test data stay untouched until the end.

An example with real data

Sex of the penguins from bill depth and body mass, 233 animals in training, 100 in test, measures standardised:

Choosing k with cross-validation

In 5-fold cross-validation the 233 training animals are divided into 5 equally sized parts (folds). Five times the method learns from four parts and predicts the fifth; in this way every animal is predicted exactly once without having been part of the learning. This is done for every k. In the example the cross-validation error is smallest at k = 17: 7.7% (18 of 233 animals wrong). Only then does one look at the test data once: with k = 17 the test error is 16%.

Why not take the k with the smallest test error straight away? That would be k = 37 or 43 with 13%. Whoever chooses that way tunes the model to the test data; the 13% is then too good, because among many k there is by chance one that fits exactly these 100 animals. The test data would no longer be an independent check.

When do I use it?

Requirements

In every fold too: standardise and do all other learning only with the remaining folds. The cases are independent of one another; with repeated measurements of the same person, all measurements belong in the same fold.

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.

The error curve shows three lines against k: training, test and cross-validation. The button "take k from the cross-validation" sets the best k; if tied, the larger one, that is the smoother model.

Reading and reporting the result

k was chosen with 5-fold cross-validation in the training data (k = 17, error 7.7%); on the test data the error was 16%.

Limits

The cross-validation error is also an estimate subject to chance; a different division into folds gives somewhat different numbers. That it is smaller than the test error here is chance of the split, not a rule.

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

  • Overfitting: small training error, large test error.
  • Choose settings with cross-validation in training, the test once at the end.

Related

Open in Statable