在R中使用bootMer()结合snow并行处理glmmTMB对象时出错
使用bootMer()并行处理glmmTMB对象时bootstrap全部失败的问题
在R中尝试用bootMer()的snow参数对glmmTMB模型执行并行bootstrap时,所有模拟运行均失败。仅当设置parallel = "snow"且ncpus大于1时出现此问题,单核心运行无异常。参考相关解决方案后仍未解决,具体情况如下:
初始可复现代码
require(tidyverse) require(glue) require(lme4) require(parallel) # 用于并行bootstrap步骤 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列 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)
错误提示
运行后收到以下警告;实际设置1000次模拟时全部失败,pred_bootMer$t返回全NA:
Warning message: In bootMer(m1, nsim = 1, FUN = pred_fun, parallel = "snow", use.u = FALSE, : some bootstrap runs failed (1/1)
已尝试的解决操作
- 调整
clusterEvalQ的包加载写法,包括:clusterEvalQ(par_cluster, library("glmmTMB"))clusterEvalQ(par_cluster, library(glmmTMB))clusterEvalQ(par_cluster, {library(lme4); library(glmmTMB)})
2023年7月16日更新:修正代码后问题依旧
修正了预测函数的硬编码问题(将函数内的m1改为参数传入),并将原始模型对象加入集群导出列表,但仍提示bootstrap运行失败:
更新后的代码
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)
疑问
- 如何解决该并行bootstrap失败的问题?
- 是否需要在glmmTMB的GitHub仓库提交相关issue?
- 说明:使用的是
bootMer()而非bootstrap_parameters
内容的提问来源于stack exchange,提问作者Karthik Thrikkadeeri
相关产品推荐
相关产品推荐

