Skip to content

Commit

Permalink
Add progress bar go simulate()
Browse files Browse the repository at this point in the history
  • Loading branch information
seananderson committed Jun 26, 2024
1 parent 0cf31fb commit 0c6fcdc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# sdmTMB (development version)

* Add `project()` experimental function.

* Add progress bar to `simulate.sdmTMB()`.

* Add `get_eao()` to calculate effective area occupied.

# sdmTMB 0.6.0
Expand Down
24 changes: 14 additions & 10 deletions R/tmb-sim.R
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ sdmTMB_simulate <- function(formula,
simulate.sdmTMB <- function(object, nsim = 1L, seed = sample.int(1e6, 1L),
type = c("mle-eb", "mle-mvn"),
model = c(NA, 1, 2),
re_form = NULL, mcmc_samples = NULL, silent = TRUE, ...) {
re_form = NULL, mcmc_samples = NULL, silent = FALSE, ...) {
set.seed(seed)
type <- tolower(type)
type <- match.arg(type)
Expand Down Expand Up @@ -401,18 +401,22 @@ simulate.sdmTMB <- function(object, nsim = 1L, seed = sample.int(1e6, 1L),
new_par <- mcmc_samples
}

# do the sim
# do the simulation
if (!silent) cli::cli_progress_bar("Simulating", total = nsim)
ret <- list()
if (!is.null(mcmc_samples)) { # we have a matrix
ret <- lapply(seq_len(nsim), function(i) {
if (!silent) cat("-")
newobj$simulate(par = new_par[, i, drop = TRUE], complete = FALSE)$y_i
})
for (i in seq_len(nsim)) {
if (!silent) cli::cli_progress_update()
ret[[i]] <-
newobj$simulate(par = new_par[, i, drop = TRUE], complete = FALSE)$y_i
}
} else {
ret <- lapply(seq_len(nsim), function(i) {
if (!silent) cat("-")
newobj$simulate(par = new_par, complete = FALSE)$y_i
})
for (i in seq_len(nsim)) {
if (!silent) cli::cli_progress_update()
ret[[i]] <- newobj$simulate(par = new_par, complete = FALSE)$y_i
}
}
if (!silent) cli::cli_progress_done()

if (isTRUE(object$family$delta)) {
if (is.na(model[[1]])) {
Expand Down
2 changes: 1 addition & 1 deletion man/simulate.sdmTMB.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0c6fcdc

Please sign in to comment.