Unified (formula-based) interface version of the k-nearest neighbor
algorithm provided by class::knn()
.
mlKnn(train, ...)
ml_knn(train, ...)
# S3 method for class 'formula'
mlKnn(formula, data, k.nn = 5, ..., subset, na.action)
# Default S3 method
mlKnn(train, response, k.nn = 5, ...)
# S3 method for class 'mlKnn'
summary(object, ...)
# S3 method for class 'summary.mlKnn'
print(x, ...)
# S3 method for class 'mlKnn'
predict(
object,
newdata,
type = c("class", "prob", "both"),
method = c("direct", "cv"),
na.action = na.exclude,
...
)
a matrix or data frame with predictors.
further arguments passed to the classification method or its
predict()
method (not used here for now).
a formula with left term being the factor variable to predict
and the right term with the list of independent, predictive variables,
separated with a plus sign. If the data frame provided contains only the
dependent and independent variables, one can use the class ~ .
short
version (that one is strongly encouraged). Variables with minus sign are
eliminated. Calculations on variables are possible according to usual formula
convention (possibly protected by using I()
).
a data.frame to use as a training set.
k used for k-NN number of neighbor considered. Default is 5.
index vector with the cases to define the training set in use (this argument must be named, if provided).
function to specify the action to be taken if NA
s are
found. For ml_knn()
na.fail
is used by default. The calculation is
stopped if there is any NA
in the data. Another option is na.omit
,
where cases with missing values on any required variable are dropped (this
argument must be named, if provided). For the predict()
method, the
default, and most suitable option, is na.exclude
. In that case, rows with
NA
s in newdata=
are excluded from prediction, but reinjected in the
final results so that the number of items is still the same (and in the
same order as newdata=
).
a vector of factor for the classification.
an mlKnn object
a new dataset with same conformation as the training set (same variables, except may by the class for classification or dependent variable for regression). Usually a test set, or a new dataset to be predicted.
the type of prediction to return. "class"
by default, the
predicted classes. Other options are "prob"
the "probability" for the
different classes as assessed by the number of neighbors of these classes,
or "both"
to return classes and "probabilities",
"direct"
(default) or "cv"
. "direct"
predicts new cases in
newdata=
if this argument is provided, or the cases in the training set
if not. Take care that not providing newdata=
means that you just
calculate the self-consistency of the classifier but cannot use the
metrics derived from these results for the assessment of its performances.
Either use a different data set in newdata=
or use the alternate
cross-validation ("cv") technique. If you specify method = "cv"
then
cvpredict()
is used and you cannot provide newdata=
in that case.
ml_knn()
/mlKnn()
creates an mlKnn, mlearning object
containing the classifier and a lot of additional metadata used by the
functions and methods you can apply to it like predict()
or
cvpredict()
. In case you want to program new functions or extract
specific components, inspect the "unclassed" object using unclass()
.
mlearning()
, cvpredict()
, confusion()
, also class::knn()
and
ipred::predict.ipredknn()
that actually do the classification.
# Prepare data: split into training set (2/3) and test set (1/3)
data("iris", package = "datasets")
train <- c(1:34, 51:83, 101:133)
iris_train <- iris[train, ]
iris_test <- iris[-train, ]
# One case with missing data in train set, and another case in test set
iris_train[1, 1] <- NA
iris_test[25, 2] <- NA
iris_knn <- ml_knn(data = iris_train, Species ~ .)
summary(iris_knn)
#> Train dataset:
#> data frame with 0 columns and 0 rows
predict(iris_knn) # This object only returns classes
#> [1] setosa setosa setosa setosa setosa setosa
#> [7] setosa setosa setosa setosa setosa setosa
#> [13] setosa setosa setosa setosa setosa setosa
#> [19] setosa setosa setosa setosa setosa setosa
#> [25] setosa setosa setosa setosa setosa setosa
#> [31] setosa setosa setosa versicolor versicolor versicolor
#> [37] versicolor versicolor versicolor versicolor versicolor versicolor
#> [43] versicolor versicolor versicolor versicolor versicolor versicolor
#> [49] versicolor versicolor versicolor versicolor versicolor versicolor
#> [55] versicolor virginica versicolor versicolor versicolor versicolor
#> [61] versicolor versicolor versicolor versicolor versicolor versicolor
#> [67] virginica virginica virginica virginica virginica virginica
#> [73] versicolor virginica virginica virginica virginica virginica
#> [79] virginica virginica virginica virginica virginica virginica
#> [85] virginica virginica virginica virginica virginica virginica
#> [91] virginica virginica virginica versicolor virginica virginica
#> [97] virginica virginica virginica
#> Levels: setosa versicolor virginica
# Self-consistency, do not use for assessing classifier performances!
confusion(iris_knn)
#> 99 items classified with 96 true positives (error rate = 3%)
#> Predicted
#> Actual 01 02 03 (sum) (FNR%)
#> 01 setosa 33 0 0 33 0
#> 02 versicolor 0 32 1 33 3
#> 03 virginica 0 2 31 33 6
#> (sum) 33 34 32 99 3
# Use an independent test set instead
confusion(predict(iris_knn, newdata = iris_test), iris_test$Species)
#> 50 items classified with 47 true positives (error rate = 6%)
#> Predicted
#> Actual 01 02 03 04 (sum) (FNR%)
#> 01 setosa 16 0 0 0 16 0
#> 02 NA 0 0 0 0 0
#> 03 versicolor 0 1 15 1 17 12
#> 04 virginica 0 0 1 16 17 6
#> (sum) 16 1 16 17 50 6