我正在尝试使用 Rstan 拟合来自 Christensen、Johnson、Branscum 和 Hanson 的贝叶斯思想和数据分析的示例模型:科学家和统计学家简介。作者使用 WinBUGS,因此需要进行一些调整。数据在这里,WinBUGS 代码复制在这篇文章的底部。这是一个非常简单的模型,但我是一个完整的初学者,我不知道如何解决我遇到的错误。我的斯坦代码如下:
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
}