当我知道某些模拟失败时,从runjags获取尾波文件

当我知道某些模拟失败时,从runjags获取尾波文件,r,parallel-processing,mcmc,runjags,R,Parallel Processing,Mcmc,Runjags,我正在按模拟ID拆分数据集,并同时将runjags函数应用于每个子数据集。这使我能够利用并行处理并在集群上运行模拟。一项耗时超过1天的工作可以在大约2小时内完成 在我运行的1000个模拟中,大约有25个失败。我使用带有陷阱捕捉错误的for循环从成功运行的模拟中提取尾波文件。问题是,一旦模拟失败,错误会影响我正在使用的所有内核,并且我无法为成功运行的模拟提取尾码文件 有什么建议吗?我在下面包括代码和日志文件。多谢各位 library(parallel) library(coda) #1) usi

我正在按模拟ID拆分数据集,并同时将runjags函数应用于每个子数据集。这使我能够利用并行处理并在集群上运行模拟。一项耗时超过1天的工作可以在大约2小时内完成

在我运行的1000个模拟中,大约有25个失败。我使用带有陷阱捕捉错误的for循环从成功运行的模拟中提取尾波文件。问题是,一旦模拟失败,错误会影响我正在使用的所有内核,并且我无法为成功运行的模拟提取尾码文件

有什么建议吗?我在下面包括代码和日志文件。多谢各位

library(parallel)
library(coda)

#1) using mclapply to apply a function to all simulations simultaneously

output_models <- parallel::mclapply(subsetdata, function(x){
  library(runjags)
  set.seed(1)
  model_data = x
 
  
  runJagsOut <- run.jags(method = "simple",
                         model = "tempModel.txt",
                         monitor = c( "mu" ),
                         data = model_data,
                         #inits = initsList, # NOTE: Let JAGS initialize.
                         n.chains = 1, # NOTE: Not only 1 chain.
                         adapt = 500,
                         burnin = 3000,
                         sample = 2500,
                         thin = 1,
                         summarise = TRUE,
                         plots = FALSE)
  return(runJagsOut)
  
}, mc.cores = numcores)


#2) Build an empty list vector

mcmc <- list()

#3) Extracting coda files for each of the simulations. tryCatch function in place to 'ignore' simulations that fail

for (SimulID in 1:length(unique(df$SimulID))) {
  tryCatch({
  mcmc[[SimulID]] <- cbind(output_models[[SimulID]][["mcmc"]][[1]],SimulID)
  }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
}

#4) Main text file with the coda for each simulation

lapply(mcmc, function(x) write.table( data.frame(x), 'output.txt'  , append= T, sep=',', col.names = FALSE ))

建议:如果您将
output\u模型
替换为
future::plan(“multicore”,workers=numcores)
output\u模型,谢谢您。每当我运行RStudio时,lapply函数都会在我的计算机上工作,但由于某些原因,它在集群上不工作。我能想到的唯一原因是集群运行在Linux环境中。我试过了,代码在一个地方停留了大约30分钟。我想补充一点,Parlappy在集群上也不起作用。请注意,
plan(multicore)
在内部使用相同的
mclappy()
机器,。因此,如果一个有效,那么另一个也有效。我之所以提到
future\u lappy()
w/
plan(multicore)
是因为它很可能会给您提供更多的信息/线索,而不仅仅是“调度的核心39、24、…37在用户代码中遇到错误,所有作业的值都会受到影响”。您好,很抱歉回复太晚。我找到了这个页面,并能够通过在函数末尾添加mc.preschedule=FALSE使代码正常工作。
. Initializing model
. Adapting 500
-------------------------------------------------| 500
++++++++++++++++++++++++++++++++++++++++++++++++++ 100%
Adaptation successful
. Updating 3000
-------------------------------------------------| 3000
************************************************** 100%
. . Updating 2500
-------------------------------------------------| 2500
************************************************** 100%
. . . . Updating 0
. Deleting model
. 
Simulation complete.  Reading coda files...
Coda files loaded successfully
Calculating summary statistics...
Finished running the simulation
Warning message:
In parallel::mclapply(subsetdata, function(x) { :
  scheduled cores 39, 24, 35, 21, 22, 23, 29, 3, 8, 19, 47, 34, 6, 48, 10, 33, 38, 41, 31, 18, 5, 16, 37 encountered errors in user code, all values of the jobs will be affected
ERROR : subscript out of bounds 
ERROR : subscript out of bounds 
ERROR : subscript out of bounds