# small example
# -------------
set.seed(181230)
xi <- spatstat.random::rpoispp(20)
eta <- spatstat.random::rpoispp(20)
dmat <- spatstat.geom::crossdist(xi,eta)
res <- ppdist(dmat, penalty=1, type="rtt", ret_matching=TRUE, p=1)
plotmatch(xi, eta, dmat, res, penalty=1, p=1)
res$dist
# for comparison: ospa-distance computation from spatstat:
res_ospa <- spatstat.geom::pppdist(xi,eta,"spa")
res_ospa$distance # exactly the same as above because nothing gets cut off
# same example, but with a much smaller penalty for adding/deleting points
# ---------------------------------------------------------------
res <- ppdist(dmat, penalty=0.1, type="rtt", ret_matching=TRUE, p=1)
plotmatch(xi, eta, dmat, res, penalty=0.1, p=1)
# dashed lines indicate points that are deleted and re-added at new position
# grey segments on dashed lines indicate the cost of deletion plus re-addition
res$dist
# for comparison: ospa-distance computation from spatstat
# (if things do get cut off, we have to ensure that the cutoff distances
# are the same, thus cutoff = 2^(1/p) * penalty):
res_ospa <- spatstat.geom::pppdist(xi,eta,"spa",cutoff=0.2)
res_ospa$distance # NOT the same as above
res_ospa$distance - abs(xi$n-eta$n) * 0.1 / max(xi$n,eta$n) # the same as above
# a larger example
# ---------------------------------------------------------------
set.seed(190203)
xi <- spatstat.random::rpoispp(2000)
eta <- spatstat.random::rpoispp(2000)
dmat <- spatstat.geom::crossdist(xi,eta)
res <- ppdist(dmat, penalty = 0.1, type = "rtt", ret_matching = TRUE, p = 1)
res$dist
# takes about 2-3 seconds
Run the code above in your browser using DataLab