Error messages RStan

Hi There, I keep running in to error when I am trying to fit the multilevel model using this RStan code. The error message ask for “p”, which I declared as the probability of transmission. this probability is in binomial distribution and “p” is present in the model. thus, I don’t know how to fix this.

data{
    int<lower=0> M; // data length
    int<lower=0> N; // number of animal per pen
    int isol[M]; 
    int s0[M]; 
    int i0[M]; 
    int dt[M];
    int time[M];
    real timeck[M,N]; 
    int e0; 
}

parameters{
    vector[12] z;
    //real a_bar;
    real<lower=0> sigma; 
    real<lower=0> delta; // decay rate added
    real<lower=0> beta; //transmission rate
}

transformed parameters{
   real<lower=0> et;
    for(i in 1:M){
      et = e0*exp(-delta * time[i]);
      for(j in 1:N){
       et += ((1/delta)*(1-exp(-delta* timeck[i,j])));
    }
}
}

model{
    z ~ normal( 0 , 1 ); 
    sigma ~ exponential( 0.5 );
    delta ~ normal(0.01,0.1);
    beta ~ normal(0.2,0.5);
    for ( i in 1:M ) {
      p[i] = beta+z[isol[i]]*sigma+log((((1-exp(-delta*dt[i]))*et) - (i0[i]*(1-(exp(-delta*dt[i]))-delta*dt[i]))/delta)/delta);
      p[i] = 1-exp(-exp(p[i]));
    }
    ca ~ binomial( s0 , p );
}

generated quantities{
    vector[M] log_lik;
    vector[M] p;
    vector[12]a;
    a = beta+z * sigma;
    for ( i in 1:M) {
         p[i] = beta+z[isol[i]]*sigma+log((((1-exp(-delta*dt[i]))*et) - (i0[i]*(1-(exp(-delta*dt[i]))-delta*dt[i]))/delta)/delta);
         p[i] = 1-exp(-exp(p[i]));
    }
    for (i in 1:M) log_lik[i]=binomial_lpmf( ca[i] | s0[i],p[i] );
 }

The error message is the following

Variable "p" does not exist.
    40:       p[i] = beta+z[isol[i]]*sigma+log((((1-exp(-delta*dt[i]))*et) - (i0[i]*(1-(exp(-             delta*dt[i]))-delta*dt[i]))/delta)/delta);
              ^
    21:       p[i] = 1-exp(-exp(p[i]));

Thank you for your help

You are assigning to p in model block but it hasn’t been declared before that. You need the vector[M] p; declararation also there. But it seems that it would be better for you to instead declare p in transformed parameters, so you only need to compute it only there and then can use it in both model and generated quantities.

Dear jtimonen

Thank you very much. I declare p variable in transformed parameters, also in parameters, and also in the model. I tried declared with vector and also real but both came back with error = variable “real” does not exist.

then I tried declared it in the loop but it churn out error of
" Variable definition base type mismatch, variable declared as base type vector variable definition has base type realVariable definition dimensions mismatch, definition specifies 1, declaration specifies 0 error"

model{
    //a_bar ~ normal( -2 , 2 );
    z ~ normal( 0 , 1 ); 
    sigma ~ exponential( 0.5 );
    delta ~ normal(0.01,0.1);
    beta ~ normal(0.2,0.5);
    for ( i in 1:M ) {
      vector[i] p  = beta+z[isol[i]]*sigma+log((((1-exp(-delta*dt[i]))*et) - (i0[i]*(1-(exp(-delta*dt[i]))-delta*dt[i]))/delta)/delta);
      vector[i] p = 1-exp(-exp(p[i]));
    }
    ca ~ binomial( s0 , p );
}

Can you please suggest the solution.
Thank you very much

You need to put all declarations of a block before any other statements. The entire model block would be


model{
    vector[M] p;
    z ~ normal( 0 , 1 ); 
    sigma ~ exponential( 0.5 );
    delta ~ normal(0.01,0.1);
    beta ~ normal(0.2,0.5);
    for ( i in 1:M ) {
      p[i] = beta+z[isol[i]]*sigma+log((((1-exp(-delta*dt[i]))*et) - (i0[i]*(1-(exp(-delta*dt[i]))-delta*dt[i]))/delta)/delta);
      p[i] = 1-exp(-exp(p[i]));
    }
    ca ~ binomial( s0 , p );
}

This you should not do.

Dear jtimonen

It fixed! thank you very much

Best wishes

1 Like