Prediction using spatial layers (rasters) with brms.fit


#1

predict.brmsfit expects a data.frame. Would it be possible to use a brmsfit object to predict using a raster stack?
If so, can you share a guide?


#2

Can you elaborate a bit more?


#3

Thanks for the quick reply Paul (as usual)!

In my workflow I extract values from a raster stack and then use those values as the independent variables in a mixed effects model.
When attempting to use the brmsfit object to predict values using the same raster stack I get an error stating that the input values need to be a dataframe.
If I run a simple lm, I can use it to predict a raster stack.


#4

Here’s some rough code to demonstrate my workflow:

datXY <- SpatialPoints(datXY, proj4string=CRS('+proj=longlat +datum=WGS84'))
xVar <- rnorm(length(datXY))
filesraster <- list.files('rasters', full.names = T)
layers <- stack(filesraster)
dat <- data.frame(extract(layers, datXY))
dat <- cbind(dat, xVar)
mod <- brm(xVar ~ layer1 + layer2 + (1 | layer3), data = dat, prior = c(set_prior("normal(0.1, 1)", class = "b")), family = gaussian(), chains = 3, iter = 8000, warmup = 1000)
p <- predict(mod, layers, allow.new.levels = F)

#5

For the brm call, you seemed to have transformed the stack into a data.frame. Wouldn’t it be possible to do the same for the newdata in predict()?


#6

I did try converting to a DF, but I reach the max memory allocation. I can’t export directly as CSV and then import it with a clean workspace either.
I’m already allocating 16 gb of ram to R …


#7

I see. In any case, your transformed DF should contain the variable layer1, layer2 and layer3 or otherwise brms won’t be able to make sense of it.

Anyway, a conversion to DF is necessary because brms has no idea how to deal with rasters. It is not designed to know. The newdata should have the same structure as the original data.


#8

Ok. Good to know.
The model is run on values extracted from the rasters at specific points.

I’ll work to figure it out and then will post if I get a workaround.