# nnct: Nearest Neighbor Contingency Table (NNCT) In nnspat: Nearest Neighbor Methods for Spatial Patterns

 nnct R Documentation

## Nearest Neighbor Contingency Table (NNCT)

### Description

Returns the k \times k NNCT given the IPD matrix or data set x where k is the number of classes in the data set. Rows and columns of the NNCT are labeled with the corresponding class labels.

The argument ties is a logical argument (default=FALSE) to take ties into account or not. If TRUE a NN contributes 1/m to the NN count if it is one of the m tied NNs of a subject.

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

nnct(x, lab, ties = 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). lab The vector of class labels (numerical or categorical) ties A logical argument (default=FALSE) to take ties into account or not. If TRUE a NN contributes 1/m to the NN count if it is one of the m tied NNs of a subject. 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 the k \times k NNCT where k is the number of classes in the data set.

Elvan Ceyhan

### References

\insertAllCited

nnct.sub, scct, rct, and tct

### Examples

n<-20  #or try sample(1:20,1)
Y<-matrix(runif(3*n),ncol=3)
ipd<-ipd.mat(Y)
cls<-sample(1:2,n,replace = TRUE)  #or try cls<-rep(1:2,c(10,10))
nnct(ipd,cls)
nnct(ipd,cls,ties = TRUE)

nnct(Y,cls,is.ipd = FALSE)
nnct(Y,cls,is.ipd = FALSE,method="max")
nnct(Y,cls,is.ipd = FALSE,method="mink",p=6)

#with one class, it works but really uninformative
cls<-rep(1,n)
nnct(ipd,cls)

#cls as a factor
na<-floor(n/2); nb<-n-na
fcls<-rep(c("a","b"),c(na,nb))
nnct(ipd,fcls)

#cls as an unsorted factor
fcls1<-sample(c("a","b"),n,replace = TRUE)
nnct(ipd,fcls1)

fcls2<-sort(fcls1)
nnct(ipd,fcls2) #ipd needs to be sorted as well, otherwise this result will not agree with fcls1

nnct(Y,fcls1,ties = TRUE,is.ipd = FALSE)

#############
n<-40
Y<-matrix(runif(3*n),ncol=3)
ipd<-ipd.mat(Y)
cls<-sample(1:4,n,replace = TRUE)  #or try cls<-rep(1:2,c(10,10))
nnct(ipd,cls)
nnct(Y,cls,is.ipd = FALSE)

#cls as a factor
fcls<-rep(letters[1:4],rep(10,4))
nnct(ipd,fcls)

#1D data points
n<-20  #or try sample(1:20,1)
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)
cls<-sample(1:2,n,replace = TRUE)  #or try cls<-rep(1:2,c(10,10))
nnct(ipd,cls)

#cls as a factor
na<-floor(n/2); nb<-n-na
fcls<-rep(c("a","b"),c(na,nb))
nnct(ipd,fcls)

#with possible ties in the data
Y<-matrix(round(runif(3*n)*10),ncol=3)
ipd<-ipd.mat(Y)
cls<-sample(1:2,n,replace = TRUE)  #or try cls<-rep(1:2,c(10,10))
nnct(ipd,cls)
nnct(ipd,cls,ties = TRUE)



nnspat documentation built on Aug. 30, 2022, 9:06 a.m.