pbs-assess / sdmTMB

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

Prediction fails when both offset and nsim are provided and model includes extra_time (v0.4.1) #273

Closed sebpardo closed 10 months ago

sebpardo commented 10 months ago

Just came across this issue that's very much related to #270 and which is occurring in both sdmTMB 0.4.0 and 0.4.1. The problem with matching the length of the offset and the number of rows of newdata (in this case obtained internally) resurfaces when a value of nsim is being provided:

library(sdmTMB)

mesh <- make_mesh(pcod, c("X", "Y"), cutoff = 10)

pcod$os <- rep(log(0.01), nrow(pcod)) # offset

m <- sdmTMB(
  data = pcod,
  formula = density ~ poly(log(depth), 2),
  mesh = mesh,
  offset = pcod$os,
  family = tweedie(link = "log"),
  spatial = "on",
  time = "year",
  extra_time = c(2006, 2008, 2010, 2012, 2014, 2016), 
  spatiotemporal = "AR1"
)

p1 <- predict(m, offset = pcod$os) # This works
p2 <- predict(m, newdata = pcod, offset = pcod$os) # This used to fail but now works 
p3 <- predict(m, newdata = pcod) # This works
p4 <- predict(m, offset = pcod$os, nsim = 30) # This fails
# Error in `predict()`:
# ! Prediction offset vector does not equal number of rows in prediction dataset.
# Run `rlang::last_trace()` to see where the error occurred.
seananderson commented 10 months ago

Thanks. Should be fixed now.