Stan error: require unconstrained variable declaration. found simplex

1.1k views Asked by At

In Stan, I get the following error:

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;
                        ^

I don't quite understand what is the problem. When I used real pi[7] instead of simplex[7] pi, I got different error:

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:    

which I don't understand either... My whole code:

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

There are 1 answers

1
Ben Goodrich On BEST ANSWER

Constrained local parameters, such as simplexes, cannot be declared inside the model block because they are not checked. So, you should just declare pi to be a plain vector of length 7, like vector[7] pi;. Nevertheless, pi needs to be on the simplex in order to be an admissible argument to the categorical function.

If it is the case that symbolically pi is non-negative and sums to 1, then it is a question of making sure that numerically they are sufficiently close to non-negative and sum to something that is sufficiently close to 1. I'm not sure what the numerical tolerance is for a simplex in Stan but there is some wiggle room. If numerical error is the problem then doing pi <- pi / sum(pi); before passing pi to the categorical function may help.