| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/nn.R |
| 3 |
\name{nn2} |
| 4 |
\alias{nn2} |
| 5 |
\title{Nearest Neighbour Search} |
| 6 |
\usage{ |
| 7 |
nn2( |
| 8 |
data, |
| 9 |
query = data, |
| 10 |
k = min(10, nrow(data)), |
| 11 |
treetype = c("kd", "bd"), |
| 12 |
searchtype = c("standard", "priority", "radius"), |
| 13 |
radius = 0, |
| 14 |
eps = 0 |
| 15 |
) |
| 16 |
} |
| 17 |
\arguments{ |
| 18 |
\item{data}{An \bold{M} x \bold{d} data.frame or matrix, where each of the |
| 19 |
\bold{M} rows is a point or a (column) vector (where \bold{d=1}).} |
| 20 |
|
| 21 |
\item{query}{A set of \bold{N} x \bold{d} points that will be queried against |
| 22 |
\code{data}. \bold{d}, the number of columns, must be the same as |
| 23 |
\code{data}. If missing, defaults to \code{data}.} |
| 24 |
|
| 25 |
\item{k}{The maximum number of nearest neighbours to compute. The default |
| 26 |
value is set to the smaller of the number of columnns in data} |
| 27 |
|
| 28 |
\item{treetype}{Character vector specifying the standard \code{'kd'} tree or a |
| 29 |
\code{'bd'} (box-decomposition, AMNSW98) tree which may perform better for |
| 30 |
larger point sets} |
| 31 |
|
| 32 |
\item{searchtype}{See details} |
| 33 |
|
| 34 |
\item{radius}{Radius of search for searchtype='radius'} |
| 35 |
|
| 36 |
\item{eps}{Error bound: default of 0.0 implies exact nearest neighbour search} |
| 37 |
} |
| 38 |
\value{ |
| 39 |
A \code{list} of length 2 with elements: |
| 40 |
|
| 41 |
\item{nn.idx}{A \bold{N} x \bold{k} integer \code{matrix} returning the near |
| 42 |
neighbour indices.} |
| 43 |
|
| 44 |
\item{nn.dists}{A \bold{N} x \bold{k} \code{matrix} returning the near |
| 45 |
neighbour Euclidean distances.} |
| 46 |
} |
| 47 |
\description{ |
| 48 |
Uses a kd-tree to find the p number of near neighbours for each point in an |
| 49 |
input/output dataset. The advantage of the kd-tree is that it runs in O(M log |
| 50 |
M) time. |
| 51 |
} |
| 52 |
\details{ |
| 53 |
The \code{RANN} package utilizes the Approximate Near Neighbor (ANN) C++ |
| 54 |
library, which can give the exact near neighbours or (as the name suggests) |
| 55 |
approximate near neighbours to within a specified error bound. For more |
| 56 |
information on the ANN library please visit |
| 57 |
\url{https://www.cs.umd.edu/~mount/ANN/}. |
| 58 |
|
| 59 |
Search types: \code{priority} visits cells in increasing order of distance |
| 60 |
from the query point, and hence, should converge more rapidly on the true |
| 61 |
nearest neighbour, but standard is usually faster for exact searches. |
| 62 |
\code{radius} only searches for neighbours within a specified radius of the |
| 63 |
point. If there are no neighbours then nn.idx will contain 0 and nn.dists |
| 64 |
will contain 1.340781e+154 for that point. |
| 65 |
} |
| 66 |
\examples{ |
| 67 |
|
| 68 |
x1 <- runif(100, 0, 2*pi) |
| 69 |
x2 <- runif(100, 0,3) |
| 70 |
DATA <- data.frame(x1, x2) |
| 71 |
nearest <- nn2(DATA,DATA) |
| 72 |
} |
| 73 |
\references{ |
| 74 |
Bentley J. L. (1975), Multidimensional binary search trees used |
| 75 |
for associative search. Communication ACM, 18:309-517. |
| 76 |
|
| 77 |
Arya S. and Mount D. M. (1993), Approximate nearest neighbor searching, |
| 78 |
Proc. 4th Ann. ACM-SIAM Symposium on Discrete Algorithms (SODA'93), 271-280. |
| 79 |
|
| 80 |
Arya S., Mount D. M., Netanyahu N. S., Silverman R. and Wu A. Y (1998), An |
| 81 |
optimal algorithm for approximate nearest neighbor searching, Journal of the |
| 82 |
ACM, 45, 891-923. |
| 83 |
} |
| 84 |
\author{ |
| 85 |
Gregory Jefferis based on earlier code by Samuel E. Kemp (knnFinder |
| 86 |
package) |
| 87 |
} |
| 88 |
\keyword{nonparametric} |