I am encountering an error while attempting to use parallel processing with the "snow" argument in the bootMer()
function on a glmmTMB
object in R. I followed the suggestions provided in a previous answer, but the issue remains unresolved. Here's a reproducible example:
require(tidyverse)
require(glue)
require(lme4)
require(parallel) # to parallelise bootstrap step
require(glmmTMB)
m1 <- glmmTMB(count ~ DOY + mined + (1|site),
family = nbinom2, data = Salamanders)
summary(m1)
pred_data1 <- data.frame(mined = c("yes", "no")) %>%
group_by(mined) %>%
reframe(DOY = unique(Salamanders$DOY)) %>%
# NA column required for random effect
mutate(site = NA)
pred_fun <- function(model) {
predict(m1, newdata = pred_data1, type = "link", re.form = NA,
allow.new.levels = TRUE)
}
par_cores <- max(1, floor(detectCores()/2))
par_cluster <- makeCluster(rep("localhost", par_cores), outfile = "log.txt")
clusterEvalQ(par_cluster, library("glmmTMB"))
clusterExport(par_cluster, varlist = c("Salamanders"))
print(glue("Using {par_cores} cores."))
pred_bootMer <- bootMer(m1, nsim = 1, FUN = pred_fun,
parallel = "snow",
use.u = FALSE, type = "parametric",
ncpus = par_cores, cl = par_cluster)
stopCluster(par_cluster)
I have tried using clusterEvalQ(par_cluster, library("glmmTMB"))
, clusterEvalQ(par_cluster, library(glmmTMB))
, and clusterEvalQ(par_cluster, {library(lme4); library(glmmTMB)})
, but none of these solutions have resolved the issue. I still encounter the following error:
Warning message:
In bootMer(m1, nsim = 1, FUN = pred_fun, parallel = "snow", use.u = FALSE, :
some bootstrap runs failed (1/1)
Note: in my actual runs, I use 1000 sims, and the message says all 1000 failed. And pred_bootMer$t
just gives NAs. It's worth noting that the problem occurs specifically when using parallel = "snow"
and setting n_cpus
to a value greater than 1, as OP pointed out in the linked question.
Any insights into resolving this problem would be greatly appreciated. Should I consider reopening the issue on GitHub for glmmTMB
? Additionally, I want to clarify that I am using bootMer()
rather than bootstrap_parameters
.
Edit (16/07/2023)
I fixed the issue with the function being provided to bootMer
, and also added my original model to the list of objects being exported, but I am still getting the bootstrap runs failed
message.
pred_fun <- function(some_model) {
predict(some_model, newdata = pred_data1, type = "link", re.form = NA,
allow.new.levels = TRUE)
}
par_cores <- max(1, floor(detectCores()/2))
par_cluster <- makeCluster(rep("localhost", par_cores), outfile = "log.txt")
clusterEvalQ(par_cluster, library("glmmTMB"))
clusterExport(par_cluster, varlist = c("Salamanders", "m1"))
print(glue("Using {par_cores} cores."))
pred_bootMer <- bootMer(m1, nsim = 2, FUN = pred_fun,
parallel = "snow",
use.u = FALSE, type = "parametric",
ncpus = 2, cl = par_cluster)
stopCluster(par_cluster)