@@ -341,6 +341,9 @@ sdmTMB_simulate <- function(formula,
341
341
# ' @param mcmc_samples An optional matrix of MCMC samples. See `extract_mcmc()`
342
342
# ' in the \href{https://github.com/pbs-assess/sdmTMBextra}{sdmTMBextra}
343
343
# ' 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.
344
347
# ' @param silent Logical. Silent?
345
348
# ' @param ... Extra arguments passed to [predict.sdmTMB()]. E.g., one may wish
346
349
# ' 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),
391
394
re_form = NULL ,
392
395
mle_mvn_samples = c(" single" , " multiple" ),
393
396
mcmc_samples = NULL ,
397
+ return_tmb_report = FALSE ,
394
398
silent = FALSE ,
395
399
... ) {
396
400
set.seed(seed )
@@ -457,29 +461,33 @@ simulate.sdmTMB <- function(object, nsim = 1L, seed = sample.int(1e6, 1L),
457
461
if (! is.null(mcmc_samples )) { # we have a matrix
458
462
for (i in seq_len(nsim )) {
459
463
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
461
466
}
462
467
} else {
463
468
for (i in seq_len(nsim )) {
464
469
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
466
472
}
467
473
}
468
474
if (! silent ) cli :: cli_progress_done()
469
475
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
+ }
479
487
}
480
- }
481
488
482
- ret <- do.call(cbind , ret )
483
- attr(ret , " type" ) <- type
489
+ ret <- do.call(cbind , ret )
490
+ attr(ret , " type" ) <- type
491
+ }
484
492
ret
485
493
}
0 commit comments