1

I am attempting to normalize a spatial point pattern using a population density raster in r. My problem is that the function I am trying to run is recognizing the "white space" around the raster I am using as "NA", giving me the error message: Values of the covariate ‘Y’ were NA or undefined at 32% (19493 out of 60241) of the quadrature points. Occurred while executing: ppm.ppp(X, ~offset(log(Y)), covarariates = list(Y)).

Is there a way to create a window or data frame that traces the outline of my raster set? It should be noted that I am a grad student with very limited experience using r.

    library(spatstat)
    library(maptools)
    library(raster)
    library(geostatsp)
    library(rgdal)

    X <- readShapePoints("~/Documents/Data/Cyber/US/CONTIG_US/CONUS02_FREQ")
    Y <- raster("~/Documents/Data/Cyber/US/US_POP1.tif")

    ##Set the extent

    e <- extent(-125,-67,25, 50)

    ## create a window for spatstat

    WIN <- as(e,"SpatialPoints")
    X.ppp <- as(X, "ppp") 

    ## convert raster to an im object for spatstat

    Y <- asImRaster(Y)
    Y <- eval.im((Y))

    Y <- as.im(Y)

    ## Unmark X for model:

    X<- unmark(X.ppp)


    Z <- ppm.ppp(X, ~offset(log(Y)), covarariates=list(Y))

The raster plot looks like this:

Im trying to get rid of the black rectangle...

3

The NA-values are present in your quadrature scheme due the way you are defining your observation window (owin). The owin for your events (X) was defined as the whole extent of the raster. Remember that any raster, by default is a structure or matrix of NxN rows and columns. In your example, the cells with population density had a value ranging from 0 to 41, but also there are cells with NA-values, (the empty spaces in where the oceans, Canada or Mexico would be located).

Spatast can calculate irregular windows, based on the minimal extent of the events (for more detail look at ?ripras). However, ripras would not extract the exact US border. The easiest fix for your problem, is to employ a polygonal defining the US border as the owin, you can use a shapefile and importe it similarly as you did with the point file.

US <- readShapePoly("USBORDER.shp")

USw <- as(US,"owin")

Xpoints <- readShapePoints("~/Documents/Data/Cyber/US/CONTIG_US/CONUS02_FREQ")

X <- as(Xpoints,"ppp")[USw]

Be aware that all your data must have the same Coordinate system, based on your extent, I am assuming your data is not projected, and you are using Cartesian coordinates, so the polygon must be in the same way.

In addition, even after using a polygonal to define the owin, you might get NA-values, this could be due the resolution of your raster and/or the scale of origin of the polygon. If this happen, you could:

1) run the model

Z <- ppm(**X**,~PD,covariates=list(PD=Y))

It will give you the same warning, "values of the covariate 'PD' were NA or undefined at n%"

2) extract the quadrature scheme from Z

**Qz** <- quad.ppm(Z,drop=TRUE) #this step will drop points within the quadrature scheme that had NA-values

3) run the model again, but this time, use the corrected quadrature scheme

Z1 <- ppm(**Qz**,~PD,covariates=list(PD=Y))

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.