r - R stan 8schools 示例失败
问题描述
我正在尝试从这个官方教程中运行经典的 r-stan 示例。但我不能,因为我收到以下错误消息:
我使用的代码如下:
schools_dat <- list(J = 8,
y = c(28, 8, -3, 7, -1, 1, 18, 12),
sigma = c(15, 10, 16, 11, 9, 11, 10, 18))
fit <- stan(file = '8schools.stan', data = schools_dat,
iter = 1000, chains = 4)
其中 8schools.stan 是我另外创建并使用以下代码嵌套的文件:
data {
int<lower=0> J; // number of schools
real y[J]; // estimated treatment effects
real<lower=0> sigma[J]; // s.e. of effect estimates
}
parameters {
real mu;
real<lower=0> tau;
real eta[J];
}
transformed parameters {
real theta[J];
for (j in 1:J)
theta[j] = mu + tau * eta[j];
}
model {
target += normal_lpdf(eta | 0, 1);
target += normal_lpdf(y | theta, sigma);
}
我想弄清楚这个错误的原因以及如何避免它。
另外我想提一下:
以下代码正常工作并返回 10:
fx <- inline::cxxfunction( 签名(x = "integer", y = "numeric" ) , ' return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ; ' ) fx( 2L, 5)
我已经安装了最新的 Rtools 冻结版本。
- 此外,Rtools、R 和 Rstudio 位于没有空格的文件夹中。
- 我在 Windows 上工作(不是笔记本电脑)
- 我一直严格遵循安装指南中的所有步骤。
将非常感谢您的帮助!
更新
我还按照 Ben Goodrich 的建议设置了 Sys.setenv(USE_CXX14=1) 但现在我收到以下错误消息
解决方案
这是一个临时问题,因为(本周)StanHeaders R 包(需要 C++14)已被 CRAN 接受,但尚未接受 rstan R 包(使用 C++14 编译模型) . 所以,暂时,你首先需要打电话,
Sys.setenv(USE_CXX14=1)
但任何人在未来读这篇文章的人都不太可能需要明确地这样做(rstan R 包将在内部完成)。
在 Windows 上,您需要放入
CXX14=g++ -std=c++1y
CXX14FLAGS=-O3
~/.R/Makevars 文件,但在 Mac 上,这些行应该已经存在。
推荐阅读
- facebook-graph-api - 如何找到 Instagram 应用的所有者 - 获得 AppId 和 AppSecret/ClientToken
- javascript - 如何使用硒单击甜甜圈图?
- xml - XSL-FO 和正确使用 xsl:template 和 xsl:for-each
- sql - 每组行具有唯一递增 id 的 SQL 查询(Postgresql)
- flutter - Getx Flutter - 更新列表中的项目不是反应性的
- python - 如何用while循环实现beautifulsoup并持续检测数据变化
- c# - 在 IGrouping 上调用自定义 IQueryable 扩展方法
- model-view-controller - Web 应用程序架构和 Web 应用程序架构设计之间有区别吗
- visual-studio-code - Visual Studio Code 有时会自动删除光标后的代码
- windows - Lilypond for Windows 的安装问题