statable Statistics for study and research

How do you predict a new case from similar cases? (k-nearest neighbours, distance, standardising)

In brief

k-nearest neighbours (kNN) is one of the simplest learning methods: for a new case it looks for the k training cases that are most similar to it in the features, and predicts the class that is most common among these k. Similar means: small distance, measured as with a ruler in the scatter plot (Euclidean distance). Nothing is learnt in advance; the method only remembers the training cases.

An example by hand

A penguin from the test data has a bill depth of 19.3 mm and weighs 3450 g; it is female, but the method does not know that. Its five nearest neighbours in the training data (distance in standardised units):

An example by hand
NeighbourBill depthBody massDistanceSex
119.2 mm3500 g0.081male
219.4 mm3525 g0.108female
319.0 mm3450 g0.149female
419.0 mm3425 g0.152male
519.1 mm3550 g0.161male

The same case, three answers: k is the most important setting of the method. How to choose k without using the test data is shown in the unit on overfitting.

Why standardise?

Distance counts every unit equally. In the training data body mass has a standard deviation of 786.4 g, bill depth one of 2.01 mm. If two animals are 2 mm and 100 g apart, raw bill depth contributes 4 and mass 10000 to the squared distance: bill depth plays almost no role. Standardised (each variable divided by its standard deviation) the contributions are 0.99 and 0.016; now the difference in bill depth counts, because it is large relative to the usual spread. Without standardising, the three nearest neighbours of the penguin above all weigh exactly 3450 g; their bill depth (19.0, 18.8 and 18.6 mm) only counts when the mass is the same. On the 100 test animals the error at k = 15 thus rises from 15% to 32%.

The mean and standard deviation come only from the training data (means 17.15 mm and 4207.8 g); the same numbers are applied to the test data.

When do I use it?

Requirements

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.

With your own data: New analysis → "Can something be predicted?" → in the result "How well does my model predict new cases? (k-nearest neighbours, cross-validation)". There you choose a group as the target (2 to 8 groups) and number columns as features. Statable puts 70 % into the training data, chooses k with 5-fold cross-validation in the training data, and shows accuracy, confusion matrix and the error curve. "As R script with data (.zip)" recalculates the same in R with the same split.

Tied votes (with two classes only with an even k or equally distant neighbours): Statable takes the class of the nearest neighbour. In class::knn, R then decides at random.

Limits

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

  • kNN: majority of the k most similar training cases.
  • Standardise features, using statistics from the training data.

Related

Open in Statable