Learn R Programming

spatstat.geom (version 3.3-2)

nndist.pp3: Nearest neighbour distances in three dimensions

Description

Computes the distance from each point to its nearest neighbour in a three-dimensional point pattern. Alternatively computes the distance to the second nearest neighbour, or third nearest, etc.

Usage

# S3 method for pp3
nndist(X, ..., k=1, by=NULL)

Value

Numeric vector or matrix containing the nearest neighbour distances for each point.

If k = 1 (the default), the return value is a numeric vector v such that v[i] is the nearest neighbour distance for the ith data point.

If k is a single integer, then the return value is a numeric vector v such that v[i] is the

kth nearest neighbour distance for the

ith data point.

If k is a vector, then the return value is a matrix m such that m[i,j] is the

k[j]th nearest neighbour distance for the

ith data point.

Arguments

X

Three-dimensional point pattern (object of class "pp3").

...

Ignored.

k

Integer, or integer vector. The algorithm will compute the distance to the kth nearest neighbour.

by

Optional. A factor, which separates X into groups. The algorithm will compute the distance to the nearest point in each group.

Warnings

An infinite or NA value is returned if the distance is not defined (e.g. if there is only one point in the point pattern).

Author

Adrian Baddeley Adrian.Baddeley@curtin.edu.au based on code for two dimensions by Pavel Grabarnik pavel.grabar@issp.serpukhov.su.

Details

This function computes the Euclidean distance from each point in a three-dimensional point pattern to its nearest neighbour (the nearest other point of the pattern). If k is specified, it computes the distance to the kth nearest neighbour.

The function nndist is generic; this function nndist.pp3 is the method for the class "pp3".

The argument k may be a single integer, or an integer vector. If it is a vector, then the \(k\)th nearest neighbour distances are computed for each value of \(k\) specified in the vector.

If there is only one point (if x has length 1), then a nearest neighbour distance of Inf is returned. If there are no points (if x has length zero) a numeric vector of length zero is returned.

If the argument by is given, it should be a factor, of length equal to the number of points in X. This factor effectively partitions X into subsets, each subset associated with one of the levels of X. The algorithm will then compute, for each point of X, the distance to the nearest neighbour in each subset.

To identify which point is the nearest neighbour of a given point, use nnwhich.

To use the nearest neighbour distances for statistical inference, it is often advisable to use the edge-corrected empirical distribution, computed by G3est.

To find the nearest neighbour distances from one point pattern to another point pattern, use nncross.

See Also

nndist, pairdist, G3est, nnwhich

Examples

Run this code
   X <- pp3(runif(40), runif(40), runif(40), box3(c(0,1)))

   # nearest neighbours
   d <- nndist(X)

   # second nearest neighbours
   d2 <- nndist(X, k=2)

   # first, second and third nearest
   d1to3 <- nndist(X, k=1:3)

   # distance to nearest point in each group
   marks(X) <- factor(rep(letters[1:4], 10))
   dby <- nndist(X, by=marks(X))

Run the code above in your browser using DataLab