Skip to content

Commit e1b0171

Browse files
committed
Add return_tmb_report to simulate()
1 parent 2813439 commit e1b0171

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: sdmTMB
33
Title: Spatial and Spatiotemporal SPDE-Based GLMMs with 'TMB'
4-
Version: 0.6.0.9012
4+
Version: 0.6.0.9013
55
Authors@R: c(
66
person(c("Sean", "C."), "Anderson", , "sean@seananderson.ca",
77
role = c("aut", "cre"),

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# sdmTMB (development version)
22

3+
* Add `return_tmb_report` to `simulate.sdmTMB()`.
4+
35
* Add `newdata` argument to `simulate.sdmTMB()`. This enables simulating on
46
a new data frame similar to how one would predict on new data.
57

R/tmb-sim.R

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ sdmTMB_simulate <- function(formula,
341341
#' @param mcmc_samples An optional matrix of MCMC samples. See `extract_mcmc()`
342342
#' in the \href{https://github.com/pbs-assess/sdmTMBextra}{sdmTMBextra}
343343
#' package.
344+
#' @param return_tmb_report Return the \pkg{TMB} report from `simulate()`? This
345+
#' lets you parse out whatever elements you want from the simulation.
346+
#' Not usually needed.
344347
#' @param silent Logical. Silent?
345348
#' @param ... Extra arguments passed to [predict.sdmTMB()]. E.g., one may wish
346349
#' to pass an `offset` argument if `newdata` are supplied in a model with an
@@ -391,6 +394,7 @@ simulate.sdmTMB <- function(object, nsim = 1L, seed = sample.int(1e6, 1L),
391394
re_form = NULL,
392395
mle_mvn_samples = c("single", "multiple"),
393396
mcmc_samples = NULL,
397+
return_tmb_report = FALSE,
394398
silent = FALSE,
395399
...) {
396400
set.seed(seed)
@@ -457,29 +461,33 @@ simulate.sdmTMB <- function(object, nsim = 1L, seed = sample.int(1e6, 1L),
457461
if (!is.null(mcmc_samples)) { # we have a matrix
458462
for (i in seq_len(nsim)) {
459463
if (!silent) cli::cli_progress_update()
460-
ret[[i]] <- newobj$simulate(par = new_par[, i, drop = TRUE], complete = FALSE)$y_i
464+
ret[[i]] <- newobj$simulate(par = new_par[, i, drop = TRUE], complete = FALSE)
465+
if (!return_tmb_report) ret[[i]] <- ret[[i]]$y_i
461466
}
462467
} else {
463468
for (i in seq_len(nsim)) {
464469
if (!silent) cli::cli_progress_update()
465-
ret[[i]] <- newobj$simulate(par = new_par[, i, drop = TRUE], complete = FALSE)$y_i
470+
ret[[i]] <- newobj$simulate(par = new_par[, i, drop = TRUE], complete = FALSE)
471+
if (!return_tmb_report) ret[[i]] <- ret[[i]]$y_i
466472
}
467473
}
468474
if (!silent) cli::cli_progress_done()
469475

470-
if (isTRUE(object$family$delta)) {
471-
if (is.na(model[[1]])) {
472-
ret <- lapply(ret, function(.x) .x[,1] * .x[,2])
473-
} else if (model[[1]] == 1) {
474-
ret <- lapply(ret, function(.x) .x[,1])
475-
} else if (model[[1]] == 2) {
476-
ret <- lapply(ret, function(.x) .x[,2])
477-
} else {
478-
cli_abort("`model` argument isn't valid; should be NA, 1, or 2.")
476+
if (!return_tmb_report) {
477+
if (isTRUE(object$family$delta)) {
478+
if (is.na(model[[1]])) {
479+
ret <- lapply(ret, function(.x) .x[,1] * .x[,2])
480+
} else if (model[[1]] == 1) {
481+
ret <- lapply(ret, function(.x) .x[,1])
482+
} else if (model[[1]] == 2) {
483+
ret <- lapply(ret, function(.x) .x[,2])
484+
} else {
485+
cli_abort("`model` argument isn't valid; should be NA, 1, or 2.")
486+
}
479487
}
480-
}
481488

482-
ret <- do.call(cbind, ret)
483-
attr(ret, "type") <- type
489+
ret <- do.call(cbind, ret)
490+
attr(ret, "type") <- type
491+
}
484492
ret
485493
}

man/simulate.sdmTMB.Rd

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)