kNN.plot: Visualizing the Optimal Number of k

View source: R/kNN.plot.R

kNN.plotR Documentation

Visualizing the Optimal Number of k

Description

Visualizing the Optimal Number of k for k-Nearest Neighbour (kNN) algorithm based on accuracy or Mean Square Error (MSE).

Usage

kNN.plot(formula, train, test, k.max = 10, scaler = FALSE, 
         base = "accuracy", reference = NULL, cutoff = NULL, 
         type = "class", report = FALSE, set.seed = NULL, ...)

Arguments

formula

a formula, with a response but no interaction terms. For the case of data frame, it is taken as the model frame (see model.frame).

train

data frame or matrix of train set cases.

test

data frame or matrix of test set cases.

k.max

the maximum number of neighbors to consider can either be a single value, with a minimum of 2, or a vector representing a range of values k.

scaler

a character with options FALSE (default), "minmax", and "zscore". Option "minmax" means no transformation. This option allows the users to use normalized version of the train and test sets for the kNN aglorithm.

base

base measurement: accuracy (default), error, or MSE for Mean Square Error.

reference

a factor of classes to be used as the true results.

cutoff

cutoff value for the case that the output of knn algorithm is vector of probabilites.

type

either "class" (default) for the predicted class or "prob" for model confidence values.

report

a character with options FALSE (default) and TRUE. Option TRUE reports the values of the base measurement.

set.seed

a single value, interpreted as an integer, or NULL.

...

options to be passed to kNN().

Author(s)

Reza Mohammadi a.mohammadi@uva.nl and Kevin Burke kevin.burke@ul.ie

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

kNN, scaler

Examples

data(risk)

partition_risk <- partition(data = risk, ratio = c(0.6, 0.4))

train <- partition_risk$part1
test  <- partition_risk$part1

kNN.plot(risk ~ income + age, train = train, test = test)
kNN.plot(risk ~ income + age, train = train, test = test, base = "error")

liver documentation built on April 3, 2025, 10:51 p.m.