NNsub: Finding the index of the NN of a given point among a subset...

View source: R/NNCTFunctions.R

NNsubR Documentation

Finding the index of the NN of a given point among a subset of points

Description

Returns the index (indices) of the nearest neighbor(s) of subject i (other than subject i) among the indices of points provided in the subsample ss using the given data set or IPD matrix x. The indices in ss determine the columns of the IPD matrix to be used in this function. 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.

Usage

NNsub(ss, x, i, is.ipd = TRUE, ...)

Arguments

ss

indices of subjects (i.e., row indices in the data set) among with the NN of subject is to be found

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).

i

index of (i.e., row number for) the subject whose NN is to be found.

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.

Value

Returns a list with the elements

base.ind

index of the base subject

ss.ind

the index (indices) i.e., row number(s) of the NN of subject i among the subjects with indices provided in ss

ss.dis

distance from subject i to its NN among the subjects in ss

Author(s)

Elvan Ceyhan

See Also

NN and kNN

Examples

#3D data points 
n<-20  #or try sample(1:20,1)
Y<-matrix(runif(3*n),ncol=3)
ipd<-ipd.mat(Y)
#indices of the subsample ss
ss<-sample(1:n,floor(n/2),replace=FALSE)
NNsub(ss,ipd,2)
NNsub(ss,Y,2,is.ipd = FALSE)
NNsub(ss,ipd,5)

#1D data points
n<-15
X<-as.matrix(runif(n))# need to be entered as a matrix with one column
#(i.e., a column vector), hence X<-runif(n) would not work
ipd<-ipd.mat(X)
#two class case
clab<-sample(1:2,n,replace=TRUE) #class labels
#indices of the subsample ss
ss<-sample(1:n,floor(n/2),replace=FALSE)
NNsub(ss,ipd,2)
NNsub(ss,ipd,5)

#with possible ties in the data
Y<-matrix(round(runif(60)*10),ncol=3)
ipd<-ipd.mat(Y)
ss<-sample(1:20,10,replace=FALSE) #class labels
NNsub(ss,ipd,2)
NNsub(ss,ipd,5)


nnspat documentation built on May 29, 2024, 10:03 a.m.