【发布时间】:2021-11-02 15:07:55
【问题描述】:
我正在 Jupyter Notebook 中编写关于 STAN 的教程,以供在 Google Collaboratory 上使用。我在 Github 上有所有项目文件(数据、模型代码、结果..),我需要从 Github 加载 STAN 模型。在这个笔记本中,我使用 R 语言和 cmdstanr 包。我首先加载 rpy2 以在 .ipynb 中运行 R
%load_ext rpy2.ipython
并且还要加载 cmdstanr 包:
%%R
library(cmdstanr)
那么工作原理是将文件手动上传到 Google Collaboratory,然后加载它:
%%R
setwd('my_dir')
file <- file.path("MyModel.stan")
MyModel <- cmdstan_model(file)
MyModel$print()
什么不起作用是在 Github 中加载文件。这是我尝试过的:
%%R
MyModel_URL <- url("https://github.com/laurabustamante/individual_differences_analysis_stan/blob/main/TwoStep_ITC_covar.stan?raw=true")
MyModel_txt <- readLines(MyModel_URL)
MyModel <- cmdstan_model(MyModel_txt)
我得到错误:
R[写入控制台]:初始化错误(...):断言开启 'stan_file' 失败:文件不存在:'// 同时拟合模型 用于两步任务和跨期选择任务'。
R[写入控制台]:另外:R[写入控制台]:警告 留言:
R[写入控制台]:在 readLines(TwoStep_ITC_covar_stan_URL) 中: R[写入控制台]:R[写入控制台]:最后一行不完整 发现于 'https://github.com/laurabustamante/individual_differences_analysis_stan/blob/main/TwoStep_ITC_covar.stan?raw=true'
我尝试了许多其他方法,但我认为其中没有一种更接近解决方案。我在网上搜索,似乎找不到任何东西。
另外请注意,我已经能够使用以下代码在同一笔记本中从 Github 加载 .RDS:
%%R
ITC_con <- gzcon(url("https://github.com/IanEisenberg/Self_Regulation_Ontology/blob/master/Data/Complete_02-16-2019/Individual_Measures/discount_titrate.csv.gz?raw=true"))
ITC_txt <- readLines(ITC_con)
ITC_data_full <- read.csv(textConnection(ITC_txt))
感谢您的想法!
【问题讨论】:
-
您可以尝试不使用
?raw=true扩展名吗?我的意思是MyModel_URL <- url("https://github.com/laurabustamante/individual_differences_analysis_stan/blob/main/TwoStep_ITC_covar.stan") -
看起来
MyModel_txt包含作为字符串而不是文件名的 stan 代码。看起来cmdstan_model()需要一个文件名。您也许可以使用write_stan_file()创建一个临时文件并弥合差距(请参阅mc-stan.org/cmdstanr/reference/write_stan_file.html)
标签: r github jupyter-notebook stan