Generalized k Nearest Neighbors
Predicts the classes of new observations with k Nearest Neighbors based on an user-specified distance measure.
gknn(data, cl, newdata, nn = 5, distance = NULL, use.weights = FALSE, ...)
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.
## 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)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.