Stan错误:需要无约束的变量声明。找到了单纯形

时间:2014-11-25 08:39:38

标签: r stan

在Stan,我收到以下错误:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
require unconstrained variable declaration. found simplex.

ERROR at line 48
 46:    for (j in 1:records) {  
 47:            real phenology_predictor;
 48:            simplex[7] pi;
                        ^

我不太明白这是什么问题。当我使用real pi[7]代替simplex[7] pi时,我得到了不同的错误:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
no matches for function name="categorical_log"
    arg 0 type=int
    arg 1 type=real[1]
available function signatures for categorical_log:
0.  categorical_log(int, vector) : real
1.  categorical_log(int[1], vector) : real
unknown distribution=categorical

ERROR at line 63
 62:    
 63:                    Y[j] ~ categorical(pi);
                                      ^
 64:    

我也不明白......我的整个代码:

data {
    int sites;
    int records;
    int Y[records];
    vector[records] yday;
    int site[records];
}
transformed data {
    int M[sites];
}
parameters {
    real<lower=0,upper=1> psi;
    real<lower=0,upper=1000> phi_phen_scale;
    real phi_alpha;
    real q_date;
    real q_date2;
    real q_site[sites];
}
model {
    real p[records];
    real q[records];

// priors
phi_phen_scale ~ normal(0, 10);
phi_alpha ~ normal(0, 10);
q_date ~ normal(0, 10);
q_date2 ~ normal(0, 10);

// vectorized
M ~ bernoulli(psi);
q_site ~ normal(0, 10);

for (j in 1:records) {  
    real phenology_predictor;
    simplex[7] pi;

    phenology_predictor <- q_date * yday[j] + q_date2 * yday[j]^2;
    p[j] <- M[site[j]] * inv_logit(phi_alpha + phi_phen_scale * phenology_predictor);
    q[j] <- inv_logit(q_site[site[j]] + phenology_predictor);

    pi[1] <- 1-p[j] + p[j]*(1-q[j])^6; 
    pi[2] <- p[j]*q[j]  ;   
    pi[3] <- p[j]*(1-q[j])*q[j];
    pi[4] <- p[j]*(1-q[j])^2*q[j];
    pi[5] <- p[j]*(1-q[j])^3*q[j];
    pi[6] <- p[j]*(1-q[j])^4*q[j];
    pi[7] <- p[j]*(1-q[j])^5*q[j];

    Y[j] ~ categorical(pi);

}

}

1 个答案:

答案 0 :(得分:3)

不能在模型块中声明受约束的本地参数(例如,单工),因为它们未被检查。因此,您应该将pi声明为长度为7的普通向量,例如vector[7] pi;。然而,pi需要在单纯形式上才能成为分类函数的可接受论证。

如果是象征性的pi是非负的并且总和为1,那么这是一个确保在数字上它们足够接近非负并且总和足够接近1的问题的问题。我不确定Stan的单纯数字公差是多少,但有一些摆动空间。如果数字错误是问题,那么在将pi <- pi / sum(pi);传递给pi函数之前执行categorical可能有所帮助。