[R-sig-Geo] Problem using trend covariates in predict.ppm (spatstat)
Rawlins, Barry G.
bgr at bgs.ac.uk
Thu Mar 26 10:34:47 CET 2015
Dear Rolf
Many thanks for this - your amended code worked fine.
I am using spatstat version 1.41-1 which I believe is the latest.
I suspect you are correct that my syntax is a bit cumbersome. I had been reading your spatstat text "Modelling Spatial Point Patterns in R" which was the most comprehensive description I could find of the package. Maybe this was written a while ago (?) and I think I was using some of the syntax from that which might account for my approach, but I may be wrong here.
I sometimes like to use the full reference (e.g. predict.ppm) to distinguish it from the other calls to predict which I often use in lm or functions in nlme.
Thanks again.
Best wishes, Barry
-----Original Message-----
From: Rolf Turner [mailto:r.turner at auckland.ac.nz]
Sent: 25 March 2015 22:15
To: Rawlins, Barry G.; r-sig-geo at r-project.org
Subject: Re: [R-sig-Geo] Problem using trend covariates in predict.ppm (spatstat)
On 26/03/15 05:32, Rawlins, Barry G. wrote:
> Dear list
>
> I have been successfully forming spatial point pattern models using
the function ppm and a series of covariates stored as im objects:
>
> Example here in which I have a spatial point pattern object
"CI_pipe_40_spp" and a covariate "cov_CI_len"
>
> mod2<-ppm(CI_pipe40_spp, trend=~cov_CI_len,
covariates=list(cov_CI_len)) # this works fine giving me a ppm model
> summary(mod2)
>
> Point process model Fitting method: maximum likelihood (Berman-Turner
> approximation) Model was fitted using glm() Algorithm converged
> Call: ppm.ppp(Q = CI_pipe40_spp, trend = ~cov_CI_len, covariates =
list(cov_CI_len))
> Edge correction: "border" [border correction distance r = 0 ]
>
> I then want to use the predict function I next write in the same
workspace:
> preds=predict.ppm(mod2,type="trend", window=mask40,ngrid=c(402,402),
> covariates=list(cov_CI_len))
>
> But I get the following error: Error in mpl.get.covariates(covariates,
> list(x = xpredict, y =
ypredict), :
> Each entry in the list 'covariates' should be an image, a function, a
window, a tessellation or a single number
>
> But if I check the class of "cov_CI_len": class(cov_CI_len) [1] "im"
>
>
> Which shows that this object is an image. Can someone suggest what
> is
wrong here? The
>
> The help says "If covariates is a list of images, then the names of
the entries should correspond to the names of covariates in the model
formula trend."
> I think I have the code correct - can anyone help with this?
I think that the problem is that you do not *name* the covariate in the
list that you supply to predict. The error message is less informative
than it might be, I guess.
I believe that
preds=predict.ppm(mod2,type="trend", window=mask40,ngrid=c(402,402),
covariates=list(cov_CI_len=cov_CI_len))
should work.
BTW, what version of spatstat are you using? The syntax of your calls
is unnecessarily cumbersome, and many aspects of it could be simplified
considerably, particularly with recent versions of spatstat:
mmm <- ppm(CI_pipe_40_spp ~ cov_CI_len)
prd <- predict(mmm,window=mask40,ngrid=402)
I.e. there is no longer any need to specify "covariates" (the newer
usage is "data" rather than "covariates", although the older usage still
works) if the covariate in question is in your global environment/workspace.
Also there is no need to to type "predict.ppm"; the *generic* predict()
automatically dispatches to the *method* predict.ppm().
Also there is no need to specify 'type="trend"' for a Poisson model.
Also ngrid=c(402,402) is equivalent to ngrid=402.
HTH
cheers,
Rolf Turner
--
Rolf Turner
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276
Home phone: +64-9-480-4619
This message (and any attachments) is for the recipient ...{{dropped:6}}
More information about the R-sig-Geo
mailing list