Problem in generated quantities

Hello guys I’m trying to run a model in Stan with the following specs. Everything works well. But when I add the generated quantities (y_pred) the model won’t compile and it does not check on RStudio (doesn’t give any error). Without the generated quantities it runs smoothly. X dimensions are 20095 by 4.

What am I doing wrong?

data {
  int<lower=1> N; //the number of observations
  int<lower=1> K; //number of columns in the model matrix
  matrix[N, K] X; //the model matrix
  vector[N] y; //the response variable

parameters {
  real alpha;           // intercept
  vector[K] beta;       // coefficients for predictors
  real<lower=0> sigma;  // error scale

model {
 y ~ normal(alpha + X * beta, sigma);  // likelihood

generated quantities {
  vector[N] y_pred;
  for (n in 1:N){
  y_pred[n] = normal_rng(alpha + beta * X[n], sigma);

Thank you for your help

The funny thing about matrix multiplication is that it is not commutative. X is a matrix so X[n] is a row vector. A row vector times a column vector is a scalar but a column vector times a row vector is a matrix.
In the model block you have X * beta and that is correct.
But in the generated quantities you have beta * X[n] which is backwards. It should be X[n] * beta.


Oh boy. It makes sense. Thank you so much.
It now runs smoothly.