Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

cosnn

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)

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.

Details

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

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.

Author(s)

Michail Tsagris

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

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

Examples

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)

Directional

A Collection of R Functions for Directional Data Analysis

v4.9
GPL-2
Authors
Michail Tsagris, Giorgos Athineou, Anamul Sajib, Eli Amson, Micah J. Waldstein
Initial release
2021-03-26

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.