# nn: Determine nearest neighbors In smacpod: Statistical Methods for the Analysis of Case-Control Point Data

## Description

`nn` determines the nearest neighbors for a set of observations based on a distance matrix.

## Usage

 `1` ```nn(d, k, method = "c", self = FALSE) ```

## Arguments

 `d` A square distance matrix for the coordinates of interest. `k` The number of neighbors to return (if `method = "c"`) or the distance for which observations are considered neighbors (if ```method = "d"```). `method` The method of determining the neighbors. The default is `"c"`, specifying that the `k` nearest neighbors (the number of neighbors) for each observation should be returned. The alternative is `"d"`, meaning that neighbors are determined by their distance from an observation. In that case, two observations are neighbors if their separation distance is less or equal to `k`. `self` A logical indicating whether an observation is a neighbor with itself. The default is `FALSE`.

## Details

This function determine nearest neighbors in two ways: 1. number of neighbors or 2. distance.

If `method = "c"`, then `k` specifies the total number of neighbors to return for each observation.

If `method = "d"`, then `k` specifies the maximum distance for which an observation is considered a neighbor.

The function returns the neighbors for each observation.

## Value

Returns a list with the nearest neighbors of each observation. For each element of the list, the indices order neighbors from nearest to farthest.

Joshua French

## Examples

 ```1 2 3 4 5 6 7``` ```data(grave) # make distance matrix d = as.matrix(dist(cbind(grave\$x, grave\$y))) # 3 nearest neighbors nnc = nn(d, k = 3, method = "c") # nearest neighbors within k units of each observation nnd = nn(d, k = 200, method = "d") ```

smacpod documentation built on May 14, 2018, 5:07 p.m.