Skip to content

Commit

Permalink
Add progress bar in simulate.sdmTMB() #346
Browse files Browse the repository at this point in the history
Also silent = FALSE by default
  • Loading branch information
seananderson committed Sep 23, 2024
1 parent 11506d9 commit 796eb08
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: sdmTMB
Title: Spatial and Spatiotemporal SPDE-Based GLMMs with 'TMB'
Version: 0.6.0.9008
Version: 0.6.0.9009
Authors@R: c(
person(c("Sean", "C."), "Anderson", , "[email protected]",
role = c("aut", "cre"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# sdmTMB (development version)

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

* Add AUC and TSS examples to cross validation vignette. #268

* Add `model` (linear predictor number) argument to coef() method. Also,
Expand Down
23 changes: 13 additions & 10 deletions R/tmb-sim.R
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,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 @@ -421,18 +421,21 @@ 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
}
}
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 796eb08

Please sign in to comment.