Multivariate Bayesian Modelling via Stan

 data {
    int<lower=0> N;
    int<lower=0> K;
    matrix[N,K] y;
     matrix[N,K] X;
    
  }
   parameters {
     matrix[K,K] lambda_hat;
     matrix[K,K] sigma_0;
     matrix[K,K] L;
   }
   transformed parameters {
     matrix[N,K] mu;
     vector[4] v;
     for (i in 1:K)
       v[i]=1;
     
     for (i in 1:N)
       mu[i,]'=lambda_hat*x[i,]';

   }
   model {
     L~ wishart(K,diag_matrix(v));
     sigma_0~ wishart(K,L);
     lambda_hat~ wishart(K,L);
     for (t in 1:N)
       y[t,]'~ multi_normal(y[(t-1),]'+mu[t,]',sigma_0);
   }

I am having the following error when I run this code,

rt = stanc("mystancode.stan")
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Illegal statement beginning with non-void expression parsed as
  transpose(mu[i, :])
Not a legal assignment, sampling, or function statement.  Note that
  * Assignment statements only allow variables (with optional indexes) on the left;
  * Sampling statements allow arbitrary value-denoting expressions on the left.
  * Functions used as statements must be declared to have void returns

 error in 'model2e850c17b4_mystancode' at line 20, column 7
  -------------------------------------------------
    18:      
    19:      for (i in 1:N)
    20:        mu[i,]'=lambda_hat*x[i,]';
              ^
    21: 
  -------------------------------------------------

PARSER EXPECTED: <statement>
Error in stanc("mystancode.stan") : 
  failed to parse Stan model 'mystancode' due to the above error.

Please help me out.

The issue is that you’re trying to assign the result to a transposition (i.e., mu[i,]'), which isn’t allowed. Instead, try transposing the product:

     for (i in 1:N)
       mu[i,] = (lambda_hat*x[i,]')';
1 Like

Thanks a lot @andrjohns

Is the following part correct? or do I also need to change that?

model {
     L~ wishart(K,diag_matrix(v));
     sigma_0~ wishart(K,L);
     lambda_hat~ wishart(K,L);
     for (t in 1:N)
       y[t,]'~ multi_normal(y[(t-1),]'+mu[t,]',sigma_0);
   }

No, it’s fine to use a transposition as the target for a prior

Thanks @andrjohns