Syntax error with transformed parameters

I’m trying to write a simple stan model with students, classrooms, and schools. I’m getting the following syntax error:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:

variable "vector" does not exist.
  error in 'model_two' at line 25, column 9
  -------------------------------------------------
    23:   vector[J] b;
    24:   b = b_raw * tau;
    25:   vector[S] c;
                ^
    26:   c = c_raw * nu;
  -------------------------------------------------

This is is my stan code:

data {
  int<lower=0> N;      // number of data items
  int<lower=0> K;      // number of predictors
  int<lower=0> J;      // number  of classrooms
  int<lower=0> S;      // number  of schools
  vector[N] y;         // outcome vector
  vector[K] x[N];      // predictor matrix
  int classroom[N];    // numeric of classroom identifier
  int school[N];       // numeric of school identifier
}

parameters {
  real alpha;                   // intercept
  vector[K] beta;               // coefficients for predictors
  real<lower=0> sigma;          // error sd
  vector[J] b_raw;              // classroom random effects
  real<lower=0> tau;            // classroom sd
  vector[S] c_raw;              // school random effects
  real<lower=0> nu;             // school sd
}

transformed parameters{
  vector[J] b;
  b = b_raw * tau;
  vector[S] c;
  c = c_raw * nu;
}



model {
  real yHat[N];
  for(i in 1:N){
    yHat[i] = alpha + dot_product(x[i], beta) + b[classroom[i]] + c[school[i]];
    tau ~ normal(0,1);
    nu ~ normal(0,1);
    sigma ~ normal(0,1);
  }
  y ~ normal(yHat, sigma);       // likelihood
  b_raw ~ normal(0, 1);
  c_raw ~ normal(0, 1);
  beta ~ normal(0,1); 
}

What am I doing wrong?

Thanks!

Variables have to be defined at the top of whatever block they’re in.

transformed parameters {
  vector[J] b;
  vector[S] c;
  b = b_raw * tau;
  c = c_raw * nu;
}

Should work!

3 Likes

I think we need to be encouraging

transformed parameters {
  vector[J] b = b_raw * tau;
  vector[S] c = c_raw * nu;
}

It is easier to read and allows you to refer to variables that have just been defined. You should only need to define a variable after declaring it if the definition involves a loop or something, but even then it is usually better to call a user-defined function.

2 Likes

I agree with @bgoodri to the point where I re-marked his answer as the solution because it’s how we want to suggest people organize their local variables wherever possible.

I was have the same problems! thanks

Just realized tau and nu and sigma should have their priors declared outside the loop:

model {
  real yHat[N];

  for(i in 1:N){
    yHat[i] = alpha + dot_product(x[i], beta) + b[classroom[i]] + c[school[i]];
  }

  y ~ normal(yHat, sigma);       // likelihood
  b_raw ~ normal(0, 1);
  c_raw ~ normal(0, 1);
  beta ~ normal(0,1);

  tau ~ normal(0,1);
  nu ~ normal(0,1);
  sigma ~ normal(0,1);
}
1 Like