I am using different Goodness of fit tests for objects of class kppm. The goodness of fit tests in the attached code worked fine in version 1.59-0 of spatstat, but in the most recent version (1.61-0 and 1.61-0.019), there is an error related to rinterval
.
The code with seeds to replicate the error is:
library(spatstat)
#### Seed to recreate the same results ####
set.seed(1234)
#### Model from Thomas process ####
Data.Example <- rThomas(5, 0.05, 10) # kappa, scale, mu
#### Fitting a Thomas model ####
DE.fit.Thomaskppm <- kppm(Data.Example, ~ 1, "Thomas")
#*********************************************************
#### Goodness-of-fit test ####
#*********************************************************
####Using Dao-Genton test ####
#Thomas model
set.seed(100000)
dg.test(DE.fit.Thomaskppm, rinterval = c(0, 0.25))
#### Diggle-Cressie-Loosmore-Ford test ####
#Thomas model
set.seed(100000)
dclf.test(DE.fit.Thomaskppm, rinterval = c(0, 0.2))
#### Maximum Absolute Deviation Tests ####
#Thomas model
set.seed(100000)
mad.test(DE.fit.Thomaskppm, rinterval = c(0, 0.2))
The error is:
Error in envelopeTest(Yi, ..., nsim = nsimsub, alternative = alternative, :
Some function values were infinite, NA or NaN at distances r up to 0.25 units. Please specify a shorter ‘rinterval’
This error appeared in version 1.59-0, but it was fixed by setting the rinterval
from 0 to 0.25. In 1.61-0 version, I set the rinterval
even shorter, but this error continues to appear.
dclf.test
and mad.test
work fine if I use 10 as the seed.
Thanks in advance.
Thank you for the clear reproducible example. I can confirm that I also get the error. Just a clarifying question: Has the behavior changed from spatstat 1.59-0 to 1.61-0 with the same seed?
I’m very confident that I have an explanation of why the error occurs, but I’m not sure what the best way to solve it is. I will try to get back to you on that part. Now for the explanation:
Your model has parent intensity 3.78. The model is simulated in an expanded window and then restricted to the target window (unit square). The expansion factor is 4*scale
where your fitted model has scale=0.05
(rounded). Thus the window of simulation has area (1+8*scale)^2=1.9
and the expected number of parent points is 3.78*1.9=7.2
. The Poisson distributed number of parents may occasionally be zero which gives you an estimated K-function which is NA
everywhere, so changing rinterval
does not help. The NA
K-function may also happen if you only get one or two parents in the expanded region which have no offspring in the target simulation window. Collectively the probability of an empty point pattern in the target window is non-negligible.
I will think about how to best circumvent this problem without invalidating the statistical inference.
Code below illustrates the problem (starting with the original code from the question)
library(spatstat)
#### Seed to recreate the same results ####
set.seed(1234)
#### Model from Thomas process ####
Data.Example <- rThomas(5, 0.05, 10) # kappa, scale, mu
#### Fitting a Thomas model ####
DE.fit.Thomaskppm <- kppm(Data.Example, ~ 1, "Thomas")
Simulate the fitted model and extract the number of points:
s <- simulate(DE.fit.Thomaskppm, nsim = 1000, verbose = FALSE)
np <- sapply(s, npoints)
summary(np)
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 0.00 30.00 45.00 47.52 63.00 167.00
Find the proportion of patterns with 1 or less points yielding a pure NA
K-function estimate:
mean(np<=1)
#> [1] 0.016
A simple workaround is to simulate the patterns first as illustrated above and then delete patterns with less than two points and simulate new ones. Once you have a list of simulated patterns of the length you need you can provide this list as the simulate
argument to envelope
. This is not necessarily the best way to handle this problem in general, but in your case it will most likely introduce very little bias.