View source: R/NNCTFunctions.r
NN | R Documentation |
Returns the index (or indices) of the nearest neighbor(s) of subject i given data set or IPD matrix x
.
It will yield a vector
if there are ties, and subject indices correspond to rows (i.e. rows 1:n
) if x
is the data set and to rows or columns if x
is the IPD matrix.
The argument is.ipd
is a logical argument (default=TRUE
) to determine the structure of the argument x
.
If TRUE
, x
is taken to be the inter-point distance (IPD) matrix, and if FALSE
, x
is taken to be the data set
with rows representing the data points.
NN(x, i, is.ipd = TRUE, ...)
x |
The IPD matrix (if |
i |
index of (i.e., row number for) the subject whose NN is to be found. |
is.ipd |
A logical parameter (default= |
... |
are for further arguments, such as |
Returns the index (indices) i.e. row number(s) of the NN of subject i
Elvan Ceyhan
kNN
and NNsub
#3D data points n<-20 #or try sample(1:20,1) Y<-matrix(runif(3*n),ncol=3) ipd<-ipd.mat(Y) NN(ipd,1) NN(Y,1,is.ipd = FALSE) NN(ipd,5) NN(Y,5,is.ipd = FALSE) NN(Y,5,is.ipd = FALSE,method="max") #1D data points X<-as.matrix(runif(15)) # 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) NN(ipd,1) NN(ipd,5) #with possible ties in the data Y<-matrix(round(runif(30)*10),ncol=3) ny<-nrow(Y) ipd<-ipd.mat(Y) for (i in 1:ny) cat(i,":",NN(ipd,i),"|",NN(Y,i,is.ipd = FALSE),"\n")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.