vcr.knn.train {classmap}R Documentation

Carry out a k-nearest neighbor classification on training data, and prepare to visualize its results.

Description

Carries out a k-nearest neighbor classification on the training data. Various additional output is produced for the purpose of constructing graphical displays such as the classmap.

Usage

vcr.knn.train(X, y, k)

Arguments

X

This can be a rectangular matrix or data frame of (already standardized) measurements, or a dist object obtained from stats::dist or cluster::daisy. Missing values are not allowed.

y

factor with the given (observed) class labels. There need to be non-missing y in order to be able to train the classifier.

k

the number of nearest neighbors used. It can be selected by running cross-validation using a different package.

Value

A list with components:

yint

number of the given class of each case. Can contain NA's.

y

given class label of each case. Can contain NA's.

levels

levels of y

predint

predicted class number of each case. Always exists.

pred

predicted label of each case.

altint

number of the alternative class. Among the classes different from the given class, it is the one with the highest posterior probability. Is NA for cases whose y is missing.

altlab

label of the alternative class. Is NA for cases whose y is missing.

PAC

probability of the alternative class. Is NA for cases whose y is missing.

figparams

parameters used to compute fig.

fig

distance of each case i from each class g. Always exists.

farness

farness of each case from its given class. Is NA for cases whose y is missing.

ofarness

for each case i, its lowest fig[i,g] to any class g. Always exists.

k

the requested number of nearest neighbors, from the arguments. Will also be used for classifying new data.

ktrues

for each case this contains the actual number of elements in its neighborhood. This can be higher than k due to ties.

counts

a matrix with 3 columns, each row representing a case. For the neighborhood of each case it says how many members it has from the given class, the predicted class, and the alternative class. The first and third entry is NA for cases whose y is missing.

X

If the argument X was a data frame or matrix of coordinates, as.matrix(X) is returned here. This is useful for classifying new data.

Author(s)

Raymaekers J., Rousseeuw P.J.

References

Raymaekers J., Rousseeuw P.J., Hubert M. (2021). Class maps for visualizing classification results. Technometrics, appeared online. doi: 10.1080/00401706.2021.1927849(link to open access pdf)

See Also

vcr.knn.newdata, classmap, silplot, stackedplot

Examples

vcrout <- vcr.knn.train(iris[, 1:4], iris[, 5], k = 5)
confmat.vcr(vcrout)
stackedplot(vcrout)
classmap(vcrout, "versicolor", classCols = 2:4)
# The cases misclassified as virginica are shown in blue.

# For more examples, we refer to the vignette:
## Not run: 
vignette("K_nearest_neighbors_examples")

## End(Not run)

[Package classmap version 1.2.3 Index]