Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in check_time_class() #377

Open
javierlenzi opened this issue Oct 23, 2024 · 1 comment
Open

Error in check_time_class() #377

javierlenzi opened this issue Oct 23, 2024 · 1 comment

Comments

@javierlenzi
Copy link

Dear Sean and others,

I am running a series of sdmTMB binomial models with the same response and predictors, with and without space and time random fields. When using the function predict() to get the predictions over my SpatRaster stack of predictors for the study area (the Baltic Sea), I keep receiving this error message:

Error in check_time_class():
! Class of fitted time column (integer) does not match class of newdata time column (SpatRaster)

Reading the sdmTMB vignette, I realized that you need to predict using a data frame replicated for each of the years in the training set. Is this correct?

grid_yrs <- replicate_df(qcs_grid, "year", unique(pcod$year))

In my case, predictors are just one tiff layer stacked in a SpatRaster object (dimensions : 11023, 9825, 33 (nrow, ncol, nlyr)) with no time stamp associated to them, they are static. Is there any way that I could use sdmTMB to predict using my static raster stack with no need to convert it into a data frame and replicate it over the years of my observations?

This will simplify things a lot because although I have a column "year" in my observations I am not interested in predicting for each year at this time. Further, my 33 static predictors are in high spatial resolution (250 m, 11023 x 9825 cells each) for the entire Baltic Sea, and my computer is not able to handle the conversion to a data frame due to memory issues.

Thanks a lot for any help and guidance you can provide.

Javier.

@ericward-noaa
Copy link
Collaborator

Can you please include a small reproducible example?

In general, yes -- you need to have time in the prediction grid if time is included in the model. From the help files, you can fit a spatial model with no time argument (and spatiotemporal fields off as a result):

# Fit a Tweedie spatial random field GLMM with a smoother for depth:
mesh <- make_mesh(pcod_2011, c("X", "Y"), cutoff = 20)

fit <- sdmTMB(
  density ~ s(depth),
  data = pcod_2011, mesh = mesh,
  family = tweedie(link = "log")
)
fit

And then you can make a prediction to a grid without time,

# Predict on new data:
p <- predict(fit, newdata = qcs_grid)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants