Last data update: 2014.03.03

R: Optimal Weighted Nearest Neighbor Classification
ownnR Documentation

Optimal Weighted Nearest Neighbor Classification

Description

This function implements Samworth's optimal weighting scheme for k nearest neighbor classification. The performance improvement is greatest when the dimension is 4 as reported in the reference.

Usage

  ownn(train, test, cl, testcl=NULL, k = NULL, prob = FALSE,
      algorithm=c("kd_tree", "cover_tree", "brute"))

Arguments

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.

testcl

factor of true classifications of testing set for error rate calculation.

k

number of neighbours considered, chosen by 5-fold cross-validation if not supplied.

prob

if this is true, the proportion of the weights for the winning class are returned as attribute prob.

algorithm

nearest neighbor search algorithm.

Value

a list includes k, predictions by ordinary knn, optimal weighted knn and bagged knn, and accuracies if class labels of test data set are given.

Author(s)

Shengqiao Li. To report any bugs or suggestions please email: shli@stat.wvu.edu.

References

Richard J. Samworth (2012), “Optimal Weighted Nearest Neighbor Classifiers,” Annals of Statistics, 40:5, 2733-2763.

See Also

knn and knn in class.

Examples

    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)))
    testcl <- factor(c(rep("s",25), rep("c",25), rep("v",25)))
    out <- ownn(train, test, cl, testcl)
    out

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(FNN)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/FNN/ownn.rd_%03d_medium.png", width=480, height=480)
> ### Name: ownn
> ### Title: Optimal Weighted Nearest Neighbor Classification
> ### Aliases: ownn
> ### Keywords: classif nonparametric
> 
> ### ** Examples
> 
>     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)))
>     testcl <- factor(c(rep("s",25), rep("c",25), rep("v",25)))
>     out <- ownn(train, test, cl, testcl)
>     out
$k
[1] 5

$knnpred
 [1] s s s s s s s s s s s s s s s s s s s 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 v c v v v v c v v v v v v v v v v v
Levels: c s v

$ownnpred
 [1] s s s s s s s s s s s s s s s s s s s 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 v c v v v v c v v v v v v v v v v v
Levels: c s v

$bnnpred
 [1] s s s s s s s s s s s s s s s s s s s s s s s s s c c c 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 v c v v v v c v v v v v v v v v v v
Levels: c s v

$accuracy
      knn      ownn       bnn 
0.9200000 0.9200000 0.9333333 

> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>