Syntax error: Sum of means and variances in a loop from a latent variable

I am working on a model attempting to link migration data aggregated in 5 year chunks (sum of migrants in 5 years) with yearly precipitation data. The model is built assuming that during “normal” precipitation years the yearly migration (which we do not observe) comes from a distribution with mean=mu[1] and var=sigma[1], and in dry years it comes from a mix of mu[1], sigma[1] and mu[2], sigma[2] with the weights of the mixture determined by the precipitation (parameter theta in the model). I want to obtain this “unobserved” yearly migration, aggregate the mean and var in 5 year chunks and use this to get the likelihood with the observed 5-year migration aggregates. I want to do the inference for mu[1], mu[2], sigma[1], sigma[2] and the yearly weights.

With the model below, I am getting the following syntax error: SYNTAX ERROR, MESSAGE(S) FROM PARSER:
error in ‘model5c4c5c7b3c75_gauss_mix_PC_theo’ at line 41, column 11

39: for(r in 1:M)
40: {
41: mm[e,r]=sum(z[inx[e],r]:z[inx[e+4],r]); //aggregate z in five year intervals
42: ss[e.r]=sum(w[inx[e],r]:w[inx[e+4],r]); 

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

I am not sure if this index aggregation is correct or if the transformed parameters chunk is the right place to put it. Any help will be much appreciated. Thanks!

data {
  int<lower = 0> N; // Number of periods where we have 5-year data aggregates
  int<lower = 0> M; //Number of locations
  int<lower = 0> P; //Number of years of climate data (5*N)
  int inx[N]; // An index to do 5 year loops
  matrix[N,M] y; // Mig data at each location in 5-year aggregates 
  matrix[P,M] x; // yearly precipitation in each location

parameters {
  ordered[2] mu; 
  real<lower=0> sigma[2];
  real b;
  real a; 
transformed parameters {
matrix[P,M] theta; // mixture weights for each year and each place dependent on precipitation
matrix[P,M] z; // to store the yearly migration mean, which is a combination of mu[1] and mu[2] depending on precip (theta) 
matrix[P,M] w; // to store the yearly migration variance
matrix[N,M] mm; // to store the five year aggregates of z 
matrix[N,M] ss; // to store the five year aggregates of w

for(p in 1:P)
for(m in 1:M)
theta[p,m]=a+b*x[p,m]; //To get weigths for each mu depending on rainfall. This is transformed to inv_logit

for (j in 1:P)
for (k in 1:M)

for(e in 1:N)
for(r in 1:M)
mm[e,r]=sum(z[inx[e],r]:z[inx[e+4],r]); //aggregate z in five year intervals

model {
  sigma ~ normal(0, 2);

//Likelihood on the observed data y (5 year migration aggregates)
for(n in 1:N)
for(m in 1:M)

For this code:


If you’re looking for the sum z[inx[e], r] + z[inx[e + 1], r] + z[inx[e + 2], r] + z[inx[e + 3], r] + z[inx[e + 4], r], then do:


I changed quite a bit in that sum though just based on what I thought would compile and run – wasn’t really thinking about the model so let me know if that looks wrong.

Also this should probably also be a comma between the e and r:

1 Like

Thank you so much for replying! I had to tweak your response a little but it worked! I had spent so much time trying to figure out where the error was so I really appreciate it.

1 Like