1

I wish to calculate the nearest-neighbour distances (NNDs) of a point pattern by year with spatstat but get an error, although I cannot see any difference with the example provided in the documentation. What's the right way to do it?

library(spatstat)

#Build point pattern
pp <- structure(list(window = structure(list(type = "rectangle", xrange = c(952727.038715708, 
969663.326063713), yrange = c(1928725.27732809, 1943334.46685032
), units = structure(list(singular = "unit", plural = "units", 
    multiplier = 1), class = "unitname")), class = "owin"), n = 6L, 
    x = c(959238.044802669, 968582.344942023, 960218.207335433, 
    969663.326063713, 964665.091671559, 952727.038715708), y = c(1941763.77812852, 
    1934201.29833662, 1943334.46685032, 1939411.6354699, 1928725.27732809, 
    1931868.51667007), markformat = "vector", marks = c("1994", 
    "1994", "2005", "2005", "2005", "2005")), class = "ppp")

#Calculate NNDs
nndist(pp, by=marks(pp))

#Error in split.ppp(X %mark% idX, f = by, un = FALSE) : 
#  f must be a factor, a logical vector, a tessellation, a window, an image, or the name of a column of marks
syre
  • 902
  • 1
  • 7
  • 19

1 Answers1

1

The error message gives you a hint:

> nndist(pp, by=marks(pp))
Error in split.ppp(X %mark% idX, f = by, un = FALSE) :
  f must be a factor, a logical vector, a tessellation, a window, an image, or the name of a column of marks

It indicates that marks should be a factor, logical vector, or a tessellation.

Upon inspection, it turns out, that pp$marks is a character:

> str(pp$marks)
 chr [1:6] "1994" "1994" "2005" "2005" "2005" "2005"

Transforming it into a factor fixes the issue:

pp$marks <- as.factor(pp$marks)
nndist(pp, by=marks(pp))
          1994      2005
[1,] 12021.108  1851.427
[2,] 12021.108  5321.291
[3,]  1851.427 10227.359
[4,]  5321.291 10227.359
[5,]  6732.880 11797.483
[6,] 11845.227 12344.920
Otto Kässi
  • 2,943
  • 1
  • 10
  • 27
  • 1
    Thanks! I did notice that my marks form a vector but assumed that my example fell under the case of "name of a column of marks". Is it not a bit weird that the base `spatstat` function `nndist` does not systematically accept the output of the base function `marks`? – syre Nov 05 '21 at 09:23
  • 1
    @syre I think you are misunderstanding. `marks()` is a spatstat function. The problem is that if you want the marks of the point pattern to represent two different groups they should be encoded as `factors`. This is the way R represents grouping variables, so you should follow that convention. Does this make sense? The only thing @otto-kässi did differently than you, was to overwrite the marks as a factor representing groups in stead of just arbitrary strings. Otherwise your code was completely correct. – Ege Rubak Nov 05 '21 at 09:40
  • nndist assumes that the `marks` element in `ppp` is a factor. Providing a non-factor element as a `by` argument results in an error. If you think `marks()` should return factors by default, you might want to file a bug report to the spatstat developer. – Otto Kässi Nov 05 '21 at 09:42
  • I agree that the error message is not super informative! – Otto Kässi Nov 05 '21 at 09:43