knn3: k-Nearest Neighbour Classification

Description Usage Arguments Details Value Author(s) Examples

Description

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

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
## S3 method for class 'formula'
knn3(formula, data, subset, na.action, k = 5, ...) 

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

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

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

Arguments

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 NAs.

k

number of neighbours considered.

x

a matrix of training set predictors

y

a factor vector of training set classes

...

additional parameters to pass to knn3Train. However, passing prob = FALSE will be over–ridden.

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 kth largest are included. If false, a random selection of distances equal to the kth 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

1
2
3
4
5
6
7
8
9
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) 

Example output

Loading required package: lattice
Loading required package: ggplot2
 [1] "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s"
[20] "s" "s" "s" "s" "s" "s" "c" "c" "v" "c" "c" "c" "c" "c" "v" "c" "c" "c" "c"
[39] "c" "c" "c" "c" "c" "c" "c" "c" "c" "c" "c" "c" "v" "c" "c" "v" "v" "v" "v"
[58] "v" "c" "v" "v" "v" "v" "c" "v" "v" "v" "v" "v" "v" "v" "v" "v" "v" "v"
attr(,"prob")
              c s         v
 [1,] 0.0000000 1 0.0000000
 [2,] 0.0000000 1 0.0000000
 [3,] 0.0000000 1 0.0000000
 [4,] 0.0000000 1 0.0000000
 [5,] 0.0000000 1 0.0000000
 [6,] 0.0000000 1 0.0000000
 [7,] 0.0000000 1 0.0000000
 [8,] 0.0000000 1 0.0000000
 [9,] 0.0000000 1 0.0000000
[10,] 0.0000000 1 0.0000000
[11,] 0.0000000 1 0.0000000
[12,] 0.0000000 1 0.0000000
[13,] 0.0000000 1 0.0000000
[14,] 0.0000000 1 0.0000000
[15,] 0.0000000 1 0.0000000
[16,] 0.0000000 1 0.0000000
[17,] 0.0000000 1 0.0000000
[18,] 0.0000000 1 0.0000000
[19,] 0.0000000 1 0.0000000
[20,] 0.0000000 1 0.0000000
[21,] 0.0000000 1 0.0000000
[22,] 0.0000000 1 0.0000000
[23,] 0.0000000 1 0.0000000
[24,] 0.0000000 1 0.0000000
[25,] 0.0000000 1 0.0000000
[26,] 1.0000000 0 0.0000000
[27,] 1.0000000 0 0.0000000
[28,] 0.4000000 0 0.6000000
[29,] 1.0000000 0 0.0000000
[30,] 1.0000000 0 0.0000000
[31,] 1.0000000 0 0.0000000
[32,] 1.0000000 0 0.0000000
[33,] 1.0000000 0 0.0000000
[34,] 0.4000000 0 0.6000000
[35,] 0.8000000 0 0.2000000
[36,] 1.0000000 0 0.0000000
[37,] 1.0000000 0 0.0000000
[38,] 1.0000000 0 0.0000000
[39,] 1.0000000 0 0.0000000
[40,] 1.0000000 0 0.0000000
[41,] 1.0000000 0 0.0000000
[42,] 1.0000000 0 0.0000000
[43,] 1.0000000 0 0.0000000
[44,] 1.0000000 0 0.0000000
[45,] 1.0000000 0 0.0000000
[46,] 1.0000000 0 0.0000000
[47,] 1.0000000 0 0.0000000
[48,] 1.0000000 0 0.0000000
[49,] 1.0000000 0 0.0000000
[50,] 1.0000000 0 0.0000000
[51,] 0.0000000 0 1.0000000
[52,] 0.8000000 0 0.2000000
[53,] 0.6000000 0 0.4000000
[54,] 0.0000000 0 1.0000000
[55,] 0.0000000 0 1.0000000
[56,] 0.0000000 0 1.0000000
[57,] 0.0000000 0 1.0000000
[58,] 0.0000000 0 1.0000000
[59,] 0.6666667 0 0.3333333
[60,] 0.2000000 0 0.8000000
[61,] 0.0000000 0 1.0000000
[62,] 0.0000000 0 1.0000000
[63,] 0.0000000 0 1.0000000
[64,] 0.6000000 0 0.4000000
[65,] 0.0000000 0 1.0000000
[66,] 0.0000000 0 1.0000000
[67,] 0.0000000 0 1.0000000
[68,] 0.0000000 0 1.0000000
[69,] 0.0000000 0 1.0000000
[70,] 0.0000000 0 1.0000000
[71,] 0.0000000 0 1.0000000
[72,] 0.2000000 0 0.8000000
[73,] 0.0000000 0 1.0000000
[74,] 0.0000000 0 1.0000000
[75,] 0.2000000 0 0.8000000

caret documentation built on May 2, 2019, 5:47 p.m.

Related to knn3 in caret...