parameters icon indicating copy to clipboard operation
parameters copied to clipboard

A solution for bootstrap_model/parameters to make it "snow"

Open plants-22 opened this issue 3 years ago • 0 comments

Hi Daniel,

I love the parameters package, it's been very useful for my mixed model analysis. I'm using a PC with Windows OS and need to use parallel = "snow" however I found that the bootstrap_model.merMod function fails when calling bootMer with snow operations. I had a look at the source code and found if I add a cl argument to the bootMer function, snow works. Alternatively, adding ... to the bootMer arguments works too, I guess this would be a more general modification. Any chance you can verify the problem and update the package to fix this bug?

Below is my suggestion. Code is from the bootstrap_model function:

#' @rdname bootstrap_model
#' @export
bootstrap_model.merMod <- function(model,
                                   iterations = 1000,
                                   type = "parametric",
                                   parallel = c("no", "multicore", "snow"),
                                   n_cpus = 1,
                                   verbose = FALSE,
                                   #cl = NULL,
                                   ...) {
  insight::check_if_installed("lme4")
  
  type <- match.arg(type, choices = c("parametric", "semiparametric"))
  parallel <- match.arg(parallel)
  
  boot_function <- function(model) {
    params <- insight::get_parameters(model, verbose = FALSE)
    n_params <- insight::n_parameters(model)
    
    if (nrow(params) != n_params) {
      params <- stats::setNames(rep.int(NA, n_params), params$Parameter)
    } else {
      params <- stats::setNames(params$Estimate, params$Parameter) # Transform to named vector
    }
    return(params)
  }
  
  if (verbose) {
    results <- lme4::bootMer(
      model,
      boot_function,
      nsim = iterations,
      type = type,
      parallel = parallel,
      ncpus = n_cpus,
      #cl = cl,
      **...**
      )
  } else {
    results <- suppressMessages(lme4::bootMer(
      model,
      boot_function,
      nsim = iterations,
      verbose = FALSE,
      type = type,
      parallel = parallel,
      ncpus = n_cpus,
      #cl = cl,
      **...**
      ))
  }
  
  out <- as.data.frame(results$t)
  out <- out[stats::complete.cases(out), ]
  
  names(out) <- insight::find_parameters(model, effects = "fixed")$conditional
  class(out) <- unique(c("bootstrap_model", "see_bootstrap_model", class(out)))
  attr(out, "original_model") <- model
  out
}

Here's an example with bootstrap_parameters. I checked with the modified bootstrap_model function first and found bootstrap_parameters worked successfully when calling the modified bootstrap_model function:

library(glmmTMB)
library(parameters)
library(parallel)

m1 <- glmmTMB(count ~ mined + (1|site), zi=~mined,
              family=poisson, data=Salamanders)

cl <- makeCluster(6)
clusterEvalQ(cl, library("glmmTMB"))

system.time(d <- bootstrap_parameters(m1, iterations = 24, n_cpus = 6, parallel = "snow", cl = cl))

stopCluster(cl)

system.time(d <- bootstrap_parameters(m1, iterations = 24, n_cpus = 6, parallel = "snow", cl = cl)) user system elapsed 0.11 0.11 7.50

Cheers,

Neil

plants-22 avatar Jul 30 '22 05:07 plants-22