首页 > 解决方案 > 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);
}

我想弄清楚这个错误的原因以及如何避免它

另外我想提一下:

  1. 以下代码正常工作并返回 10:

    fx <- inline::cxxfunction( 签名(x = "integer", y = "numeric" ) , ' return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ; ' ) fx( 2L, 5)

  2. 我已经安装了最新的 Rtools 冻结版本。

  3. 此外,Rtools、R 和 Rstudio 位于没有空格的文件夹中。
  4. 我在 Windows 上工作(不是笔记本电脑)
  5. 我一直严格遵循安装指南中的所有步骤。

将非常感谢您的帮助!

更新

我还按照 Ben Goodrich 的建议设置了 Sys.setenv(USE_CXX14=1) 但现在我收到以下错误消息

在此处输入图像描述

标签: rstan

解决方案


这是一个临时问题,因为(本周)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 上,这些行应该已经存在。


推荐阅读