diff --git a/R/tmb-sim.R b/R/tmb-sim.R index 5ec8ccb95..e81907f07 100644 --- a/R/tmb-sim.R +++ b/R/tmb-sim.R @@ -412,6 +412,9 @@ simulate.sdmTMB <- function(object, nsim = 1L, seed = sample.int(1e6, 1L), } ret <- do.call(cbind, ret) + if (!is.null(object$extra_time)) { + ret <- ret[seq(1, nrow(object$data)),,drop=FALSE] # drop extra time rows + } attr(ret, "type") <- type ret } diff --git a/tests/testthat/test-tmb-simulation.R b/tests/testthat/test-tmb-simulation.R index 21bbbe3fc..fe8851f5b 100644 --- a/tests/testthat/test-tmb-simulation.R +++ b/tests/testthat/test-tmb-simulation.R @@ -165,3 +165,21 @@ test_that("TMB breakpt sims work", { expect_lt(max(sim_dat$observed), 0.53) expect_gt(min(sim_dat$observed), -0.05) }) + +test_that("simulate.sdmTMB returns the right length", { + skip_on_cran() + pcod$os <- rep(log(0.01), nrow(pcod)) # offset + m <- sdmTMB( + data = pcod, + formula = density ~ 0, + time_varying = ~ 1, + offset = pcod$os, + family = tweedie(link = "log"), + spatial = "off", + time = "year", + extra_time = c(2006, 2008, 2010, 2012, 2014, 2016), + spatiotemporal = "off" + ) + s <- simulate(m, nsim = 2) + expect_equal(nrow(s), nrow(pcod)) +})