<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Nearest Neighbor Contingency Table (NNCT) with (only) base...</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="https://rpkg.net/assets/examples.css" />
</head><body>

<table width="100%" summary="page for nnct.sub"><tr><td>nnct.sub</td><td style="text-align: right;">R Documentation</td></tr></table>

<h2>Nearest Neighbor Contingency Table (NNCT) with (only) 
base points restricted to a subsample</h2>

<h3>Description</h3>

<p>Returns the <i>k \times k</i> NNCT with (only) base points are restricted 
to be in the subset of indices <code>ss</code> using
the IPD matrix or data set <code>x</code> where <i>k</i> is 
the number of classes in the data set. 
That is, the base points
are the points with indices in <code>ss</code> 
but for the NNs the function checks all the points in the data set 
(including the points in <code>ss</code>). 
Row and columns of the NNCT are labeled with 
the corresponding class labels.
</p>
<p>The argument <code>ties</code> is a logical argument 
(default=<code>FALSE</code>) to take ties into account or not. 
If <code>TRUE</code> a NN contributes <i>1/m</i> to the NN count 
if it is one of the <i>m</i> tied NNs of a subject.
</p>
<p>The argument <code>is.ipd</code> is a logical argument (default=<code>TRUE</code>) 
to determine the structure of the argument <code>x</code>.
If <code>TRUE</code>, <code>x</code> is taken to be 
the inter-point distance (IPD) matrix, 
and if <code>FALSE</code>, <code>x</code> is taken to be the data set
with rows representing the data points.
</p>


<h3>Usage</h3>

<pre>
nnct.sub(ss, x, lab, ties = FALSE, is.ipd = TRUE, ...)
</pre>


<h3>Arguments</h3>

<table summary="R argblock">
<tr valign="top"><td><code>ss</code></td>
<td>
<p>indices of subjects (i.e., row indices in the data set) 
chosen to be the base points</p>
</td></tr>
<tr valign="top"><td><code>x</code></td>
<td>
<p>The IPD matrix (if <code>is.ipd=TRUE</code>)
or a data set of points in matrix 
or data frame form where points
correspond to the rows (if <code>is.ipd = FALSE</code>).</p>
</td></tr>
<tr valign="top"><td><code>lab</code></td>
<td>
<p>The <code>vector</code> of class labels (numerical or categorical)</p>
</td></tr>
<tr valign="top"><td><code>ties</code></td>
<td>
<p>A logical argument (default=<code>FALSE</code>) 
to take ties into account or not. 
If <code>TRUE</code> a NN 
contributes <i>1/m</i> to the NN count 
if it is one of the <i>m</i> tied NNs of a subject.</p>
</td></tr>
<tr valign="top"><td><code>is.ipd</code></td>
<td>
<p>A logical parameter (default=<code>TRUE</code>). 
If <code>TRUE</code>, 
<code>x</code> is taken as the inter-point distance matrix, 
otherwise, <code>x</code> is taken as the data set with rows 
representing the data points.</p>
</td></tr>
<tr valign="top"><td><code>...</code></td>
<td>
<p>are for further arguments, 
such as <code>method</code> and <code>p</code>, 
passed to the <code>dist</code> function.</p>
</td></tr>
</table>


<h3>Value</h3>

<p>Returns the <i>k \times k</i> NNCT 
where <i>k</i> is the number of classes in the data set with (only) base points
restricted to a subsample <code>ss</code>.
</p>


<h3>Author(s)</h3>

<p>Elvan Ceyhan
</p>


<h3>See Also</h3>

<p><code>nnct</code> and <code>nnct.boot.dis</code>
</p>


<h3>Examples</h3>

<pre>
n&lt;-20  #or try sample(1:20,1)
Y&lt;-matrix(runif(3*n),ncol=3)
ipd&lt;-ipd.mat(Y)
cls&lt;-sample(1:2,n,replace = TRUE)  #or try cls&lt;-rep(1:2,c(10,10))
nnct(ipd,cls)

#subsampling indices
ss&lt;-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&lt;-floor(n/2); nb&lt;-n-na
fcls&lt;-rep(c("a","b"),c(na,nb))
nnct.sub(ss,ipd,fcls)

#cls as an unsorted factor
fcls&lt;-sample(c("a","b"),n,replace = TRUE)
nnct(ipd,fcls)
nnct.sub(ss,ipd,fcls)

fcls&lt;-sort(fcls)
nnct.sub(ss,ipd,fcls)

#############
n&lt;-40
Y&lt;-matrix(runif(3*n),ncol=3)
ipd&lt;-ipd.mat(Y)
cls&lt;-sample(1:4,n,replace = TRUE)  #or try cls&lt;-rep(1:2,c(10,10))
ss&lt;-sample(1:40,30)
nnct.sub(ss,ipd,cls)

#cls as a factor
fcls&lt;-rep(letters[1:4],rep(10,4))
nnct.sub(ss,ipd,cls)

#1D data points
n&lt;-20  #or try sample(1:20,1)
X&lt;-as.matrix(runif(n))# need to be entered as a matrix with one column
#(i.e., a column vector), hence X&lt;-runif(n) would not work
ipd&lt;-ipd.mat(X)
cls&lt;-sample(1:2,n,replace = TRUE)  #or try cls&lt;-rep(1:2,c(10,10))
nnct(ipd,cls)

#subsampling indices
ss&lt;-sample(1:n,floor(n/2))
nnct.sub(ss,ipd,cls)

#with possible ties in the data
Y&lt;-matrix(round(runif(120)*10),ncol=3)
ipd&lt;-ipd.mat(Y)
cls&lt;-sample(1:4,n,replace = TRUE)  #or try cls&lt;-rep(1:2,c(10,10))
ss&lt;-sample(1:40,30)
nnct.sub(ss,ipd,cls)
nnct.sub(ss,ipd,cls,ties = TRUE)

</pre>


</body></html>
