Prediction using spatial layers (rasters) with


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?


Can you elaborate a bit more?


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.


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, = F)


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()?


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 …


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.


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.