[R-sig-Geo] how to limit an image or a funxy to an irregular polygonal window, instead of the whole enclosing rectangle?

Christopher W. Ryan cryan at binghamton.edu
Fri Dec 15 20:37:31 CET 2017


Using R 3.3.3 and spatstat 1.50-0 on Windows 7.  MWE below.

I have a SpatialPolygonsDataFrame of census tract poverty levels in 3
contiguous counties in the US, called sremsPoverty. I want to use this
as a predictor in a ppm model. The window for the point pattern is the
three counties--so an irregular polygon--called sremsWindow.

I understand ppm predictors need to be an image, a tesselation, a funxy,
a window, or a single number. So I proceed as follows:

### Poverty
p <- slot(sremsPoverty, "polygons")
v <- lapply(p, function(z) { SpatialPolygons(list(z)) })
sat <- tess(tiles = lapply(v, as.owin) )
pov.f <- as.function.tess(sat, values = sremsPoverty at data$propPoverty)

Thus pov.f is a spatial function (funxy) that I can use in a call to ppm():

m1 <- ppm(unique.cases.ppp ~ pov.f)

pov.f looks as expected when I plot it. But examing the structure of
as.im(pov.f) it appears it is a 128 x 128 pixel array, with the value of
the function at all pixels outside of the irregular polygonal window,
but inside the bounding rectangle, set to NA. I think this is the cause
of the NA values I am seeing among the residuals from m1, and those NA
residuals are causing me some trouble with model diagnostics such as
rhohat().

How do I constrain the funxy (or the image I can derive from it) to the
irregular polygonal window, so as to eliminate the NA values outside the
window but inside the bounding rectangle? Or can I constrain the
modeling activity of ppm() to the window?

Thanks.

--Chris Ryan
Broome County Health Department
Binghamton University
SUNY Upstate Medical University
Binghamt, NY, US

MWE:

x <- c(0, 2.6, 3, 1, 0)
y <- c(1,2,3,2,1)
test.window <- owin(poly=list(x=x,y=y))
plot(test.window)  ## looks as expected
## make spatial function
test.f <- function(x,y){x+y}
test.funxy <- funxy(test.f, W = test.window)  ## I *thought* this would
restrict the funxy to the window, but I don't think it does
plot(test.funxy)  ## looks as expected
## but the image from test.funxy is square, with NA values outside the
polygonal window, which is not square
test.im <- as.im(test.funxy)
str(test.im)

## my incorrect attempts to restrict the image to the window yields a
numeric vector
str(test.im[test.window])
## or an error message
window(test.im) <- test.window



More information about the R-sig-Geo mailing list