Given two point patterns X
and Y
on a linear network,
finds the nearest neighbour in Y
of each point of X
using the shortest path in the network.
# S3 method for lpp
nncross(X, Y,
iX=NULL, iY=NULL,
what = c("dist", "which"),
…,
k = 1,
method="C")
Point patterns on a linear network (objects of class "lpp"
).
They must lie on the same linear network.
Optional identifiers, used to determine whether a point in
X
is identical to a point in Y
. See Details.
Character string specifying what information should be returned.
Either the nearest neighbour distance ("dist"
),
the identifier of the nearest neighbour ("which"
),
or both.
Ignored.
Integer, or integer vector. The algorithm will compute the distance to the
k
th nearest neighbour, for each value of k
.
Internal use only.
By default (if what=c("dist", "which")
and k=1
)
a data frame with two columns:
Nearest neighbour distance
Nearest neighbour index in Y
If what="dist", a vector of nearest neighbour distances.
If what="which", a vector of nearest neighbour indices.
If k is a vector of integers, the result is a matrix with one row for each point in X, giving the distances and/or indices of the kth nearest neighbours in Y.
Given two point patterns X
and Y
on the same linear
network, this function finds, for each point of X
,
the nearest point of Y
, measuring distance by the shortest path
in the network. The distance between these points
is also computed.
The return value is a data frame, with rows corresponding to
the points of X
. The first column gives the nearest neighbour
distances (i.e. the i
th entry is the distance
from the i
th point of X
to the nearest element of
Y
). The second column gives the indices of the nearest
neighbours (i.e.\ the i
th entry is the index of
the nearest element in Y
.)
If what="dist"
then only the vector of distances is returned.
If what="which"
then only the vector of indices is returned.
Note that this function is not symmetric in X
and Y
.
To find the nearest neighbour in X
of each point in Y
,
use nncross(Y,X)
.
The arguments iX
and iY
are used when
the two point patterns X
and Y
have some points in
common. In this situation nncross(X, Y)
would return some zero
distances. To avoid this, attach a unique integer identifier to
each point, such that two points are identical if their
identifying numbers are equal. Let iX
be the vector of
identifier values for the points in X
, and iY
the vector of identifiers for points in Y
. Then the code
will only compare two points if they have different values of the
identifier. See the Examples.
The k
th nearest neighbour may be undefined, for example
if there are fewer than k+1
points in the dataset, or if
the linear network is not connected.
In this case, the k
th nearest neighbour distance is infinite.
nndist.lpp
for nearest neighbour
distances in a single point pattern.
nnwhich.lpp
to identify which points are nearest
neighbours in a single point pattern.
# NOT RUN {
# two different point patterns
X <- runiflpp(3, simplenet)
Y <- runiflpp(5, simplenet)
nn <- nncross(X,Y)
nn
plot(simplenet, main="nncross")
plot(X, add=TRUE, cols="red")
plot(Y, add=TRUE, cols="blue", pch=16)
XX <- as.ppp(X)
YY <- as.ppp(Y)
i <- nn$which
arrows(XX$x, XX$y, YY[i]$x, YY[i]$y, length=0.15)
# nearest and second-nearest neighbours
nncross(X, Y, k=1:2)
# two patterns with some points in common
X <- Y[1:2]
iX <- 1:2
iY <- 1:5
nncross(X,Y, iX, iY)
# }
Run the code above in your browser using DataLab