Learn R Programming

Directional (version 7.0)

The k-nearest neighbours using the cosinus distance: The k-nearest neighbours using the cosinus distance

Description

The k-nearest neighbours using the cosinus distance.

Usage

cosnn(xnew, x, k = 5, index = FALSE, rann = FALSE)

Value

A matrix with the shortest distance of each xnew from x, if index is FALSE, or the indices of the nearest neighbours of each xnew from x, if index is TRUE.

Arguments

xnew

The new data whose k-nearest neighbours are to be found.

x

The data, a numeric matrix with unit vectors.

k

The number of nearest neighbours, set to 5 by default. It can also be a vector with many values.

index

If you want the indices of the closest observations set this equal to TRUE.

rann

If you have large scale datasets and want a faster k-NN search, you can use kd-trees implemented in the R package "RANN". In this case you must set this argument equal to TRUE.

Author

Michail Tsagris.

R implementation and documentation: Michail Tsagris mtsagris@uoc.gr.

Details

The shortest distances or the indices of the k-nearest neighbours using the cosinus distance are returned.

References

Tsagris M. and Alenazi A. (2019). Comparison of discriminant analysis methods on the sphere. Communications in Statistics: Case Studies, Data Analysis and Applications, 5(4): 467--491.

See Also

dirknn, dirknn.tune

Examples

Run this code
xnew <- rvmf(10, rnorm(3), 5)
x <- rvmf(50, rnorm(3), 5)
a <- cosnn(xnew, x, k = 5)
b <- cosnn(xnew, x, k = 5, index = TRUE)

Run the code above in your browser using DataLab