Model both response and between group correlations - multivariate brms

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))
2 Likes

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!

1 Like

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.

1 Like