# Model both response and between group correlations - multivariate brms

**URL:** <https://discourse.mc-stan.org/t/model-both-response-and-between-group-correlations-multivariate-brms/19600>\
**Category:** brms\
**Created:** [December 2, 2020, 6:31pm UTC](https://discourse.mc-stan.org/t/model-both-response-and-between-group-correlations-multivariate-brms/19600 "2020-12-02T18:31:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonnations](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@jonnations](https://discourse.mc-stan.org/u/jonnations)\
**Post date:** [December 2, 2020, 6:31pm UTC](https://discourse.mc-stan.org/t/model-both-response-and-between-group-correlations-multivariate-brms/19600/1 "2020-12-02T18:31:07Z")

</div>

Hi,  
I am setting up a multivariate model in brms. There are two response variables, `y1` and `y2`, and three groups `g1, g2, g3` in the data (fake data code at the bottom of the topic). I would like to model the `rescor(y1, y2)` and also model the correlation between every combination of response variables between groups, such as same response different groups: `cor(y1_groupg1, y1_groupg2)`, and different response, same group: `cor(y1_groupg1, y2_groupg1)` etc. I can’t figure out a direct way to do this, but I found a work-around that produces the outputs that I want. I created a dummy variable `dum_v` with only one level `Q` and set up the model like this:

```
m1 <- brm(data = toy_dat,
          family = student,
          formula = bf( y1 ~ 0 + (0 + group | i | dum_v)) + 
                    bf( y2 ~ 0 + (0 + group | i | dum_v)) +
                    set_rescor(TRUE))

```

This method appears to be working, but I may be making some big mistake that I am not aware of. Before I spend more time on this I definitely want to check with the experts to see if this is ok, or if there is a more direct way of producing the same outputs.

Thank you in advance!

- Operating System: OSX
- brms Version: 2.14

Toy data

```
set.seed(6)
n = 50
toy_dat =
  tibble(
    group = rep(c("g1", "g2", "g3"), times = c(30, 15, 5)),
    y1 = rnorm(50, 
              c(0.3, 0.3, 2, 2, 1, 1), 
              c(0.1, 0.3, 0.6, 0.2, 0.3, 0.3)),
    y2 = rnorm(50, 0.5 * exp(y1), 1),
   dum_v = rep("Q", 50))

```

---

<div class="post-metadata">

**Author:** ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)\
**Post date:** [December 7, 2020, 8:50pm UTC](https://discourse.mc-stan.org/t/model-both-response-and-between-group-correlations-multivariate-brms/19600/2 "2020-12-07T20:50:01Z")

</div>

Wow, that’s a clever hack. Inspecting the generated Stan code (via `make_stancode`) looks like you really are modelling just one big correlation matrix for the groups, so yeah, this seems to be (without me digging deeply) what you are after. You are obviously paying a bit of a penalty for fitting the `sd` parameters which you probably don’t care about and are not really informed by data, but hey, why not.

The best way to check is always to create a simulated dataset (prerably multiple different ones) where you know the value of the correlations and all other model parameters (because you created it) and see if you can recover them. So I would recommend you try that.

Best of luck!

---

<div class="post-metadata">

**Author:** ![jonnations](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@jonnations](https://discourse.mc-stan.org/u/jonnations)\
**Post date:** [December 7, 2020, 10:03pm UTC](https://discourse.mc-stan.org/t/model-both-response-and-between-group-correlations-multivariate-brms/19600/3 "2020-12-07T22:03:28Z")

</div>

Thanks @martinmodrak for looking through the Stan code! It is good to know that it appears to be modeling what I thought it was modeling. I’ll work on the simulated data, good suggestion.
