pbs-assess / sdmTMB

:earth_americas: An R package for spatial and spatiotemporal GLMMs with TMB
https://pbs-assess.github.io/sdmTMB/
186 stars 26 forks source link

`predict()` for a model with spatially varying coefficients requires `newdata` #279

Closed ecophilina closed 10 months ago

ecophilina commented 10 months ago

Not obvious why this is the case. Would be nice if it would use the model data like all other cases.

seananderson commented 10 months ago

This part: https://github.com/pbs-assess/sdmTMB/blob/48b391434e9b214c97a3c24cf4f6f7548cac0272/R/predict.R#L838-L840 which is parsing the results from the TMB report, needs to be finished. It needs to match something like this, which is for newdata: https://github.com/pbs-assess/sdmTMB/blob/48b391434e9b214c97a3c24cf4f6f7548cac0272/R/predict.R#L717-L719 https://github.com/pbs-assess/sdmTMB/blob/48b391434e9b214c97a3c24cf4f6f7548cac0272/R/predict.R#L691-L694

There may be some other reason why I disabled this, but I can't remember right now. An easy hack would be internally setting newdata = object$data in the case of spatially varying coefficients, but that is slightly slower to return.

klwilson23 commented 10 months ago

I'm also getting this error as well when calling the dharma_residuals() function in sdmTMBextra

s_spp_re <- simulate(fit_spp_re, nsim = 500)
r_spp_re <- sdmTMBextra::dharma_residuals(s_spp_re, fit_spp_re, plot = FALSE)
Error in `predict()`:
! Prediction with `newdata = NULL` is not supported with spatially varying coefficients yet.
Please provide your data to `newdata`.
Run `rlang::last_trace()` to see where the error occurred.
seananderson commented 10 months ago

OK, fixed now. All I had to do was uncomment that code. I wish I could remember why I disabled it at some point, but everything looks fine to me in tests.