Description Usage Arguments Value Author(s) References See Also Examples
Predicts the classes of new observations with k Nearest Neighbors based on an user-specified distance measure.
1 |
data |
a numeric matrix in which each row represents an observation and each column
a variable. If |
cl |
a numeric vector of length |
newdata |
a numeric matrix in which each row represents a new observation for
which the class label should be predicted and each column consists of the same
variable as the corresponding column of |
nn |
an integer specifying the number of nearest neighbors used to classify the new observations. |
distance |
character vector naming the distance measure used to identify the
|
use.weights |
should the votes of the nearest neighbors be weighted by the reciprocal of the distances to the new observation when the class of a new observation should be predicted? |
... |
further arguments for the distance measure. If, e.g.,
|
The predicted classes of the new observations.
Holger Schwender, holger.schwender@udo.edu
Schwender, H.\ (2007). Statistical Analysis of Genotype and Gene Expression Data. Dissertation, Department of Statistics, University of Dortmund.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | ## Not run:
# Using the example from the function knn.
library(class)
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 <- c(rep(2, 25), rep(1, 25), rep(1, 25))
knn.out <- knn(train, test, as.factor(cl), k = 3, use.all = FALSE)
gknn.out <- gknn(train, cl, test, nn = 3)
# Both applications lead to the same predictions.
knn.out == gknn.out
# But gknn allows to use other distance measures than the Euclidean
# distance. E.g., the Manhattan distance.
gknn(train, cl, test, nn = 3, distance = "manhattan")
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.