R语言中无法运行brm()函数

r语言,无法运行brms

img

img


在网上自己搜了很多,

Error in compileCode(f, code, language = language, verbose = verbose) : 
  C:\RBuildTools\4.2\x86_64-w64-mingw32.static.posix\bin/ld.exe: file4e8473fd1cc9.o:file4e8473fd1cc9.cpp:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D0Ev[_ZN3tbb8internal26task_scheduler_observer_v3D0Ev]+0x1d): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\RBuildTools\4.2\x86_64-w64-mingw32.static.posix\bin/ld.exe: file4e8473fd1cc9.o:file4e8473fd1cc9.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD1Ev[_ZN3tbb10interface623task_scheduler_observerD1Ev]+0x1d): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\RBuildTools\4.2\x86_64-w64-mingw32.static.posix\bin/ld.exe: file4e8473fd1cc9.o:file4e8473fd1cc9.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD1Ev[_ZN3tbb10interface623task_scheduler_observerD1Ev]+0x3a): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\RBuildTools\4.2\x86_64-w64-mingw32.static.posix\bin/ld.exe: file4e8473fd1cc9.o:file4e8473fd1cc9.c
Error in sink(type = "output") : invalid connection

得知这一段报错很常见,与rstan与当前R的版本不匹配有关,但是我根据各种解决办法,包括更换R和Rtools的版本、安装cmdr等等,都无法解决。其中更换R的版本以后,我第一次换成了R4.0.0,把各种东西配置好以后,运行例子报错:

img


所以我又更换了R4.0.5版本,安装了rstan:

img


然后运行例子,这次显示:

img

一筹莫展了,其实没写出来的尝试还有很多,根据这些报错去官网还有可以找到的大部分帖子下面都找过解决办法,比如
https://zhuanlan.zhihu.com/p/577892365
这里提到的安装CmdStanR,通过原文给出的代码

install.packages("cmdstanr", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

我无法下载:

img


但是我找到一个通过github下载的办法

# install.packages("remotes")
remotes::install_github("stan-dev/cmdstanr")

img


这一次下载成功了,然后根据指示检查C++工具链,并下载CmdStan

cmdstanr::check_cmdstan_toolchain(fix = TRUE)

cmdstanr::install_cmdstan()

第一行运行以后:

> cmdstanr::check_cmdstan_toolchain(fix = TRUE)
The C++ toolchain required for CmdStan is setup properly!

但是后面要安装cmdstan的时候就会失败

> cmdstanr::install_cmdstan()
The C++ toolchain required for CmdStan is setup properly!
* Latest CmdStan release is v2.31.0
* Installing CmdStan v2.31.0 in C:/Users/cj/Documents/.cmdstan/cmdstan-2.31.0
* Downloading cmdstan-2.31.0.tar.gz from GitHub...
Error: Download of CmdStan failed. Please try again.

vpn已经试过,没有什么改变。
总结一下就是运行brms包中的函数时就会报错,Error in sink(type = "output") : invalid connection,换版本之类的尝试过,请问还有没有什么解决办法,或者说我之前列举的尝试还有什么地方做错了,求一个解决办法,谢谢了

目前已经解决了,方法参照https://github.com/stan-dev/rstan/wiki/Configuring-C---Toolchain-for-Windows#rstan-compatibility%E8%BF%99%E9%87%8C%E6%8F%90%E5%88%B0%E7%9A%84%EF%BC%8C
其实这个方法我很早之前就试过 ,但是之前显示的是报错

Error in download.file(url, destfile, method, mode = "wb", ...) : 
  download from 'https://mc-stan.org/r-packages/bin/windows/contrib/4.2/rstan_2.26.16.zip' failed
Warning in install.packages :
  下载程序包‘rstan’时出了问题

但是今天换了一个网络,重启了R以后莫名安装成功了......之后就可以正常运行了。

缺少必要的包:确保您已经安装了R语言的所有必要包,例如ggplot2、reshape2、e1071等。您可以使用以下命令在终端中安装这些包:
install.packages("ggplot2")
install.packages("reshape2")
install.packages("e1071")
缺少必要的依赖项:确保您已经安装了R语言的所有必要依赖项,例如devtools等。您可以使用以下命令在终端中安装这些依赖项:
install.packages("devtools")
配置不正确:确保您的R环境设置正确,例如设置了正确的工作目录、包含了正确的R版本等。您可以使用以下命令在终端中设置R环境:
options("R.environment", "path/to/your/R/environment")
缺少必要的库文件:确保您已经安装了R语言的所有必要库文件,例如ggplot2.2.0等。您可以使用以下命令在终端中安装这些库文件:
install.packages("ggplot2", repos="http://cran.r-project.org%22/)