Correlated coefficients example - what's going wrong?

If you are going to do a model like this, you should usually utilize a non-centered parameterization where the coefficients become transformed parameters

data {
  int<lower=1> N; //data points
  int<lower=0> Y[N]; //dependent variable
  real X1[N]; //predictor 1
  real X2[N]; //predictor 2
  int<lower=2> K; //number of corr coeffs
}
parameters {
  real beta0; //intercept
  vector[K] beta_raw; // primitive vector of correlated coeffs
  vector[K] beta_mu; //vector of means for correlated coeffs
  cholesky_factor_corr[K] L; // Cholesky factor of correlation matrix for correlated coeffs
  vector<lower=0>[K] sigma; // vector of variance for correlated coeffs
}
transformed parameters {
  vector[K] beta = beta_mu + sigma .* (L * beta_raw);
}
model {
  //Priors on coeffs
  sigma ~ normal(0,2);
  beta_mu ~ normal(0,1);
  L ~ lkj_corr_cholesky(2);
  beta_raw ~ normal(0, 1); // implies:
  // beta ~ multi_normal(beta_mu, quad_form_diag(L * L', sigma));
  //Prior on intercept
  beta0 ~ normal(0, 2);
  //likelihood
  target += poisson_log_lpmf(Y | beta0 + beta[1] * X1 + beta[2] * X2);
}

where I have also eliminated the loop in the log-likelihood.

2 Likes