R/cosnn.R

Defines functions cosnn

Documented in cosnn

################################
#### Discriminant analysis for directional data
#### using the k-NN alorithm,
#### Tsagris Michail 01/2016
#### mtsagris@yahoo.gr
################################
cosnn <- function(xnew, x, k = 5, index = FALSE, rann = FALSE) {
  xnew <- matrix(xnew, ncol = dim(x)[2])  ## makes sure xnew is a matrix
  nu <- dim(xnew)[1]
  if ( index ) {
    if (rann) {
      disa <-  t( RANN::nn2( data = x, query = xnew, k = k )$nn.idx )
    } else disa <- Rfast::dista(xnew, x, k = k, square = TRUE, index = TRUE )
  } else {
    if (rann) {
      disa <-  t( RANN::nn2(data = x, query = xnew, k = k )$nn.dists^2 )
    } else disa <- Rfast::dista(xnew, x, k = k, square = TRUE)
    disa <- Rfast::colSort( 0.5 * disa - 1)
    disa[ disa >= 1 ] <- 1
    disa[ disa <=  -1 ] <-  -1
    disa <- acos(disa)
  }
  disa
}

Try the Directional package in your browser

Any scripts or data that you put into this service are public.

Directional documentation built on Oct. 12, 2023, 1:07 a.m.