在R中使用brm进行负二项式回归,当使用多核时,会造成错误。

问题描述 投票:0回答:1

我正在计算一个负二项式回归,使用的是 brm 函数从 brms 包。由于这需要相当长的时间,我想使用多核,正如在 文件.

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.

我试着去了解这个问题,在SO上看了一些问题,如 这个但不明白为什么我不能连接。我使用macOS Mojave,问题不在于我尝试使用更多的核心。有什么建议可以让我在多核上运行吗?


编辑:正如sjp在回答中指出的,RStudio存在一个问题。我想我把解决这个问题的代码就在我的问题中分享出来,这样大家偶然遇到就可以解决这个问题,而不用再点击(和阅读)。

问题是 parallel R-4.0.0.的软件包。这个 stan论坛。如果你能用 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")
}
r sockets non-linear-regression
1个回答
3
投票

这是一个与RStudio有关的已知问题。请查看 Stan 论坛和 Github 上的相关帖子。

Github。https:/github.comrstudiorstudioissues6692。

斯坦论坛。https:/discourse.mc-stan.orgtr-4-0-0-and-cran-macos-binaries1398913。

© www.soinside.com 2019 - 2024. All rights reserved.