# funs.kNNdist2cl: Functions for the k^{th} and 'k' NN distances In nnspat: Nearest Neighbor Methods for Spatial Patterns

## Description

Two functions: kthNNdist2cl and kNNdist2cl.

kthNNdist2cl returns the distances between subjects from class i and their k^{th} NNs from class j. The output is a list with first entry (kth.nndist) is an n_i \times 3 matrix where n_i is the size of class i and first column is the subject index for class i, second column is the index of the k^{th} NN of class i subjects among class j subjects and third column contains the corresponding k^{th} NN distances. The other entries in the list are labels of base class and NN class and the value of k, respectively.

kNNdist2cl returns the distances between subjects from class i and their k NNs from class j. The output is a list with first entry (ind.knndist) is an n_i \times (k+1) matrix where n_i is the size of class i, first column is the indices of class i subjects, 2nd to (k+1)-st columns are the indices of k NNs of class i subjects among class j subjects. The second list entry (knndist) is an n_i \times k matrix where n_i is the size of class i and the columns are the kNN distances of class i subjects to class j subjects. The other entries in the list are labels of base class and NN class and the value of k, respectively.

The argument within.class.ind is a logical argument (default=FALSE) to determine the indexing of the class i subjects. If TRUE, index numbering of subjects is within the class, from 1 to class size (i.e., 1:n_i), according to their order in the original data; otherwise, index numbering within class is just the indices in the original data.

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

 1 2 3 kthNNdist2cl(x, k, i, j, lab, within.class.ind = FALSE, is.ipd = TRUE, ...) kNNdist2cl(x, k, i, j, lab, within.class.ind = FALSE, is.ipd = TRUE, ...) 

## 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 = FALSEALSE). k Integer specifying the number of NNs (of subjects). i, j class label of base class and NN classes, respectively. lab The vector of class labels (numerical or categorical) within.class.ind A logical parameter (default=FALSE). If TRUE, index numbering of subjects is within the class, from 1 to class size (i.e., 1:n_i), according to their order in the original data; otherwise, index numbering within class is just the indices in the original data. 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

kthNNdist2cl returns the list of elements

 kth.nndist n_i \times 3 matrix where n_i is the size of class i and first column is the subject index for class i, second column is the index of the k-th NN of class i subjects among class j subjects and third column contains the corresponding k-th NN distances, , returned by Zseg.ind.ct only base.class label of base class nn.class label of NN class k value of k in kNN

kNNdist2cl returns the list of elements

 ind.knndist n_i \times (k+1) matrix where n_i is the size of class i, first column is the indices of class i subjects, 2nd to (k+1)-st columns are the indices of k NNs of class i subjects among class j subjects. knndist n_i \times k matrix where n_i is the size of class i and the columns are the kNN distances of class i subjects to class j subjects. base.class label of base class nn.class label of NN class k value of k in kNN

## Author(s)

Elvan Ceyhan

NNdist2cl, kthNNdist and kNNdist
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 #Examples for kthNNdist2cl #3D data points n<-20 #or try sample(1:20,1) Y<-matrix(runif(3*n),ncol=3) ipd<-ipd.mat(Y) #two class case clab<-sample(1:2,n,replace=TRUE) #class labels table(clab) kthNNdist2cl(ipd,3,1,2,clab) kthNNdist2cl(Y,3,1,2,clab,is.ipd = FALSE) kthNNdist2cl(ipd,3,1,2,clab,within = TRUE) #three class case clab<-sample(1:3,n,replace=TRUE) #class labels table(clab) kthNNdist2cl(ipd,3,2,3,clab) #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 table(clab) kthNNdist2cl(ipd,3,1,2,clab) # here kthNNdist2cl(ipd,3,1,12,clab) #gives an error message kthNNdist2cl(ipd,3,"1",2,clab) #Examples for kNNdist2cl #3D data points n<-20 #or try sample(1:20,1) Y<-matrix(runif(3*n),ncol=3) ipd<-ipd.mat(Y) #two class case clab<-sample(1:2,n,replace=TRUE) #class labels table(clab) kNNdist2cl(ipd,3,1,2,clab) kNNdist2cl(Y,3,1,2,clab,is.ipd = FALSE) kNNdist2cl(ipd,3,1,2,clab,within = TRUE) #three class case clab<-sample(1:3,n,replace=TRUE) #class labels table(clab) kNNdist2cl(ipd,3,1,2,clab) #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 table(clab) kNNdist2cl(ipd,3,1,2,clab) kNNdist2cl(ipd,3,"1",2,clab) #here kNNdist2cl(ipd,3,"a",2,clab) #gives an error message