How to combine vectors into one Matrix in Stan program

Hello all,

I have a question. Suppose I create some variables in the Model part of the Stan program as given :

model {
vector[N] X1;
vector[N] X2;
vector[N] X3;

X1 = alpha_X1 + Z1*beta_Z1;

X2 = alpha_X2 + Z2*beta_Z2;

X3 = alpha_X3+ Z2*beta_Z3;

Y ~normal(alpha + X1 * beta_1+ X2 * beta_2 + X3 * beta_3, sigma);

}

My question is instead of writing like X1 * beta_1+ X2 * beta_2 + X3 * beta_3 , can I put X1, X2 and X3 into a matrix, something like matrix[N,3] X?

This will then allow me to write my likelihood as :

Y ~normal(alpha + X*beta, sigma);?

Thanks for your help in advance.

Regards
Ants.

You can do:

matrix[N, 3] X;
X[,1] = alpha_X1 + Z1*beta_Z1;
X[,2] = alpha_X2 + Z2*beta_Z2;
X[,3] = alpha_X3 + Z3*beta_Z3;
2 Likes

Great Thanks Andrew!