I am trying to model a point process with an image covariate using the ppm() function in the spatstat package in R. I convert my raster to an im object for use with spatstat, and I run into a problem using the im as a covariate in the model. The pixel values are numeric, but these are actually just codes for different landscape zones so the crux of the problem is getting the model to read the pixel values as factor rather than numeric. I have tried the following two approaches (R code and data are presented below). The first consists of converting the raster values from numeric to factor prior to converting the raster object to the im object. Using the as.factor() function this seems to have the desired effect of converting the values to factor. However, when I run the ppm model with this covariate, the ppm() function does not include a parameter for each factor level in the model (compared to a reference level). Rather it treats the covariate as numeric with just the one parameter for the one covariate. The second approach was to runt the ppm model with factor(covariate) used to specify the covariate in the formula argument, rather than just the covariate itself. This actually works in fitting the model, giving me a parameter for each factor level compared to the reference. However, when I run predict.ppm() to get my predictions it fails because I used factor() in the formula argument. The qustion is, how can I run the ppm model such that it recognizes the values of the covariate image as factor and, thus, fitting a model with a parameter estimate for each factor level (minus the reference) and allowing prediction with predict.ppm.

The point process data is in csv format here: https://www.dropbox.com/s/tp1opzsmc14e2hb/EbolaData_AnalyticSet_8.8.14.csv?dl=0

The tiff file for the covariate is here: https://www.dropbox.com/s/0fyt0jflokrpp5z/anthrome2000_global_5min.tif?dl=0

And the R code is as follows:


# First set the geographic extent we'll be using
e <- extent(-20, 60, -40, 35)

# Then read in the point process data in .csv file:
outbreaks <- read.csv("EbolaData_AnalyticSet_8.8.14.csv")
coordinates(outbreaks) <- ~Longitude+Latitude
proj4string(outbreaks) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") 

# Then read in the anthropogenic biome tiff and crop it
anthro2000 <- raster("anthrome2000_global_5min.tif")
anthro2000.africa <- crop(anthro2000, e)

# Then we define oour point process and window for spatstat:
SP <- as(outbreaks, "SpatialPoints")
outbreaks.ppp <- as(SP, "ppp")

# Now let's create a window
SP.win <- as(e, "SpatialPolygons")
W <- as(SP.win, "owin")

# Before creating the im object, let's convert the pixel values in raster to factor:
f <- as.factor(anthro2000.africa)
rat <- levels(f)[[1]]
rat$anthro <- c("Urban", "Mixed Settle", "Rice Villages", "Irrigated villages", "Rainfed villages", "Pastoral vilalges",
    "Resid. irrig. cropland", "Resid. rainfed cropland", "Pop. cropland", "Remote cropland", 
    "Resid. rangeland", "Pop. rangeland", "Remote rangeland", "Resid. forests", "Pop. forests",
    "Remote forests", "Inhabited treeless and barren", "Wild forests", "Wild treeless and Barren")
rat$code <- c(11,12,21,22,23,24,31,32,33,34,41,42,43,51,52,53,54,61,62)
levels(f) <- rat

# Now let's convert that raster to an im object for use in spatstat:
anthro2000.africa.img <- asImRaster(f)

# Everything is now set up for the ppm models

# Aprroach 1
spatial.m.1 <- ppm(outbreaks.1.ppp, ~ Cov1, covariates = list(Cov1 = anthro2000.africa.img))
spatial.m.1 # Notice the model is fitted, however the pixel values of the covariate are not interepreted as factor

# Approach 2:
spatial.m.2 <- ppm(outbreaks.ppp, ~ factor(Cov1), covariates = list(Cov1 = anthro2000.africa.img)) # Noticce the use of factor() here to force the covariate as factor
spatial.m.2 # Here the model is fitted correctly with covariate as factor and thus each factor level has a parameter estimate in the model (relative to the ref)

# However, the approach does not allow me to run the predictions:
p <- predict.ppm(spatial.m.2, covariates = list(Cov1 = anthro2000.africa.img))
  • Since your independent data is truly multi-level categorical, this would not be a valid point process model as it does not follow underlying assumptions. You could, in theory, dummy code the levels in the covariate but it would not make your model any more valid. Feb 24, 2016 at 16:31

3 Answers 3


The problem here is a bug in asImRaster, a function from the package geostatsp designed to convert between image formats. After the command anthro2000.africa.img <- asImRaster(f), if you had printed the object anthro2000.africa.img, the printout would have revealed that the pixel values are numeric. The factor levels have been discarded by asImRaster.

You can work around this bug by reinstating the levels:

levels(anthro2000.africa.img) <- levels(f).

[Always print the resulting object to check it is what you expected.]

Then you can proceed to fit the model using ppm as you did before.

[Note: contrary to one of the comments above, this is a perfectly valid point process model. It is explained in detail in chapter 9 of the spatstat book.]


Suppose that Y is an "im" factor covariate with values 1, 2 and 3, representing three land use classes. To assign the factors to the image you must:

   # Step 1: write a vector with the classes

   # Step 2: use the function cut.im (but spatstat would also recognize only "cut")

   # if correct, the plot would display the image and use the assigned   
   # factors in the legend

This code works for converting classified shapefiles into factor-valued pixel images:

beachtypes      = st_read("Beachwindowtypes.shp")
beachtypes$Type = as.factor(beachtypes$Type)

# make a spatraster 
y = rast(ext(beachtypes), 10000, 10000)
beachtypes.spatraster = rasterize(as(beachtypes,"SpatVector"), y, field="Type")
plot(beachtypes.spatraster, main = "Beach types")

# turn into raster
beachtypes.raster = raster(beachtypes.spatraster)
is.factor(beachtypes.raster) # TRUE

# turn into pixel im
beachtypes.im = as.im.RasterLayer(beachtypes.raster) # discards the factor levels
print(beachtypes.im) # real valued

# make factors again
classes = c("dunes_ifo_dikes" , "dikes", "dunes")
bt.im = cut(beachtypes.im, breaks=3, labels=classes)

# ppm 
pm.A = ppm(westcoastA.pp ~ types, covariates = list(types = bt.im))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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