From 796eb08f9dbf9fb980ab19c76a2282011055d43c Mon Sep 17 00:00:00 2001 From: Sean Anderson Date: Mon, 23 Sep 2024 14:48:27 -0700 Subject: [PATCH] Add progress bar in simulate.sdmTMB() #346 Also silent = FALSE by default --- DESCRIPTION | 2 +- NEWS.md | 2 ++ R/tmb-sim.R | 23 +++++++++++++---------- man/simulate.sdmTMB.Rd | 2 +- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 30269120..e287cdf3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", , "sean@seananderson.ca", role = c("aut", "cre"), diff --git a/NEWS.md b/NEWS.md index bca9cc51..5815ca76 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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, diff --git a/R/tmb-sim.R b/R/tmb-sim.R index 724e884d..c54ff2a9 100644 --- a/R/tmb-sim.R +++ b/R/tmb-sim.R @@ -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) @@ -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]])) { diff --git a/man/simulate.sdmTMB.Rd b/man/simulate.sdmTMB.Rd index e3cda1e8..2d8b25ca 100644 --- a/man/simulate.sdmTMB.Rd +++ b/man/simulate.sdmTMB.Rd @@ -12,7 +12,7 @@ model = c(NA, 1, 2), re_form = NULL, mcmc_samples = NULL, - silent = TRUE, + silent = FALSE, ... ) }