Reputation: 327
I'm using the sdmTMB
package in R to fit a spatiotemporal model, and I'm trying to make predictions over a raster stack of environmental variables. However, because this is a spatiotemporal model, I need to include a time variable in my predictions. I've tried to convert the raster stack to a dataframe, but I’m unsure how to incorporate time across the raster predictions. Apologies for my example code as the points and rasters don't spatially overlap.
library(sdmTMB)
library(terra)
# Data and mesh setup
data("pcod")
mesh <- make_mesh(pcod, xy_cols = c("X", "Y"), cutoff = 10)
# Model fitting
m <- sdmTMB(
density ~ 1,
time_varying = ~ 0 + depth_scaled + depth_scaled2,
data = pcod, mesh = mesh,
time = "year",
family = binomial(link = "logit"),
spatial = "on",
spatiotemporal = "ar1"
)
# Example raster stack
elev <- rast(system.file("ex/elev.tif", package = "terra"))
slope <- terrain(elev, "slope")
aspect <- terrain(elev, "aspect")
# Create a raster stack
raster_stack <- c(elev, slope, aspect)
# Converting raster stack to dataframe for predictions
vars_stack_df <- terra::as.data.frame(raster_stack, xy = TRUE) |>
rename(X = x, Y = y)
# Attempt to predict on raster data
tmb_ras_p <- predict(mod_tmb, newdata = vars_stack_df)
Upvotes: 0
Views: 26