spatstat/spatstat.linnet

Bug in bw.relrisklpp (maybe) with duplicated points

Closed this issue · 3 comments

Dear spatstat group, I'm sorry to bother you again. I'm writing this issue to report a problematic behaviour in bw.relrisklpp that occur with duplicated points. Reproducible example:

# packages
library(spatstat)
#> Carico il pacchetto richiesto: spatstat.data
#> Carico il pacchetto richiesto: spatstat.geom
#> spatstat.geom 2.0-0
#> Carico il pacchetto richiesto: spatstat.core
#> Carico il pacchetto richiesto: nlme
#> Carico il pacchetto richiesto: rpart
#> spatstat.core 2.0-0
#> Carico il pacchetto richiesto: spatstat.linnet
#> spatstat.linnet 2.0-0
#> 
#> spatstat 2.0-1       (nickname: 'Caution: contains small parts') 
#> For an introduction to spatstat, type 'beginner'

# simulate data
set.seed(1)
my_ppp1 <- ppp(
  x = c(0.2, 0.2, runif(5)), 
  y = c(0.2, 0.2, runif(5))
)
#> Warning: data contain duplicated points
my_lpp1 <- lpp(
  X = my_ppp1, 
  L = simplenet
)

# Estimate intensity at "points" following Rakshit et al. (2019)
density(my_lpp1, sigma = 0.2, distance = "euclidean", at = "points")
#> [1] 1.8888483 1.2261747 1.6113216 1.4250548 0.8453113 4.0378911
#> attr(,"sigma")
#> [1] 0.2

# It returns 6 estimates, i.e. one for each unique pair of coordinates. The
# problem is that I think that implies the following code fails:
my_ppp2 <- ppp(
  x = runif(5), 
  y = runif(5)
)
my_lpp2 <- lpp(
  X = my_ppp2, 
  L = simplenet
)

my_lpp <- superimpose(A = my_lpp1, B = my_lpp2)

# Fails
bw.relrisklpp(my_lpp, distance = "euclidean")
#> Error in `[<-.data.frame`(`*tmp*`, , j, value = structure(c(13.9870593985448, : replacement has 6 rows, data has 7

Created on 2021-03-18 by the reprex package (v1.0.0)

I run the previous code in Rstudio with the debugger, and I think that the problem occurs here:

https://github.com/baddstats/spatstat.linnet/blob/2bc676e44d5db6ccc378a3183387ad58eb7457f3/R/relrisk.lpp.R#L62-L79

since the lapply() function returns a list with fewer observations than points (since there are duplicated coordinates in one of the spatial inputs). Unfortunately, I don't understand the internals of spatstat, so I have no idea how to fix that. Moreover, I'm also not extremely familiar with estimating relative risk on a road network, so I'm not sure if I should change something in case of duplicated points.

EDIT

I simply fixed the error on the real dataset by jittering the points by a small margin, but maybe you still want to add a warning message.

EDITv2

Unfortunately, that's not true since, even if the points are not identical after jittering, they are projected to the same point of the network (one of the edges near the boundary of the owin).

Thank you for providing a detailed example.
This bug has been fixed in the development version, spatstat.linnet 2.1-0.

I have also added a new function rjitterlpp to perform jittering on a network.

Thank you very much Prof. Baddeley, the approached defined in the development version worked out perfectly.

I have also added a new function rjitterlpp to perform jittering on a network.

Thanks!