Skip to content

Commit

Permalink
Ensure simulate doesn't include extra_time
Browse files Browse the repository at this point in the history
  • Loading branch information
seananderson committed Mar 26, 2024
1 parent f897635 commit 4d5a4b6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions R/tmb-sim.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
18 changes: 18 additions & 0 deletions tests/testthat/test-tmb-simulation.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})

0 comments on commit 4d5a4b6

Please sign in to comment.