# Residual covariance (brms mutlivariate model in long format)

**URL:** <https://discourse.mc-stan.org/t/residual-covariance-brms-mutlivariate-model-in-long-format/17381>\
**Category:** brms\
**Created:** [August 11, 2020, 8:37pm UTC](https://discourse.mc-stan.org/t/residual-covariance-brms-mutlivariate-model-in-long-format/17381 "2020-08-11T20:37:23Z")\
**Posts on this page:** 13\
**Page:** 1

<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 11, 2020, 8:37pm UTC](https://discourse.mc-stan.org/t/residual-covariance-brms-mutlivariate-model-in-long-format/17381/1 "2020-08-11T20:37:23Z")

</div>

Hi Everyone,

I’ve received a lot of help for fitting models like this from others. e.g., see [here](https://discourse.mc-stan.org/t/multivariate-formula-with-different-number-of-observations/16401) .

I’m trying to fit a multivariate outcomes model in brms in long format. The wide-format equation looks like this.

```
bf(y_correct ~ 0 + (1 | p | subject)) + 
bf(y_error ~ 0 + (1 | p | subject)) +
set_rescor(TRUE)

```

If re-written in a long-format, so correct (y\_correct) and error trial scores (y\_error) are in the same column (y) with event type (correct, error) in a separate one, I end up with the following syntax.

```
bf(y ~ 0 + event + (0 + event | subject),
  sigma ~ 0 + event)

```

This gives me all of the estimates I need, except for covariances between the sigma for correct and sigma for error. (i.e., the same output cor in `summary()` thanks to `set_rescor(TRUE)` from the wide-format above).

Anyone have any recommendations?

`VarCorr()` does not include the sigmas.

I apologize if I missed something in a vignette somewhere… I’ve been working on this for a while now…

Thank you for you thoughts!

- Operating System: OS X 10.15.6
- brms Version: 2.13.5

---

<div class="post-metadata">

**Author:** ![Max\_Mantei](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/max_mantei/32/7912_2.png) [@Max\_Mantei](https://discourse.mc-stan.org/u/Max_Mantei)\
**Post date:** [August 18, 2020, 10:10am UTC](https://discourse.mc-stan.org/t/residual-covariance-brms-mutlivariate-model-in-long-format/17381/2 "2020-08-18T10:10:49Z")

</div>

Hey there!

Hm, yea, I think this would be a it awkward. I think the covariance is captured in the regression coefficient for `event`. IIRC, a regression coefficient is simply \beta\_\text{event}=\rho \frac{\sigma\_y}{\sigma\_\text{event}}, but it’s a bit too hot for me to work that through for the hierarchical part.

What I wanted to say is: I think the covariances between the sigma for correct and sigma for error is in your model, but a bit awkward to extract. Personally, I would got with the wide-format model if that’s possible.

All that being said, quite possible there _is_ a way to exactly do this. I’m not really a `brms` expert…

Cheers!  
Max

---

<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 18, 2020, 10:20am UTC](https://discourse.mc-stan.org/t/residual-covariance-brms-mutlivariate-model-in-long-format/17381/3 "2020-08-18T10:20:02Z")

</div>

Thanks for your thoughts @Max_Mantei. I typically use the wide format, when I can, because it’s more straightforward (in my head) to implement. For this particular dataset that I’m working on though, I have unbalanced data (not missing data, see discussion with @paul.buerkner and @jroon here: [Multivariate formula with different number of observations](https://discourse.mc-stan.org/t/multivariate-formula-with-different-number-of-observations/16401)).

I’ll keep thinking about it. Thanks again!

---

<div class="post-metadata">

**Author:** ![Max\_Mantei](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/max_mantei/32/7912_2.png) [@Max\_Mantei](https://discourse.mc-stan.org/u/Max_Mantei)\
**Post date:** [August 18, 2020, 10:33am UTC](https://discourse.mc-stan.org/t/residual-covariance-brms-mutlivariate-model-in-long-format/17381/4 "2020-08-18T10:33:30Z")

</div>

Just skimmed through the other thread. I think you are already almost doing the correct thing. Keep in mind:

> [@Multivariate formula with different number of observations](https://discourse.mc-stan.org/t/multivariate-formula-with-different-number-of-observations/16401/19):
>
> You can only define a correlation between two variables when there is a one to one correspondence between values. This is not a brms limitation but how correlations work. So i am unsure how you would define this residual correlation in the first place.

So in your model above the residual correlations would be captured by doing something like `y ~ 0 + event + (1 + event | subject)` adding an intercept to the subject level. this would give you a varying intercept per subject, which is common across event types (if this number is higher, you predictions for both events types is higher, if it is lower your predictions for both event types is lower → residual correlation). If you think about the “random/mixed effect” as composite error, this might become more obvious. There is a cool chapter in _Data Analysis Using Regression and Multilevel/Hierarchical Models_ (Gelman/Hill) – I think it is chapter 12.5 – that further describes this “view” of hierarchical models.

Hope this helps!  
Cheers,  
Max

---

<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 18, 2020, 10:46am UTC](https://discourse.mc-stan.org/t/residual-covariance-brms-mutlivariate-model-in-long-format/17381/5 "2020-08-18T10:46:59Z")

</div>

Hm, interesting. I don’t readily see how that would give me the sigma covariances between the two events, but that’s probably just due to my missing something. I’ll give the approach a shot. I’ll also take a look at that chapter in the Gelman/Hill book. I’ll report back when I’ve had a chance to do so :)

Thanks again,  
Peter

---

<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 19, 2020, 2:53pm UTC](https://discourse.mc-stan.org/t/residual-covariance-brms-mutlivariate-model-in-long-format/17381/6 "2020-08-19T14:53:34Z")

</div>

Thanks, @Max_Mantei.

I read the section of Gelman and Hill’s book that you referred to. A great read. Thanks for the recommendation. The section _Large regression with correlated errors_ is what I’m trying to do, but I can’t wrap my head around how to get the covariances for the residual using the long-format equation in brms.

I am interested in explicitly estimating the covariance for my two population-level sigmas (sigma\_eventcorrect, sigma\_eventerror). Adding the subject-level intercept didn’t help me estimate the covariance. Am I missing something?

---

<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 20, 2020, 6:26pm UTC](https://discourse.mc-stan.org/t/residual-covariance-brms-mutlivariate-model-in-long-format/17381/7 "2020-08-20T18:26:37Z")

</div>

> [@Peter\_Clayson](#):
>
> If re-written in a long-format, so correct (y\_correct) and error trial scores (y\_error) are in the same column (y) with event type (correct, error) in a separate one, I end up with the following syntax.
> 
> ```no-highlight
> bf(y ~ 0 + event + (0 + event | subject),
> sigma ~ 0 + event)
> 
> ```

@paul.buerkner. Sorry to ping you. Do you know if it’s possible to get the covariances of the two levels of “event” in `sigma ~ 0 + event` when I write this equation in long format? You helped me get this far already, so I thought it wouldn’t hurt to ask :)

thanks in advance if you have any advice!

---

<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 24, 2020, 7:23am UTC](https://discourse.mc-stan.org/t/residual-covariance-brms-mutlivariate-model-in-long-format/17381/8 "2020-08-24T07:23:52Z")

</div>

Can you clarify, which covariances (on which level of the model) you mean?

---

<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 24, 2020, 10:49am UTC](https://discourse.mc-stan.org/t/residual-covariance-brms-mutlivariate-model-in-long-format/17381/9 "2020-08-24T10:49:02Z")

</div>

Absolutely. Here is the formula I am working with.

There are only two events: correct and error.

`bf(y ~ 0 + event + (0 + event | subject), sigma ~ 0 + event)`

This formula gives me the group-level effects (~subject) variances for the intercepts for each event ( for correct and for error) as well as the covariance between the two events by just using `VarCorr()`.

This formula also gives me the population-level effects for two residual variances (sigmas) separately for each event: one for correct and one for error.

However, I also need the covariance between the event:correct sigma and event:error sigma. In other words, _I need the covariances between the two residuals_. In the wide format of the equation, the residual correlations (`rescor(correct,error)`) is shown in the summary of the brm fit, and the covariance between sigma\_correct and sigma\_error can be extracted using `VarCorr()`.

I hope that clarifies what I’m looking for. Please let me know if you have any questions.

I appreciate you taking the time to help me this! Thank you! (and thanks for brms)

---

<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 24, 2020, 12:28pm UTC](https://discourse.mc-stan.org/t/residual-covariance-brms-mutlivariate-model-in-long-format/17381/10 "2020-08-24T12:28:56Z")

</div>

To achieve that, you could cheat a little and add a grouped autoregressive term to the main the formula. It would look something like `ar(gr = <group>, cov = TRUE)`, where `<group>` is a grouping variable that has the same value for both observations you want to model as correlated (and different values across different pairs of course).

---

<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 24, 2020, 1:22pm UTC](https://discourse.mc-stan.org/t/residual-covariance-brms-mutlivariate-model-in-long-format/17381/11 "2020-08-24T13:22:47Z")

</div>

Thanks for your help.

> [@paul.buerkner](#):
>
> where `<group>` is a grouping variable that has the same value for both observations you want to model as correlated (and different values across different pairs of course).

I’ve been thinking about this a bit. I’m trying to wrap my head on around how to code `<group>`.

I imagine that if I had 20 observations per event (correct, error) within a subject, then I would simply code the first pair of observations as 1, the second pair as 2, and so on. I am guessing that I would do this nested within subjects, starting over at 1 for each subject’s first pair.

How do I deal with unbalanced observations? I’m looking at responses based on participant behavior, so typically people have (many) more correct observations than error observations. This means I do not necessarily have a “matched” observation for each correct response, so I cannot pair all of the data up.

Thanks again for your continued help! I’m going to do some more reading on `ar()` as well, because it’s new to me.

---

<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 24, 2020, 1:24pm UTC](https://discourse.mc-stan.org/t/residual-covariance-brms-mutlivariate-model-in-long-format/17381/12 "2020-08-24T13:24:33Z")

</div>

I think I have explained that earlier but I forgot that this was the situation you are in. Residuals only make sense if you have two observations which **natrually** pair with one another. Since you don’t seem to have that residuals on the observational model do not seem sensible for your model.

---

<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 24, 2020, 1:35pm UTC](https://discourse.mc-stan.org/t/residual-covariance-brms-mutlivariate-model-in-long-format/17381/13 "2020-08-24T13:35:59Z")

</div>

> [@paul.buerkner](#):
>
> Since you don’t seem to have that residuals on the observational model do not seem sensible for your model.

Understood. I guess I will throw in the towel. Thanks for all of your help.
