# Modeling Multivariate Multilevel Model in STAN

**URL:** <https://discourse.mc-stan.org/t/modeling-multivariate-multilevel-model-in-stan/2302>\
**Category:** Modeling\
**Created:** [October 25, 2017, 2:04am UTC](https://discourse.mc-stan.org/t/modeling-multivariate-multilevel-model-in-stan/2302 "2017-10-25T02:04:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunny](https://avatars.discourse-cdn.com/v4/letter/s/7c8e57/32.png) [@sunny](https://discourse.mc-stan.org/u/sunny)\
**Post date:** [October 25, 2017, 2:04am UTC](https://discourse.mc-stan.org/t/modeling-multivariate-multilevel-model-in-stan/2302/1 "2017-10-25T02:04:53Z")

</div>

Hello, I am a beginner of STAN and am reading User’s Guide and Reference Manual now.  
I would like to ask any opinion or thoughts to extend the multivariate outcomes model at Chapter 9 to multivariate ‘multilevel’ model.

For example, I had a data set which had two scores (math and science) at level-1 nested students at level-2, middle school at level-3, and high school at level-4.  
Because I will not add error term at level-1 because the level-1 error is omitted from the model because level one is only used to partition the multivariate outcomes’ structure into the model (Hox, 2010; Goldstein, 2010).  
Therefore, it will be actually 3-level multivariate model as shown below.  
Y = (Gamma1 + u11 + u21 + r1)\*dummy1 + (Gamma2 + u12 + u22 + r2)\*dummy2  
(Actually, the middle schools are not purely nested to high school thus it is cross-classified, but as I search in the forum, it would not require specific codes to set the characteristic. If I am wrong, please let me know.)

However, I am struggle with extending the multivariate model to multivariate multilevel model because I cannot find the example to make 3-level multilevel model with multiple responses…  
Could you give any your thoughts or suggestion about it? Or, is there any study or example I can follow to understand how the code can be set up to multivariate multilevel model?

Here is the code what I saw at the Manual (Chapter 9.15 Multivariate outcomes)

data {  
int\<lower=1\> K;  
int\<lower=1\> J;  
int\<lower=0\> N;  
vector[J] x[N];  
vector[K] y[N];  
}  
parameters {  
matrix[K, J] beta;  
cholesky\_factor\_corr[K] L\_Omega;  
vector\<lower=0\>[K] L\_sigma;  
}  
model {  
vector[K] mu[N];  
matrix[K, K] L\_Sigma;  
for (n in 1:N)  
mu[n] = beta \* x[n];  
L\_Sigma = diag\_pre\_multiply(L\_sigma, L\_Omega);  
to\_vector(beta) ~ normal(0, 5);  
L\_Omega ~ lkj\_corr\_cholesky(4);  
L\_sigma ~ cauchy(0, 2.5);  
y ~ multi\_normal\_cholesky(mu, L\_Sigma);  
}

Thank you for your time in advance.

---

<div class="post-metadata">

**Author:** ![sakrejda](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/sakrejda/32/845_2.png) [@sakrejda](https://discourse.mc-stan.org/u/sakrejda)\
**Post date:** [October 25, 2017, 2:24am UTC](https://discourse.mc-stan.org/t/modeling-multivariate-multilevel-model-in-stan/2302/2 "2017-10-25T02:24:59Z")

</div>

If you’re a beginner try to start with a univariate model. The majority of what’s hard about a multivariate hierarchical model is the level of bookkeeping required so you don’t want to be doing that at the same time as you construct the hierarchical piece.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [November 2, 2017, 9:34pm UTC](https://discourse.mc-stan.org/t/modeling-multivariate-multilevel-model-in-stan/2302/3 "2017-11-02T21:34:45Z")

</div>

> [@sunny](#):
>
> Or, is there any study or example I can follow to understand how the code can be set up to multivariate multilevel model?

It’s elephants all the way down. Really, it’s just more of the same. You just code the multiple levels up directly; each level can involve regression predictors, etc. The tricky part’s just keeping track of all the predictors and when to apply them.

---

<div class="post-metadata">

**Author:** ![blakeobeans](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/blakeobeans/32/3108_2.png) [@blakeobeans](https://discourse.mc-stan.org/u/blakeobeans)\
**Post date:** [June 1, 2020, 4:37am UTC](https://discourse.mc-stan.org/t/modeling-multivariate-multilevel-model-in-stan/2302/4 "2020-06-01T04:37:03Z")

</div>

> [@sunny](#):
>
> a {  
> int\<lower=1\> K;  
> int\<lower=1\> J;  
> int\<lower=0\> N;

Any examples of a univariate multi-level model?

---

<div class="post-metadata">

**Author:** ![anon79882417](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@anon79882417](https://discourse.mc-stan.org/u/anon79882417)\
**Post date:** [June 2, 2020, 5:06am UTC](https://discourse.mc-stan.org/t/modeling-multivariate-multilevel-model-in-stan/2302/5 "2020-06-02T05:06:26Z")

</div>

@blakeobeans

The documentation has an example of hierarchical logistic regression: [https://mc-stan.org/docs/2\_23/stan-users-guide/hierarchical-logistic-regression.html](https://mc-stan.org/docs/2_23/stan-users-guide/hierarchical-logistic-regression.html)

What’s also helpful is the case studies: [https://mc-stan.org/users/documentation/case-studies.html](https://mc-stan.org/users/documentation/case-studies.html)

In particular, this one may be helpful, for multilevel (hierarchical) modeling: [https://mc-stan.org/users/documentation/case-studies/radon.html](https://mc-stan.org/users/documentation/case-studies/radon.html)
