nn: Nearest neighbors and distances

Description Usage Arguments Details Value Examples

Description

Obtain nearest neighbors and distances from a matrix or disto handle. k nearest or fixed radius neighbors are supported

Usage

1
nn(x, k, r, method = "euclidean", ...)

Arguments

x

Object of class 'disto' or a numeric matrix

k

Number of nearest neighbors

r

Radius for nearest neighbors

method

(string or function) distance metric when x is a matrix. Passed to 'proxy::dist'. Ignored when x is not a matrix.

...

Additional arguments for dapply when x is 'disto' object. Else additional arguments are sent to pbmclapply

Details

Exactly one among k or r has to be provided

Value

Object of class nn. A list with these elements:

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
## Not run: 
# create a matrix
set.seed(100)
mat <- cbind(rnorm(3e3), rpois(3e3, 1))

# compute a distance matrix and get a disto handle
do <- stats::dist(mat)
dio <- disto(objectname = "do")

# nearest neighbors: k nearest and fixed radius
nn(dio, k = 1)
nn(mat, k = 1) # distance method defaults to 'euclidean'
str(nn(mat, k = 1)) # observe the structure of the output

nn(dio, r = 0.1)
nn(mat, r = 0.1)

# nearest neighbors parallelized: k nearest and fixed radius
# fast computation, higher memory usage
nn(dio, k = 1, nproc = 2)
nn(mat, k = 1, mc.cores = 2)

nn(dio, r = 0.1, nproc = 2)
nn(mat, r = 0.1, mc.cores = 2)

# different distance method
do <- stats::dist(mat, method = "manhattan")

nn(dio, k = 1, nproc = 2)
nn(mat, k = 1, method = "manhattan", mc.cores = 2)

nn(dio, r = 0.1, nproc = 2)
nn(mat, r = 0.1, method = "manhattan", mc.cores = 2)

## End(Not run)

talegari/disto documentation built on May 10, 2019, 3:19 a.m.