Simple hierarchical model failing

I have a simple stan program that I’m trying to write. The model is as follows:

Vote[i] = Beta_1 + Beta_2 * income[i] + Beta_3[i] * age[i]
Beta_3[i] ~ N(lambda_1 + lambda_2 * income[i], tau)
tau ~ halfcauchy(0, 2)

The program is shown below:

  data {
    int<lower=0> N;
    vector[N] age;
    vector[N] income;
    int<lower=0, upper=1> vote[N];
  }

  parameters {
    vector[2] beta;
    vector[2] lambda;
    real<lower=0> tau;
  }

  model {
    vector[N] beta_age;
    vector[N] eta;

    beta_age ~ normal(lambda[1] + lambda[2] * income, tau);

    // eta = beta[1] + beta[2] * income + beta_age .* age;
    // vote ~ bernoulli_logit(eta);
  }

I generated a synthetic dataset in R and tried to fit the model. But I keep getting:

Rejecting initial value:
  Error evaluating the log probability at the initial value.
Exception: normal_lpdf: Random variable[1] is nan, but must not be nan!  

I’ve checked by printing that lambda[1] + lambda[2] * income is not NaN, nor is tau. What’s going on here?

I guess that beta_age is nan.

y ~ normal(mu, sigma) just stands for target += normal_lpdf(y | mu, sigma) in Stan. Your error message tells you that the first element of normal_lpdf is nan.

You will either will have to specify the full likelihood

Vote[i] = Beta_1 + Beta_2 * income[i] + Beta_3[i] * age[i]
Beta_3[i] ~ N(lambda_1 + lambda_2 * income[i], tau)

or you have to generate beta_age, lambda, and tau in the generated quantities blog with the *_rng functions.

beta_age must be a parameter in the parameter section.

Yup, that works! Thanks! But what if I don’t want to export beta_age?

Leaving this here, in case someone finds it useful. I checked with @jonah and there is no way not to export beta_age. However, we can post-process the fit object in R and have it ignore beta_age.