Rstan problem

Please share your Stan program and accompanying data if possible.


When I run the following code




data {
  int<lower=0> N;         
  real Y[N];
  real X[N]; 
  real M1[N]; 
  real M2[N];
  real<lower=0> sigma1[N];
  real<lower=0> sigma2[N];
}
parameters {
  real ie;                
  real de;      
  real alpha1;
  real alpha2;
  real beta1;
  real beta2;
  real c;
  
}

model {
  for(i in 1:N){
    real mu_M1[i]=alpha1*X[i];
    M1[i] ~ bernoulli(mu_M1[i]);
    real mu_M2[i]=alpha2*X[i];
    M2[i] ~ normal(mu_M2[i],sigma1[i]);
    real mu_Y=c*X+beta1*M1[i]+beta2*M2[i];
    Y[i]~normal(mu_Y[i],sigma2[i]);
    real ie1=alpha1*mu_M1[i]*(1-mu_M1[i])*beta1
    real ie2=alpha2*beta2
    
  }
  
  
  
  alpha1~normal(0,1);
  alpha2~normal(0,1)
  mu_M2=alpha2*X;
  sigma1~gamma(1,0.1);
  
  
  c~normal(0,1);
  beta1~normal(0,1);
  beta2~normal(0,1);
  
  sigma2~gamma(1,0.1);
  
}  

I have the following errors:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Variable definition base type mismatch, variable declared as base type real[ ] variable definition has base type realVariable definition dimensions mismatch, definition specifies 1, declaration specifies 0 error in 'model356c14886602_567' at line 25, column 29
  -------------------------------------------------
    23: model {
    24:   for(i in 1:N){
    25:     real mu_M1[i]=alpha1*X[i];
                                    ^
    26:     M1[i] ~ bernoulli(mu_M1[i]);
  -------------------------------------------------

Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  failed to parse Stan model '567' due to the above error.

Hi,
sorry for not getting to you earlier. Note that real mu_M1[i] defines an array of size i (i.e. the array size will be different at each iteration of the cycle). It appears that you actually want to have just one value per iteration, so either reusing the same scalar variable as in

model {
  for(i in 1:N){
    real mu_M1=alpha1*X[i];
    ...
  }
}

or defining the array outside of the loop as in

model {
  real mu_M1[N];
  for(i in 1:N){
    mu_M1[i]=alpha1*X[i];
    ...
  }
}

should work.

Best of luck with your model!

1 Like