I would like to use envelope simulation on a list of kppm objects.
I have a point dataset that is in a grid pattern. I would like to shift each point such that its position in the cell is random, then run a spatstat simulation to determine if the points are randomly distributed (obviously will be at sub cell size scale). Then randonly shift shift each point inside the cell, and re-run the simulation - repeating n times and generate an envelope of all the simulations. There are 2 ways to do this 1) using a list of point sets and 2) using simulate.
Method 1) - on a list called my_kppm_list
Lmat = envelope(fitM, Lest, nsim = 19, global=FALSE, simulate=my_kppm_list)
plot(Lmat)
How to create a list of cluster process models (kppm)?
Naive way:
my_list <- list(kppm_0, kppm_1)
This fails when trying to run simulation:
Lmat = envelope(fitM, Lest, nsim = 19, global=FALSE, simulate=my_list)
Error in envelopeEngine(X = X, fun = fun, simul = simrecipe, nsim = nsim, :
‘simulate’ should be an expression, or a list of point patterns of the same kind as X
I can convert a list to .ppm
fitM_0 <- kppm(create_pts(), ~1, "MatClust")
fitM_1 <- kppm(create_pts(), ~1, "MatClust")
my_list <- list(fitM_0, fitM_1)
ppm_list <- lapply(my_list, as.ppm)
But trying to convert to kppm fails with an error
kppm_list <- lapply(my_list, as.kppm)
Method 2) apply a function in simulation such that a random shift is applied to each point then simulation run, and envelope of all simulations is used (see page 399 of Baddelley et al. Spatial Point Patterns book (2016)):
e_rand <- function(){
j_x <- matrix(unlist(runif(dim(c_df)[1], min=-10, max=10)), ncol = 1, byrow = T)
j_y <- matrix(unlist(runif(dim(c_df)[1], min=-10, max=10)), ncol = 1, byrow = T)
x_j<- c_df[,1]+j_x
y_j<- c_df[,2]+j_y
c_j <- ppp(x = x_j, y = y_j, window = window)
return(c_j)
}
Lmat = envelope(fitM, Lest, nsim = 19, global=TRUE, simulate=e_rand)
However I found that the null model (red dashed line in output plot) had kinks in it when simulate is added - kinks that do not appear without simulate.