Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

nnfun

Nearest Neighbour Index Map as a Function


Description

Compute the nearest neighbour index map of an object, and return it as a function.

Usage

nnfun(X, ...)

  ## S3 method for class 'ppp'
nnfun(X, ..., k=1, value=c("index", "mark"))

  ## S3 method for class 'psp'
nnfun(X, ..., value=c("index", "mark"))

Arguments

X

Any suitable dataset representing a two-dimensional collection of objects, such as a point pattern (object of class "ppp") or a line segment pattern (object of class "psp").

k

A single integer. The kth nearest neighbour will be found.

...

Extra arguments are ignored.

value

String (partially matched) specifying whether to return the index of the neighbour (value="index", the default) or the mark value of the neighbour (value="mark").

Details

For a collection X of two dimensional objects (such as a point pattern or a line segment pattern), the “nearest neighbour index function” of X is the mathematical function f such that, for any two-dimensional spatial location (x,y), the function value f(x,y) is the index i identifying the closest member of X. That is, if i = f(x,y) then X[i] is the closest member of the collection X to the location (x,y).

The command f <- nnfun(X) returns a function in the R language, with arguments x,y, that represents the nearest neighbour index function of X. Evaluating the function f in the form v <- f(x,y), where x and y are any numeric vectors of equal length containing coordinates of spatial locations, yields the indices of the nearest neighbours to these locations.

If the argument k is specified then the k-th nearest neighbour will be found.

The result of f <- nnfun(X) also belongs to the class "funxy" and to the special class "nnfun". It can be printed and plotted immediately as shown in the Examples.

A nnfun object can be converted to a pixel image using as.im.

Value

A function with arguments x,y. The function also belongs to the class "nnfun" which has a method for print. It also belongs to the class "funxy" which has methods for plot, contour and persp.

Author(s)

Adrian Baddeley Adrian.Baddeley@curtin.edu.au, Rolf Turner r.turner@auckland.ac.nz and Ege Rubak rubak@math.aau.dk.

See Also

Examples

f <- nnfun(cells)
   f
   plot(f)
   f(0.2, 0.3)

   g <- nnfun(cells, k=2)
   g(0.2, 0.3)

   plot(nnfun(amacrine, value="m"))

   L <- psp(runif(10), runif(10), runif(10), runif(10), window=owin())
   h <- nnfun(L)
   h(0.2, 0.3)

spatstat.geom

Geometrical Functionality of the 'spatstat' Family

v2.1-0
GPL (>= 2)
Authors
Adrian Baddeley [aut, cre], Rolf Turner [aut], Ege Rubak [aut], Tilman Davies [ctb], Ute Hahn [ctb], Abdollah Jalilian [ctb], Sebastian Meyer [ctb], Suman Rakshit [ctb], Dominic Schuhmacher [ctb], Rasmus Waagepetersen [ctb]
Initial release
2021-04-15

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.