# K nearest neighbor (KNN) search

### Description

This is a wrapper for several k nearest neighbors (KNNs)
algorithms in R. Currently wrapped functions are from the
`FNN`

, `RANN`

, and `yaImpute`

package.

It searches for KNN in a *N \times d* data matrix
`data`

where *N* are the number of samples, and
*d* is the dimension of space.

Either knn search in itself `query=NULL`

or to query
new data points wrt to training dataset.

### Usage

1 | ```
search_knn(data, k = 1, query = NULL, method = c("FNN", "RANN", "yaImpute"), ...)
``` |

### Arguments

`data` |
an |

`k` |
number of nearest neighbors (excluding point
itself). Default: |

`query` |
(optional) an |

`method` |
what method should be used: |

`...` |
other parameters passed to the knn functions in each package. |

### See Also

Packages FNN, RANN, and yaImpute for
other options (`...`

).

### Examples

1 2 3 4 5 6 7 8 9 10 11 | ```
set.seed(1984)
XX <- matrix(rnorm(40), ncol = 2)
YY <- matrix(runif(length(XX) * 2), ncol = ncol(XX))
knns_of_XX_in_XX <- search_knn(XX, 1)
knns_of_YY_in_XX <- search_knn(XX, 1, query = YY)
plot(rbind(XX, YY), type = "n", xlab = "", ylab = "")
points(XX, pch = 19, cex = 2, xlab = "", ylab = "")
arrows(XX[, 1], XX[, 2], XX[knns_of_XX_in_XX, 1], XX[knns_of_XX_in_XX, 2], lwd = 2)
points(YY, pch = 15, col = 2)
arrows(YY[, 1], YY[, 2], XX[knns_of_YY_in_XX, 1], XX[knns_of_YY_in_XX, 2], col = 2)
legend("left", c("X", "Y"), lty = 1, pch = c(19, 15), cex = c(2, 1), col = c(1, 2))
``` |