# Multivariate formula with different number of observations

**URL:** <https://discourse.mc-stan.org/t/multivariate-formula-with-different-number-of-observations/16401>\
**Category:** brms\
**Tags:** cognitive-science\
**Created:** [July 5, 2020, 6:02pm UTC](https://discourse.mc-stan.org/t/multivariate-formula-with-different-number-of-observations/16401 "2020-07-05T18:02:28Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [August 4, 2020, 3:53pm UTC](https://discourse.mc-stan.org/t/multivariate-formula-with-different-number-of-observations/16401/21 "2020-08-04T15:53:30Z")

</div>

So you want to correlate the random effects per participant? This is easy by stacking and having a term of the form (0 + type | person) where type is either correct or error.

---

<div class="post-metadata">

**Author:** ![Peter\_Clayson](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/peter_clayson/32/251_2.png) [@Peter\_Clayson](https://discourse.mc-stan.org/u/Peter_Clayson)\
**Post date:** [August 4, 2020, 4:05pm UTC](https://discourse.mc-stan.org/t/multivariate-formula-with-different-number-of-observations/16401/22 "2020-08-04T16:05:41Z")

</div>

Just to make sure I’m not misunderstanding.

I can do…  
score ~ 0 + event + (0 + event | trial) + (0 + event | person)

(0 + event | trial): this gives me separate between-item variances for correct trials and error trials and the covariance

(0 + event | person): separate variance estimates for between-person variances for correct and error trials and the covariance

But, I only get a single residual variance estimate (sigma) for the whole model. I need separate residual variances for correct and error trial estimates and the covariance between sigmas. Am I messing up the brms formula somehow?

---

<div class="post-metadata">

**Author:** ![Peter\_Clayson](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/peter_clayson/32/251_2.png) [@Peter\_Clayson](https://discourse.mc-stan.org/u/Peter_Clayson)\
**Post date:** [August 4, 2020, 4:06pm UTC](https://discourse.mc-stan.org/t/multivariate-formula-with-different-number-of-observations/16401/23 "2020-08-04T16:06:07Z")

</div>

Also, thanks for the quick replies and for thinking this through with me :) I appreciate it!

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [August 4, 2020, 4:12pm UTC](https://discourse.mc-stan.org/t/multivariate-formula-with-different-number-of-observations/16401/24 "2020-08-04T16:12:25Z")

</div>

Use brms distributional syntax a la sigma ~ … for this purpose

---

<div class="post-metadata">

**Author:** ![Peter\_Clayson](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/peter_clayson/32/251_2.png) [@Peter\_Clayson](https://discourse.mc-stan.org/u/Peter_Clayson)\
**Post date:** [August 4, 2020, 5:14pm UTC](https://discourse.mc-stan.org/t/multivariate-formula-with-different-number-of-observations/16401/25 "2020-08-04T17:14:44Z")

</div>

Excellent!! You’ve changed my life :) Thank you so much for your help.

For posterity:

I now use…  
bf(score ~ 0 + event +  
(0 + event | trial) + (0 + event | id2),  
sigma ~ 0 + (0 + event | id2))

This gives me separate residual variances for each event and the covariance (cor(sigma\_eventcorrect, sigmaevent\_error).

Thanks again, @paul.buerkner!

---

<div class="post-metadata">

**Author:** ![Peter\_Clayson](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/peter_clayson/32/251_2.png) [@Peter\_Clayson](https://discourse.mc-stan.org/u/Peter_Clayson)\
**Post date:** [August 5, 2020, 2:02pm UTC](https://discourse.mc-stan.org/t/multivariate-formula-with-different-number-of-observations/16401/26 "2020-08-05T14:02:27Z")

</div>

Hi @paul.buerkner. Sorry, I’m back.

The sigmas (residual variance) for each event are remarkably small (~4) compared to the univariate brms model variances (~55) that are run separately on each scores from each event.

I ended up using…

`bf(score ~ 0 + event + (0 + event | trial) + (0 + event | id), sigma ~ 0 + event)`

I don’t think I needed `sigma ~ 0 + (0 + event | id)` like I wrote in my previous post, because I only need population-level estimates for residual (co)variance.

I don’t know if anything strikes you as incorrectly written in this formula. I’ll keep tinkering to see if I can figure it out. So close!

Thanks!

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [August 5, 2020, 2:04pm UTC](https://discourse.mc-stan.org/t/multivariate-formula-with-different-number-of-observations/16401/27 "2020-08-05T14:04:13Z")

</div>

This seems unlikely. For correct computation of the transformed mean, do the exponentiation per posterior draws and only summarized afterwards. If this does not solve the problem, there may be another issue but I fail to see what that might be right now.

---

<div class="post-metadata">

**Author:** ![Peter\_Clayson](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/peter_clayson/32/251_2.png) [@Peter\_Clayson](https://discourse.mc-stan.org/u/Peter_Clayson)\
**Post date:** [August 5, 2020, 2:06pm UTC](https://discourse.mc-stan.org/t/multivariate-formula-with-different-number-of-observations/16401/28 "2020-08-05T14:06:36Z")

</div>

Sorry. I knew that, but I summarized the model incorrectly in my previous post. Yes, the sigmas are estimated to be ~2, which exp(2) = 7.4.

---

<div class="post-metadata">

**Author:** ![Peter\_Clayson](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/peter_clayson/32/251_2.png) [@Peter\_Clayson](https://discourse.mc-stan.org/u/Peter_Clayson)\
**Post date:** [August 5, 2020, 2:10pm UTC](https://discourse.mc-stan.org/t/multivariate-formula-with-different-number-of-observations/16401/29 "2020-08-05T14:10:46Z")

</div>

> [@paul.buerkner](#):
>
> For correct computation of the transformed mean, do the exponentiation per posterior draws and only summarized afterwards.

Gotcha. Thanks again. I’ll look into it.

[Previous page](https://discourse.mc-stan.org/t/multivariate-formula-with-different-number-of-observations/16401.md?page=1)
