I want to use replicated spatial point patterns to perform hypothesis tests in spatstat
. spatstat
has wonderful documentation, and you can find details about replicated point pattern analysis here: https://cran.r-project.org/web/packages/spatstat/vignettes/replicated.pdf
Several mapped forest plots will represent a point process working at different locations. Each point pattern is marked, where each mark represents a tree species. Additionally, each point pattern is associated with a covariate raster image. First, I want to create a null model which assumes that each mark has a different relationship with the covariate. Then I want to use this null model to test whether certain species are associated with (or avoid) one another by simulating the random labeling hypothesis and plotting the resulting simulation envelopes. (The random labeling hypothesis states that the marks are randomly assigned to points in the point pattern.)
First, I'll show how I would normally do this analysis using a single point pattern. Then I'll explain the two problems I'm having using hyperframes to perform the same analysis.
Let's say you have a marked point pattern, where each mark is the tree species. I'll use the "lansing" data set available in spatstat:
library(spatstat)
data(lansing)
par(mar=rep(0.5,4))
plot(split(lansing),main="")
Now let's say you want to look at some spatial covariate (e.g., soil nutrients or moisture), so you create a raster image of a kernel smoothed density of the measurement:
sim1 <- rpoispp(function(x,y) {500 * exp(-3*x)}, win=owin(c(0,1),c(0,1)))
sim1 <- density(sim1)
First create null model:
single.mod <- ppm(lansing ~ marks*sim1)
Where the "ppm" function recognizes "marks" as the column with the species names, and "sim1" as the covariate.
Then perform a simulation-based hypothesis test, where you're interested in whether Black Oak and Maple are found in the same locations.
single.E<-envelope.ppm(single.mod,Lcross,i="blackoak",j="maple",nsim=39, nrank=1,global=TRUE,correction="best",simulate=expression(rlabel(lansing)))
par(mar=rep(4,4))
plot(E,legend=FALSE,ylab="L-function",xlab="Spatial scale (m)",main="Testing random label hypothesis \nfor single point pattern")
This works fine. Now if we go out and sample a couple more plots to make our analysis more robust, we can incorporate additional plots into a hyperframe, where each plot has its own point pattern and are considered "experimental" replicates. Each plot will get its own spatial covariate as well:
sim2 <- rpoispp(function(x,y) {500 * exp(-2*y)}, win=owin(c(0,1),c(0,1)))
sim2 <- density(sim2)
sim3 <- rpoispp(500, win=owin(c(0,1),c(0,1)))
sim3 <- density(sim3)
hyper <- hyperframe(pp=list(lansing,lansing,lansing),sims=list(sim1,sim2,sim3))
Sim2 and sim3 are spatial covariates for the additional two plots we've collected, and the "hyperframe" function combines the three point patterns with their associated spatial covariates into one hyperframe.
I'd like to build a model using "mppm" (used for creating models for multiple point patterns), where each point pattern is explained by their spatial covariates, "sims":
hyper.mod <- mppm(pp ~ sims, data = hyper)
The first problem arises when I try to allow each mark to carry a different relationship with the covariate:
int.mod <- mppm(pp ~ marks*sims, data=hyper)
The following error message spits out:
Error in checkvars(formula, data.sumry$col.names, extra = c("x", "y", : Variable "marks" in formula is not one of the names in data"
I get the same error using:
int.mod <- mppm(pp ~ pp$marks*sims, data=hyper)
The second problem is figuring out how to run the simulation-based hypothesis test on the hyperframe. Let's use the hyperframe model that worked (hyper.mod) to try this:
E.hyper <- envelope(hyper.mod,Lcross,i="blackoak",j="maple",nsim=39, nrank=1,global=TRUE,correction="best",simulate=expression(rlabel(pp)))
You get an error message:
Error in UseMethod("envelope") : no applicable method for 'envelope' applied to an object of class "c('mppm', 'list')"
Implying that "envelope" doesn't work on mppm objects (only ppp or ppm). I suspect there's a way to get around this limitation cleverly, but I haven't found it yet. Any suggestions or guidance would be very helpful!