k-NN algorithm using the arc cosinus distance

Description

It classifies new observations to some known groups via the k-NN algorithm.

Usage

1
dirknn(x, xnew, k = 5, ina, type = "S", mesos = TRUE)

Arguments

x

The data, a numeric matrix with unit vectors.

xnew

The new data whose membership is to be predicted, a numeric matrix with unit vectors.

k

The number of nearest neighbours, set to 5 by default.

ina

A variable indicating the groups of the data x.

type

If type is "S", the standard k-NN algorithm is to be used, else "NS" for the non standard one. See below (details) for more information.

mesos

A boolean variable used only in the case of the non standard algorithm (type="NS"). Should the average of the distances be calculated (TRUE) or not (FALSE)? If it is FALSE, the harmonic mean is calculated.

Details

The standard algorithm is to keep the k nearest observations and see the groups of these observations. The new observation is allocated to the most frequent seen group. The non standard algorithm is to calculate the classical mean or the harmonic mean of the k nearest observations for each group. The new observation is allocated to the group with the smallest mean distance.

Value

A vector including:

g

The predicted group.

Author(s)

Michail Tsagris

R implementation and documentation: Michail Tsagris <mtsagris@yahoo.gr> and Giorgos Athineou <athineou@csd.uoc.gr>

References

course webpage of Howard E. Haber. http://scipp.ucsc.edu/~haber/ph216/rotation_12.pdf

See Also

dirknn.tune, vmfda.pred, mix.vmf

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
k <- runif(4, 4, 20)
prob <- c(0.2, 0.4, 0.3, 0.1)
mu <- matrix(rnorm(16), ncol = 4)
mu <- mu / sqrt( rowSums(mu^2) )
da <- rmixvmf(200, prob, mu, k)
nu <- sample(1:200, 180)
x <- da$x[nu, ]
ina <- da$id[nu]
xx <- da$x[-nu, ]
id <- da$id[-nu]
a1 <- dirknn(x, xx, k = 5,  ina, type = "S", mesos = TRUE)
a2 <- dirknn(x, xx, k = 5,ina, type = "NS", mesos = TRUE)
a3 <- dirknn(x, xx, k = 5, ina, type = "S", mesos = FALSE)
a4 <- dirknn(x, xx, k = 5, ina, type = "NS", mesos = FALSE)
b <- vmfda.pred(xx, x, ina)
table(id, a1)
table(id, a2)
table(id, a3)
table(id, a4)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.