简单多元线性模型的Rstan代码

时间:2015-04-05 23:35:42

标签: r bayesian winbugs stan rstan

我试图使用Rstan来拟合Christensen,Johnson,Branscum和Hanson的贝叶斯思想和数据分析:科学家和统计学家简介的示例模型。作者使用WinBUGS,因此需要进行一些调整。数据为here,WinBUGS代码复制在本文的底部。这是一个非常简单的模型,但我是一个完整的初学者,我无法弄清楚如何解决我得到的错误。我的Stan代码如下:

data {
  int N_subjects;
  int N_items;
  matrix[N_subjects,N_items] y;
}

parameters {
  vector[N_items] mu;
  real<lower=0> sigma;
  real<lower=-1,upper=1> rho;
}

transformed parameters {
  cov_matrix[N_items] Sigma;
  for (j in 1:N_items)
    for (k in 1:N_items)
      Sigma[j,k] <- pow(sigma,2)*pow(rho,step(abs(j-k)-0.5));
}

model {
  sigma ~ uniform(0,100);
  rho ~ uniform(0,1);
  mu ~ multi_normal(0,100);
  for (i in 1:N_subjects)
    y[i] ~ multi_normal(mu,Sigma);
}

解析器抛出以下错误:

Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  failed to parse Stan model 'model' with error message:
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
no matches for function name="multi_normal_log"
    arg 0 type=vector
    arg 1 type=int
    arg 2 type=int
available function signatures for multi_normal_log:
0.  multi_normal_log(vector, vector, matrix) : real
1.  multi_normal_log(vector, row vector, matrix) : real
2.  multi_normal_log(row vector, vector, matrix) : real
3.  multi_normal_log(row vector, row vector, matrix) : real
4.  multi_normal_log(vector, vector[1], matrix) : real
5.  multi_normal_log(vector, row vector[1], matrix) : real
6.  multi_normal_log(row vector, vector[1], matrix) : real
7.  multi_normal_log(row vector, row vector[1], matrix) : real
8.  multi_normal_log(vector[1], vector, matrix) : real
9.  multi_normal_log(vector[1], row vector, matrix) : real
10.  multi_normal_log(row vector[1], vector, matrix) : real
11.  multi_normal_log(row vector[1], row vector, matrix) : real
12.  multi_normal_log(vector[1], vector

(我认为)我理解解析器告诉我我试图将不适当的数据类型传递给模型块中的multi_normal函数,但我无法弄清楚它的来源。我怀疑我在定义协方差矩阵时做错了什么,但似乎不止一个参数的数据类型不正确......

WinBUGS代码我在我的Stan代码上建模:

model{
for(i in 1:30){
for(j in 1:6){
logy[i,j] <- log(y[i,j])
}
}
for(i in 1:30){logy[i,1:6]~dmnorm(m[1:6],precision[1:6,1:6])}
for(j in 1:6){
for(k in 1:6){
covariance[j,k] <- sigma2*pow(rho, step(abs(j-k)-0.5))
}
}
for(i in 1:6){ m[i] <- mu }
precision[1:6,1:6] <- inverse(covariance[1:6,1:6])
sigma ~ dunif(0,100)
mu ~ dnorm(0,0.001)
L <- -1/(6-1)
rho ~ dunif(L,1)
sigma2 <- sigma*sigma
tau <- 1/sigma2
}

1 个答案:

答案 0 :(得分:2)

错误来自

mu ~ multi_normal(0,100);

当您传递矢量mu,整数0和整数100.我想你想要

mu ~ normal(0,100);

将mu的元素视为独立且相同的正态分布,均值为0,标准差为100。