knn3 {caret} | R Documentation

## k-Nearest Neighbour Classification

### Description

$k$-nearest neighbour classification that can return class votes for all classes.

### Usage

    knn3(x, ...)

    ## S3 method for class 'formula'
    knn3(formula, data, subset, na.action, k = 5, ...)

    ## S3 method for class 'data.frame'
    knn3(x, y, k = 5, ...)

    ## S3 method for class 'matrix'
    knn3(x, y, k = 5, ...)

    ## S3 method for class 'knn3'
    print(x, ...)

    knn3Train(train, test, cl, k = 1, l = 0, prob = TRUE, use.all = TRUE)

### Arguments

`x` | a matrix of training set predictors
`...` | additional parameters to pass to `knn3Train`. However, passing `prob = FALSE` will be over–ridden.
`formula` | a formula of the form `lhs ~ rhs` where `lhs` is the response variable and `rhs` a set of predictors.
`data` | optional data frame containing the variables in the model formula.
`subset` | optional vector specifying a subset of observations to be used.
`na.action` | function which indicates what should happen when the data contain `NA`s.
`k` | number of neighbours considered.
`y` | a factor vector of training set classes
`train` | matrix or data frame of training set cases.
`test` | matrix or data frame of test set cases. A vector will be interpreted as a row vector for a single case.
`cl` | factor of true classifications of training set
`l` | minimum vote for definite decision, otherwise `doubt`. (More precisely, less than `k-l` dissenting votes are allowed, even if `k` is increased by ties.)
`prob` | If this is true, the proportion of the votes for each class are returned as attribute `prob`.
`use.all` | controls handling of ties. If true, all distances equal to the `k`th largest are included. If false, a random selection of distances equal to the `k`th is chosen to use exactly `k` neighbours.

### Details

`knn3` is essentially the same code as `ipredknn` and `knn3Train` is a copy of `knn`. The underlying C code from the `class` package has been modified to return the vote percentages for each class (previously the percentage for the winning class was returned).

### Value

An object of class `knn3`. See `predict.knn3`.

### Author(s)

`knn` by W. N. Venables and B. D. Ripley and `ipredknn` by Torsten.Hothorn <Torsten.Hothorn@rzmail.uni-erlangen.de>, modifications by Max Kuhn and Andre Williams

### Examples

    irisFit1 <- knn3(Species ~ ., iris)

    irisFit2 <- knn3(as.matrix(iris[, -5]), iris[,5])

    data(iris3)
    train <- rbind(iris3[1:25,,1], iris3[1:25,,2], iris3[1:25,,3])
    test <- rbind(iris3[26:50,,1], iris3[26:50,,2], iris3[26:50,,3])
    cl <- factor(c(rep("s",25), rep("c",25), rep("v",25)))
    knn3Train(train, test, cl, k = 5, prob = TRUE)

[Package caret version 6.0-76 Index]