knn.var {knnTree}R Documentation

K-Nearest Neighbor Classification With Variable Selection

Description

Construct or predict with k-nearest-neighbor classifiers, using cross-validation to select variables by forward or backward selection, to choose the best k and to choose scaling methods.

Usage

knn.var (train, test, k.vec = seq (1, 31, by=2), 
theyre.the.same=FALSE,
return.all.rates=FALSE, scaling = 1, backward = FALSE, max.steps=-1, 
save.call = FALSE, 
verbose = 0, use.big=TRUE)

Arguments

train
test
k.vec
theyre.the.same
return.all.rates
scaling
backward
max.steps
save.call
verbose
use.big

{logical, TRUE if the C code should try to use a technique that uses more memory but runs faster.}

Details

R{knn.var} constructs a k-nearest-neighbor classifier using Euclidean metric. Leave-one-out cross-validation together with stepwise (forward or backward, but not both) selection is used to find the best set of variables to include, the best choice of k, and whether the data should be scaled.

Value

Object of class knn. This is a list with between six and eight of the following components:
which: logical vector, one per input variable; the i-th element of which is TRUE if the i-th input variable is in the classifier
rate: Smallest misclassification rate acheived by algorithm. If return.all.rates is TRUE this is a vector of error rates, one for each element of k.vec
best.k: Number giving the optimal value of k, chosen from among the elements of k.vec.
scaled: indicator of best scaling. FALSE means no scaling was used; TRUE means scaling was used.
n: the number of observations in the training set
col.sds: Numeric vector of scaling factors, present only if scaled != 0. If scaled = 1 these are column SD's; if scaled = 2 they are MAD's.
pure: logical, TRUE if every item in the training set had the same class. If a training set is pure then all the elements of which are FALSE, best.k is taken to be the first element of k.vec and scaled is set to 0.
call: a copy of the call used to create the object, if save.call was TRUE

Author(s)

Sam Buttrey buttrey@nps.navy.mil

References

Buttrey and Karo, 2002

See Also

knnTree

Examples

data(iris)
set.seed (3)
samp <- sample (nrow(iris), size = 75, replace=FALSE)
knn.var (iris[samp,c(5, 1:4)]) # Build classifier
# Output produced by this-is-escaped-codenormal-bracket33bracket-normal
## Not run: This knn classifier is based on 75 observations.
It uses 1 out of 4 variables without scaling.

Training rate is 0.01333 , achieved at k = 1## End(Not run)
iris.knn <- knn.var (iris[samp,c(5, 1:4)]) # Build and save, then predict
predict (iris.knn, iris[-samp,c(5, 1:4)], iris[samp, c(5, 1:4)])
## Not run: 
$rate
[1] 0.08
## End(Not run)

[Package knnTree version 1.2.4 Index]