使用Brm在R中使用BRM进行负二项式回归会导致错误

时间:2020-05-24 14:43:37

标签: r sockets parallel-processing non-linear-regression r-4.0.0

我正在使用brm包中的brms函数来计算负二项式回归。由于这需要花费一些时间,因此我想使用documentation中建议的多个内核。

bfit_s <- brm(
  dep_var ~ ind_var +
    var1 +
    var2 +
    (1 | some_level1) + (1 | some_level2),
  data = my_df,
  family = negbinomial(link = "log", link_shape = "log"),
  cores = 4,
  control = list(adapt_delta = 0.999)
)

但是,我遇到一个错误,说所有四个工作者的连接都失败了:

Compiling the C++ model

Start sampling
starting worker pid=11603 on localhost:11447 at 14:13:56.193
starting worker pid=11601 on localhost:11447 at 14:13:56.193
starting worker pid=11602 on localhost:11447 at 14:13:56.198
starting worker pid=11604 on localhost:11447 at 14:13:56.201
Error in unserialize(node$con) : error reading from connection
Calls: <Anonymous> -> slaveLoop -> makeSOCKmaster
Error in unserialize(node$con) : error reading from connection
Calls: <Anonymous> -> slaveLoop -> makeSOCKmaster
Error in unserialize(node$con) : error reading from connection
Calls: <Anonymous> -> slaveLoop -> makeSOCKmaster
Execution halted
Execution halted
Execution halted
Error in unserialize(node$con) : error reading from connection
Calls: <Anonymous> -> slaveLoop -> makeSOCKmaster
Execution halted

回溯显示Error in makePSOCKcluster(names = spec, ...) : Cluster setup failed. 4 of 4 workers failed to connect.

我试图理解问题,在this之类的地方读过一些问题,但无法弄清楚为什么我无法连接。我正在使用macOS Mojave,问题不在于我尝试使用的内核过多。关于如何使它在多个内核上运行的任何建议?


编辑: 正如sjp在他的回答中指出的那样,RStudio存在问题。我以为我可以在我的问题中共享代码来解决问题,因此所有绊脚石的人都可以解决此问题,而无需进一步单击(阅读)。

问题是来自R-4.0.0的parallel软件包。 -但是由this斯坦论坛的用户提供了一种解决方法。如果您可以使用setup_strategy="sequential"初始化集群,如下所示:

cl <- parallel::makeCluster(2, setup_strategy = "sequential") 

您可以在~/.Rprofile中添加一个简短的代码段,以将其作为默认设置:

## WORKAROUND: https://github.com/rstudio/rstudio/issues/6692
## Revert to 'sequential' setup of PSOCK cluster in RStudio Console on macOS and R 4.0.0
if (Sys.getenv("RSTUDIO") == "1" && !nzchar(Sys.getenv("RSTUDIO_TERM")) && 
    Sys.info()["sysname"] == "Darwin" && getRversion() == "4.0.0") {
  parallel:::setDefaultClusterOptions(setup_strategy = "sequential")
}

1 个答案:

答案 0 :(得分:3)

这是与RStudio有关的已知问题。在Stan论坛和Github上查看这些相关文章。

Github:https://github.com/rstudio/rstudio/issues/6692

斯坦论坛:https://discourse.mc-stan.org/t/r-4-0-0-and-cran-macos-binaries/13989/13