nnct.sub: Nearest Neighbor Contingency Table (NNCT) with (only) base...

View source: R/NNCTFunctions.R

nnct.subR Documentation

Nearest Neighbor Contingency Table (NNCT) with (only) base points restricted to a subsample

Description

Returns the k \times k NNCT with (only) base points are restricted to be in the subset of indices ss using the IPD matrix or data set x where k is the number of classes in the data set. That is, the base points are the points with indices in ss but for the NNs the function checks all the points in the data set (including the points in ss). Row 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.sub(ss, x, lab, ties = FALSE, is.ipd = TRUE, ...)

Arguments

ss

indices of subjects (i.e., row indices in the data set) chosen to be the base points

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

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 with (only) base points restricted to a subsample ss.

Author(s)

Elvan Ceyhan

See Also

nnct and nnct.boot.dis

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)

#subsampling indices
ss<-sample(1:n,floor(n/2))
nnct.sub(ss,ipd,cls)
nnct.sub(ss,Y,cls,is.ipd = FALSE)
nnct.sub(ss,ipd,cls,ties = TRUE)

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

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

fcls<-sort(fcls)
nnct.sub(ss,ipd,fcls)

#############
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))
ss<-sample(1:40,30)
nnct.sub(ss,ipd,cls)

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

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

#subsampling indices
ss<-sample(1:n,floor(n/2))
nnct.sub(ss,ipd,cls)

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


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