Fit models and make predictions with a KNN classifier

Share:

Description

These functions are used to apply the generic cross-validation mechanism to a K-nearest neighbors (KNN) classifier.

Usage

1
2
learnKNN(data, status, params, pfun)
predictKNN(newdata, details, status, ...)

Arguments

data

The data matrix, with rows as features and columns as the samples to be classified.

status

A factor, with two levels, classifying the samples. The length must equal the number of data columns.

params

A list of additional parameters used by the classifier; see Details.

pfun

The function used to make predictions on new data, using the cross-validated classifier.

newdata

Another data matrix, with the same number of rows as data.

details

A list of additional parameters describing details about the particular classifier; see Details.

...

Optional extra parameters required by the generic "predict" method.

Details

The input arguments to both learnKNN and predictKNN are dictated by the requirements of the general cross-validation mechanism provided by the Modeler-class.

The implementation uses the knn method from the class package. The params argument to learnKNN must be alist that at least includes the component k that specifies the number of neighbors used.

Value

The learnKNN function returns an object of the FittedModel-class, logically representing a KNN classifier that has been fitted on a training data set.

The predictKNN function returns a factor containing the predictions of the model when applied to the new data set.

Author(s)

Kevin R. Coombes <krc@silicovore.com>

References

Ripley, B. D. (1996) Pattern Recognition and Neural Networks. Cambridge.

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

See Also

See Modeler-class and Modeler for details about how to peform cross-validation. See FittedModel-class and FittedModel for details about the structure of the object returned by learnPCALR.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# simulate some data
data <- matrix(rnorm(100*20), ncol=20)
status <- factor(rep(c("A", "B"), each=10))

# set up the parameter list
knn.params <- list(k=5)

# learn the model
fm <- learnKNN(data, status, knn.params, predictKNN)

# Make predictions on some new simulated data
newdata <- matrix(rnorm(100*30), ncol=30)
predictKNN(newdata, fm@details, status)