Multivariate Regression and Optimization through Cholesky Factorization - expression is ill formed

Hello Stan users,

I am following the recommendation in the Stan user’s guide to optimize a multivariate regression through Cholesky Factorization.

I’ve taken the code directly from the user’s guide (please see the code below) and was not expecting any error in the code. However, I am getting an error when I want to save the model in the following line. They syntax looks right. Any comments or suggestions will be appreciated. Thanks.

beta = u * gamma + (diag_pre_multiply(tau,L_Omega) * z)';

The error says ("Syntax error - from parser - no matches for row_vector[] * matrix).

Here is the code taken from the user’s guide:

data {
int<lower=0> N; // num individuals  11275
int<lower=1> K; // num ind predictors 4 
int<lower=1> J; // num groups or M = 114
int<lower=1> L; // num group predictors  1 only constant
int<lower=1,upper=J> jj[N]; // group for individual
matrix[N, K] x; // individual predictors matrix
row_vector[L] u[J]; // group predictors  1
vector[N] y; // outcomes

parameters {
matrix[K, J] z;
cholesky_factor_corr[K] L_Omega;
vector<lower=0,upper=pi()/2>[K] tau_unif;
matrix[L, K] gamma; // group coeffs
real<lower=0> sigma; // prediction error scale

//transformed parameters {
//matrix[J, K] beta;
//vector<lower=0>[K] tau; // prior scale
//for (k in 1:K) tau[k] = 2.5 * tan(tau_unif[k]);
//beta = u * gamma + (diag_pre_multiply(tau,L_Omega) * z)';
// matrix[J, K] r_1 = (diag_pre_multiply(tau,L_Omega) * z)';}

transformed parameters {
  matrix[J, K] beta;
  vector<lower=0>[K] tau; // prior scale
 for (k in 1:K) tau[k] = 2.5 * tan(tau_unif[k]);
  beta = u * gamma + (diag_pre_multiply(tau,L_Omega) * z)';

model {
to_vector(z) ~ std_normal();
L_Omega ~ lkj_corr_cholesky(2);
to_vector(gamma) ~ normal(0, 5);
y ~ normal(rows_dot_product(beta[jj] , x), sigma);



Welcome, Mauricio!

Change it to matrix[J, L] u; The operators are not defined for array of row_vector, but matrix.

1 Like

Awesome! That works. Thank you.