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
.
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)
I think this is a typo/thinko. The function you pass to bootMer
should reference the model passed to it as an argument, not the original model; substitute model
for m1
inside your function:
pred_fun <- function(model) {
predict(model, newdata = pred_data1, type = "link", re.form = NA,
allow.new.levels = TRUE)
}
You also need to make sure that you've exported pred_data1
:
clusterExport(par_cluster, varlist = c("Salamanders", "pred_data1"))
If you leave the function as written above and add m1
to your list of cluster-exported objects, bootMer
would run but you'd get the same answer in every bootstrap replicate (because it would ignore the resampled data/refitted model and use the original m1
every time ...)
Although it was previously undocumented (it now is in the development version on Github), you can access the error messages via
attr(pred_bootMer, "boot.fail.msgs")
For example, if I forgot to export pred_data1
, I'd get this:
object 'pred_data1' not found
2
(the 2 is the number of times that error occurred). This will help a lot in debugging future problems!