# Calculating Group-level Correlations from Posterior Draws

I have fitted a model in brms as:

``````fmla <- bf(y ~ 0 + Intercept +
Condition +
(1 + Condition|Group/ID)
``````

where Condition has 9 levels and Group has 3 levels.

I would like to calculate the correlation between Conditions for each level of group.

Using `get_variables()` from `tidybayes`, I see parameters in the form:

``````cor_Group__Intercept__Condition2
cor_Group__Condition2__Condition3
cor_Group__Intercept__Condition3
cor_Group__Condition3__Condition4
...

r_Group[1,Intercept]
r_Group[2,Intercept]
r_Group[3,Intercept]
r_Group[1,Condition2]
r_Group[2,Condition2]
r_Group[3,Condition2]
r_Group[1,Condition3]
r_Group[2,Condition3]
r_Group[3,Condition3]
...

``````

Can I calculate per-Group correlations from these estimates?

Or do I need to specify the model differently to get a direct estimate correlations between conditions for each level of Group?

Perhaps as:

``````fmla <- bf(y ~ 0 + Intercept +
Condition +
(1 + Condition:Group|ID)

``````

?

I think you can use (1+ Condition | gr(ID , by = Group)) (gr: Set up basic grouping terms in 'brms' in brms: Bayesian Regression Models using 'Stan') to define your random effect structure. This will specify separate standard deviations and correlation matrices for each group.

Thank you!

`by = Group` was not the right fit for my actual data, but your suggestion took me to the multi-membership implementation in `brms`.

In my actual data I have time as a factor and `ID` can be a member of a different `Group` at different points in time. So, I tried:

``````fmla <- bf(y ~ 0 + Intercept +
Group +
(1 + Group|mm(SubjID, Group, Subgroup)) +
(1|year))
``````

But, I still donâ€™t see a correlation parameter indexed on `Group`
I get parameters identified as:

``````cor_mmIDGroupSubgroup__Intercept__Cond2

...

r_mmIDGroupSubgroup[Subgroup1,Cond2]
...

r_mmIDGroupSubgroup[Group1,Cond1]
``````

Do I add the random effects together with the `cor_` coeficients to get per-Group estimates?

``````cor_mmIDGroupSubgroup__Intercept__Cond2 + r_mmIDGroupSubgroup[Subgroup1,Cond2] + r_mmIDGroupSubgroup[Group1,Cond1]

``````

Is the above how I calculate the correlation between the Intercept (Cond1) and Condition 2 for Subgroup 1 of Group 1?