I am fitting multiple point process model fitted using gam in spatstat (version 3.0-7). To be clear I am using a call of the general form:
mppm(formula, data=hyperframe, eps=0.5, rbord=0.5, use.gam=TRUE)
Thus my model is Poisson process.
What I would like to do when the model is fitted is to plot:
- the effect of a given predictor variable in an
mgcv::plot.gamfashion - maps of the spatial trend.
However I get these error messages When trying to use functions
effectfun(): Error: First argument 'model' should be a fitted model of class ‘ppm’, ‘kppm’, ‘lppm’, ‘dppm’, ‘rppm’ or ‘profilepl’plot.mppm(): Error: This calculation is not supported for GAM fits
I am considering the following workaround options:
Subfit approach in spatstat
- re-fit the model to each pattern in my hyperframe individually (because
subfitsdoes not work for gam): I apply the same model formula used in themppmmodel and run it in appmfor each pattern. But in doing so, I might modify thekparameters independently for each pattern because the k-value used in mppm might give an error with some variables. - use
effectfunandploton each individually fitted ppm models. Here I am afraid that I could possibly end up with splines shapes different for each pattern, thus complication the interpretation
hack mppm and use mgcv
- run the
mppmwithuse.gam=Tmodel I want. - extract the data.frame from the mppm object using
my_model$Fit$moadf - fit the model with
mgcv::gamusing the call that is used internally bymppm:
gam(fmla, family = quasi(link = log, variance = mu), weights = .mpl.W * caseweight, data = moadf, subset = (.mpl.SUBSET == "TRUE"), control = ctrl)
- at that point, I could use the
mgcvfunctions to visualize the plots I need (at least the effect of a given predictor variable.
My questions
Is any of my described approaches good? One of them to be preferred? Or, are there other (better) options to achieve my goal?
Short answer: extract the fitted
gamobject directly asThen do
plot(m, ...)which invokesmgcv::plot.gam.Long answer:
Currently,
gamfits (produced whenuse.gam=TRUE) are not fully supported inmppm.Currently we use the
subfitsmechanism to perform many tasks formppmobjects, including plotting and predicting the model. This is our way to avoid (for the moment) having to re-write all of the code that supports theppmclass, all over again to support themppmclass.The function
subfitsis a hack which involves making "fake" objects of classppm(they are fake because they were not actually obtained by fitting a model to a single point pattern). This fakery has limitations and it does not work at all when the model is fitted bygam.Ultimately the code supporting the
mppmclass will be rewritten as native code and this problem will be resolved. Until then, I suggest you use the approach described in the short answer.