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

EvalDoubleFunObject error when using nsim in non-spatial model with smooths #233

Closed maxlindmark closed 1 year ago

maxlindmark commented 1 year ago

Hi, when I try to predict with nsim in a non-spatial model with a smooth term I get the following error:

_Error in EvalDoubleFunObject(Fun, theta, do_simulate = dosimulate) : Wrong parameter length.

Here are some examples:

library(sdmTMB)

mesh <- make_mesh(pcod_2011, c("X", "Y"), cutoff = 20)
nd <- data.frame(depth = c(50, 100))

# spatial model with smooth, non spatial predictions
fit1 <- sdmTMB(
    density ~ s(depth),
    #spatial = "off",
    spatiotemporal = "off",
    data = pcod_2011, mesh = mesh,
    family = tweedie(link = "log")
)

predict(fit1, newdata = nd, nsim = 5, re_form = NA) # works!

# non-spatial model with smooth 
fit2 <- sdmTMB(
    density ~ s(depth),
    spatial = "off",
    spatiotemporal = "off",
    data = pcod_2011, mesh = mesh,
    family = tweedie(link = "log")
)

predict(fit2, newdata = nd, nsim = 5) # throws the error: Error in EvalDoubleFunObject(Fun, theta, do_simulate = do_simulate) :  Wrong parameter length.

# linear effect
fit3 <- sdmTMB(
    density ~ depth,
    spatial = "off",
    spatiotemporal = "off",
    data = pcod_2011, mesh = mesh,
    family = tweedie(link = "log")
)

predict(fit3, newdata = nd, nsim = 5) # works!

It only seems to be related to smooths, because when I run e.g., a random year model I can use nsim.

I am running TMB_1.9.3, glmmTMB_1.1.7 and sdmTMB_0.3.0 if that helps! (had some mismatches in these packages earlier)

Grateful for any help clarifying what's going on here! Max

seananderson commented 1 year ago

Thanks for pointing this out and the reproducible example. I just fixed this. Our 'check if the model has any random effects' internal function wasn't quite right, so it wasn't taking draws from the smoother random effect parameters.