knn.ani {animation}R Documentation

Demonstrate kNN classification algorithm on the 2D plane

Description

Demonstrate the process of k-Nearest Neighbour classification on the 2D plane.

Usage

knn.ani(train, test, cl, k = 10, interact = FALSE, 
    control = ani.control(), ...)

Arguments

train matrix or data frame of training set cases containing only 2 columns
test matrix or data frame of test set cases. A vector will be interpreted as a row vector for a single case. It should also contain only 2 columns. This data set will be ignored if interact = TRUE; see interact below.
cl factor of true classifications of training set
k number of neighbours considered.
interact logical. If TRUE, the user will have to choose a test set for himself using mouse click on the screen; otherwise compute kNN classification based on argument test.
control control parameters for the animation; see ani.control
... other arguments passed to ani.control

Details

For each row of the test set, the k nearest (in Euclidean distance) training set vectors are found, and the classification is decided by majority vote, with ties broken at random. For a single test sample point, the basic steps are:

  1. locate the test point
  2. compute the distances between the test point and all points in the training set
  3. find k shortest distances and the corresponding training set points
  4. vote for the result (find the maximum in the table for the true classifications)

Value

A vector of class labels for the test set.

Note

There is a special restriction (only two columns) on the training and test data set just for sake of the convenience for making a scatterplot. This is only a rough demonstration; for practical applications, please refer to existing kNN functions such as knn, etc.

If either one of train and test is missing, there'll be random matrices prepared for them. (It's the same for cl.)

Author(s)

Yihui Xie

References

Venables, W. N. and Ripley, B. D. (2002) Modern Applied Statistics with S. Fourth edition. Springer.

See Also

ani.start, ani.stop, knn

Examples

## a binary classification problem 
x = matrix(c(rnorm(80, mean = -1), rnorm(80, mean = 1)), 
    ncol = 2, byrow = TRUE) 
y = matrix(rnorm(20, mean = 0, sd = 1.2), ncol = 2) 
knn.ani(train = x, test = y, cl = rep(c("first class", "second class"),
    each = 40), k = 30, control = ani.control(interval = 2)) 

## Not run:  

ani.start() 
op = par(mar = c(3.5, 3.5, 3, 1), mgp = c(2, 0.5, 
    0), cex.axis = 0.75, tcl = -0.3) 
knn.ani(train = x, test = y, cl = rep(c("first class", "second class"),
    each = 40), k = 30, control = ani.control(saveANI = TRUE, 
    width = 600, height = 600)) 
par(op) 
ani.stop() 

## this is an interactive demo; just click your mouse
knn.ani(train = x, cl = rep(c("first class", "second class"), 
    each = 40), k = 30, nmax = 5, interact = TRUE) 

## End(Not run)

[Package animation version 0.2-0 Index]