# [R] spatstat regression troubles

Rolf Turner rolf.turner at xtra.co.nz
Sun Apr 17 01:17:09 CEST 2011

```On 17/04/11 02:17, Gregory Ryslik wrote:
> Hi Mr. Turner,
>
> You are correct that I am confused a bit by the RCode. Basically, I have 3772 observations of data and only about 500
500?  You said 944 previously.  Doesn't really matter, but.
> of them correspond to where "people" exist.  For the other observations, I just have the covariate values so I thought that this was appropriate.  Thus, where "people exist" is my spatial point pattern and everywhere else I just have covariate values.
>
> Thank you for your help and suggestions on how to fit the data. I was able to get it to work using the data frame method but I seem to be having difficulty getting the image thing to work.
>
> Basically, at the moment, I have two matrices for Z1 and Z2 which is in the form of (z, x, y) where z is the value, x is the x-coordinate and y-is the y coordinate. Thus the matrix dimension is 3772x3. I've tried converting this to an image but they do a index swap so I'm not quite sure what the correct  way to do it would be? Hopefully, I would get the fit using the image way and see that the fits are consistent.
>
> Thank you again for your help!

To make use of a covariate you ***really*** need to have the values of
the covariate
available at ***all*** points of the observation window.  In your
situation I think that
the best that you can do is to interpolate between the actual observations.

You could use, I think, the interp() function from the "akima" package.
Here's a toy
demo:

> require(akima)
> W <- owin(c(73,135),c(18,54))
> M <- as.mask(W,dimyx=c(250,500)) # Window is roughly twice as wide as
> it is high.
> set.seed(42)
> X <- runifpoint(3772,win=W)
> Z <- exp(2*(sin(2*X\$x/pi) + sin(2*X\$y/pi))) # A made-up covariate.
> XYZ <- interp(X\$x,X\$y,Z,xo=M\$xcol,yo=M\$yrow,linear=FALSE,extrap=TRUE)
> IZ <- im(t(XYZ\$z),xcol=XYZ\$x,yrow=XYZ\$y) # Note the transpose of the z
> matrix!
> E <- as.im(function(x,y){exp(2*(sin(2*x/pi) + sin(2*y/pi)))},W=W)
> plot(listof(exact=E,interp=IZ),nrows=2,main="")

The interpolated image is a bit rough compared with the truth (which we
know in this
artificial case) but no worse than what one might reasonably expect.

HTH

cheers,

Rolf Turner

```