indexes which specify the rows of the dist.matrix to use as training set.
test
indexes which specify the rows of the dist.matrix to use as test set.
y
a vector of labels.
dist.matrix
the output from a call to knn.dist.
k
the number of nearest neighbors to consider.
agg.meth
method to combine responses of the nearest neighbors, defaults to "majority" for classification and "mean" for continuous responses.
ties.meth
method to handle ties for the k-th neighbor, the default is "min" which uses all ties, alternatives include "max" which uses none if there are ties for the k-th nearest neighbor, "random" which selects among the ties randomly and "first" which uses the ties in their order in the data.
Details
Predictions are calculated for each test case by aggregating the responses of the k-nearest neighbors among the training cases. k may be specified to be any positive integer less than the number of training cases, but is generally between 1 and 10.
The indexes for the training and test cases are in reference to the order of the entire data set as it was passed to knn.dist.
The aggregation may be any named function. By default, classification (factored responses) will use the majority class function and non-factored responses will use mean. Other options to consider include min, max, and median.
The ties are handled using the rank function. Further information may be found by examining the ties.method there.
Value
A vector of predictions whose length is the number of test cases.
Author(s)
Atina Dunlap Brooks
See Also
knn.dist,dist
Examples
#a quick classification example
x1 <- c(rnorm(20, mean=1), rnorm(20, mean=5))
x2 <- c(rnorm(20, mean=5), rnorm(20, mean=1))
y=rep(1:2,each=20)
x <- cbind(x1,x2)
train <- sample(1:40, 30)
#plot the training cases
plot(x1[train], x2[train], col=y[train]+1)
#predict the other cases
test <- (1:40)[-train]
kdist <- knn.dist(x)
preds <- knn.predict(train, test, y ,kdist, k=3, agg.meth="majority")
#add the predictions to the plot
points(x1[test], x2[test], col=as.integer(preds)+1, pch="+")
#display the confusion matrix
table(y[test], preds)
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(KODAMA)
Loading required package: e1071
Loading required package: plsgenomics
Loading required package: MASS
Loading required package: boot
Loading required package: parallel
Loading required package: class
Attaching package: 'KODAMA'
The following object is masked from 'package:plsgenomics':
transformy
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/KODAMA/knn.predict.Rd_%03d_medium.png", width=480, height=480)
> ### Name: knn.predict
> ### Title: KNN Prediction Routine using Pre-Calculated Distances
> ### Aliases: knn.predict
> ### Keywords: classifier
>
> ### ** Examples
>
> #a quick classification example
> x1 <- c(rnorm(20, mean=1), rnorm(20, mean=5))
> x2 <- c(rnorm(20, mean=5), rnorm(20, mean=1))
> y=rep(1:2,each=20)
> x <- cbind(x1,x2)
> train <- sample(1:40, 30)
> #plot the training cases
> plot(x1[train], x2[train], col=y[train]+1)
> #predict the other cases
> test <- (1:40)[-train]
> kdist <- knn.dist(x)
> preds <- knn.predict(train, test, y ,kdist, k=3, agg.meth="majority")
> #add the predictions to the plot
> points(x1[test], x2[test], col=as.integer(preds)+1, pch="+")
> #display the confusion matrix
> table(y[test], preds)
preds
1 2
1 6 0
2 0 4
>
>
>
>
>
> dev.off()
null device
1
>