# Prior for Variance Covariance Matrix and Error obtained in RStan regarding that

``````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[K,N] mu;
vector[4] v;

for (i in 1:K)
v[i]=1;

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

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

Above is my hierarchical model, then I run the following code,

``````rt = stan(file = "mystancode.stan",data = dataList,iter=2000,warmup=1500,chains = 4,thin=25)
``````

Then I am getting the following error,

``````Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: inv_wishart_lpdf: LDLT_Factor of random variable is not positive definite.  last conditional variance is -1.61381.  (in 'modeld4ca7d2dbf_mystancode' at line 26)

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: inv_wishart_lpdf: LDLT_Factor of random variable is not positive definite.  last conditional variance is -16.0546.  (in 'modeld4ca7d2dbf_mystancode' at line 26)

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: inv_wishart_lpdf: LDLT_Factor of random variable is not positive definite.  last conditional variance is 11.1906.  (in 'modeld4ca7d2dbf_mystancode' at line 26)

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: inv_wishart_lpdf: LDLT_Factor of random variable is not positive definite.  last conditional variance is -1.29482.  (in 'modeld4ca7d2dbf_mystancode' at line 26)

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: inv_wishart_lpdf: LDLT_Factor of random variable is not positive definite.  last conditional variance is -1.612.  (in 'modeld4ca7d2dbf_mystancode' at line 26)

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: inv_wishart_lpdf: LDLT_Factor of random variable is not positive definite.  last conditional variance is 0.623838.  (in 'modeld4ca7d2dbf_mystancode' at line 26)

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: inv_wishart_lpdf: LDLT_Factor of random variable is not positive definite.  last conditional variance is 0.701463.  (in 'modeld4ca7d2dbf_mystancode' at line 26)

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: inv_wishart_lpdf: LDLT_Factor of random variable is not positive definite.  last conditional variance is 0.904612.  (in 'modeld4ca7d2dbf_mystancode' at line 26)

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: inv_wishart_lpdf: LDLT_Factor of random variable is not positive definite.  last conditional variance is -1.10447.  (in 'modeld4ca7d2dbf_mystancode' at line 26)

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: inv_wishart_lpdf: LDLT_Factor of random variable is not positive definite.  last conditional variance is 0.706109.  (in 'modeld4ca7d2dbf_mystancode' at line 26)

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: inv_wishart_lpdf: LDLT_Factor of random variable is not positive definite.  last conditional variance is 3.27242.  (in 'modeld4ca7d2dbf_mystancode' at line 26)

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: inv_wishart_lpdf: LDLT_Factor of random variable is not positive definite.  last conditional variance is 40.5057.  (in 'modeld4ca7d2dbf_mystancode' at line 26)

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: inv_wishart_lpdf: LDLT_Factor of random variable is not positive definite.  last conditional variance is -1.54971.  (in 'modeld4ca7d2dbf_mystancode' at line 26)

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: inv_wishart_lpdf: LDLT_Factor of random variable is not positive definite.  last conditional variance is 0.638855.  (in 'modeld4ca7d2dbf_mystancode' at line 26)

Chain 1:
Chain 1: Initialization between (-2, 2) failed after 100 attempts.
Chain 1:  Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
``````

Please help me out and let me know what prior should I choose for variance covariance matrix and other things to help me out with this code.

To ensure that your covariance matrix respects the appropriate constraints, you should declare it as the special data type `cov_matrix` rather than `matrix`. For efficiency, it can be even better to declare the Cholesky factor of the covariance matrix (or of the correlation matrix). For more on these data types, see:

For more on using some of these Cholesky-factor tricks, see the generated brms code for a multivariate hierarchical model as well as the “Multivariate reparameterizations” paragraph here