50% off | Unlimited Data & AI Learning

Last chance! 50% off unlimited learning

Sale ends in


nnspat (version 0.1.2)

funs.kNNdist: Functions for the \(k^{th}\) and k NN distances

Description

Two functions: kthNNdist and kNNdist.

kthNNdist returns the distances between subjects and their \(k^{th}\) NNs. The output is an \(n \times 2\) matrix where \(n\) is the data size and first column is the subject index and second column contains the corresponding distances to \(k^{th}\) NN subjects.

kNNdist returns the distances between subjects and their k NNs. The output is an \(n \times (k+1)\) matrix where \(n\) is the data size and first column is the subject index and the remaining k columns contain the corresponding distances to k NN subjects.

Usage

kthNNdist(x, k, is.ipd = TRUE, ...)

kNNdist(x, k, is.ipd = TRUE, ...)

Value

kthNNdist returns an \(n \times 2\) matrix where \(n\) is data size (i.e., number of subjects) and first column is the subject index and second column is the \(k^{th}\) NN distances.

kNNdist returns an \(n \times (k+1)\) matrix where \(n\) is data size (i.e., number of subjects) and first column is the subject index and the remaining k columns contain the corresponding distances to k NN subjects.

Arguments

x

The IPD matrix (if is.ipd=TRUE) or a data set of points in matrix or data frame form where points correspond to the rows (if is.ipd = FALSE).

k

Integer specifying the number of NNs (of subjects).

is.ipd

A logical parameter (default=TRUE). If TRUE, x is taken as the inter-point distance matrix, otherwise, x is taken as the data set with rows representing the data points.

...

are for further arguments, such as method and p, passed to the dist function.

Author

Elvan Ceyhan

See Also

NNdist and NNdist2cl

Examples

Run this code
#Examples for kthNNdist
#3D data points, gives NAs when n<=k
n<-20  #or try sample(1:20,1)
Y<-matrix(runif(3*n),ncol=3)
ipd<-ipd.mat(Y)
kthNNdist(ipd,3)
kthNNdist(Y,3,is.ipd = FALSE)
kthNNdist(ipd,5)
kthNNdist(Y,5,is.ipd = FALSE)
kthNNdist(Y,3,is.ipd = FALSE,method="max")

#1D data points
X<-as.matrix(runif(5)) # need to be entered as a matrix with one column
#(i.e., a column vector), hence X<-runif(5) would not work
ipd<-ipd.mat(X)
kthNNdist(ipd,3)

#Examples for kNNdist
#3D data points, gives NAs if n<=k for n,n+1,...,kNNs
n<-20  #or try sample(1:20,1)
Y<-matrix(runif(3*n),ncol=3)
ipd<-ipd.mat(Y)
kNNdist(ipd,3)
kNNdist(ipd,5)
kNNdist(Y,5,is.ipd = FALSE)

kNNdist(Y,5,is.ipd = FALSE,method="max")

kNNdist(ipd,1)
kthNNdist(ipd,1)

#1D data points
X<-as.matrix(runif(5)) # need to be entered as a matrix with one column
#(i.e., a column vector), hence X<-runif(5) would not work
ipd<-ipd.mat(X)
kNNdist(ipd,3)

Run the code above in your browser using DataLab