# Including two matrices to describe the covariance structure of the group-level effects with brm()

**URL:** <https://discourse.mc-stan.org/t/including-two-matrices-to-describe-the-covariance-structure-of-the-group-level-effects-with-brm/8322>\
**Category:** brms\
**Created:** [April 1, 2019, 10:32pm UTC](https://discourse.mc-stan.org/t/including-two-matrices-to-describe-the-covariance-structure-of-the-group-level-effects-with-brm/8322 "2019-04-01T22:32:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AngelaMaria](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@AngelaMaria](https://discourse.mc-stan.org/u/AngelaMaria)\
**Post date:** [April 1, 2019, 10:32pm UTC](https://discourse.mc-stan.org/t/including-two-matrices-to-describe-the-covariance-structure-of-the-group-level-effects-with-brm/8322/1 "2019-04-01T22:32:12Z")

</div>

Please also provide the following information in addition to your question:

- Operating System: macOS Mojave v 10.14.2
- brms Version: 2.8.0

Hello all,

I just wanted to ask about including two matrices to describe the covariance structure of the group-level effects using brm(). Essentially I have a model with a binary y variable and several predictors, and I want to account for both phylogenetic and spatial dependence. I used brm(), adding the two matrices in the cov\_ranef argument. From what I understand, in the current implementation, the model assumes both these matrices have equal contribution - is this correct(?) or is it possible for the model to return the variance parameters associated with each matrix separately?

I am adapting this from the lmekin() function in coxme(), which unfortunately only deals with continuous response variables. But in there, the variance structure is fitted as V=s1A + s2B, where A and B are two matrices given as input, and the variances s1 and s2 are parameters that lmekin() is optimizing and returning.

Many thanks!

Example dummy code:  
set.seed(123)  
inv\_logit \<- function(x) 1 / (1 + exp(-x))  
ability \<- rnorm(100)  
p \<- 0.33 + 0.67 \* inv\_logit(ability)  
answer \<- ifelse(runif(100, 0, 1) \< p, 1, 0)  
dat\_ir \<- data.frame(ability, answer)  
dat\_ir$species\<-as.character(1:100)

#create two covariance matrices  
n \<- 100  
A \<- matrix(runif(n^2)_2-1, ncol=n)  
Sigma \<- t(A) %_% A  
rownames(Sigma)\<-colnames(Sigma)\<- dat\_ir$species

n \<- 100  
A \<- matrix(runif(n^2)_2-1, ncol=n)  
Sigma2 \<- t(A) %_% A  
rownames(Sigma2)\<-colnames(Sigma2)\<- dat\_ir$species

model \<- brm( answer ~ ability + (1|species), data = dat\_ir, family = bernoulli(), cov\_ranef = list(species = Sigma, species=Sigma2))

---

<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:** [April 5, 2019, 3:03pm UTC](https://discourse.mc-stan.org/t/including-two-matrices-to-describe-the-covariance-structure-of-the-group-level-effects-with-brm/8322/2 "2019-04-05T15:03:25Z")

</div>

Currently, brms does not support two matrices for the same grouping factor. I am not sure what brms does in this case, but I would think it just takes the first one (because I didn’t expect users to ever pass two matrices for the same grouping factor).

What would the behavior be that you hoped for? would it be the lmekin behavior?

---

<div class="post-metadata">

**Author:** ![AngelaMaria](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@AngelaMaria](https://discourse.mc-stan.org/u/AngelaMaria)\
**Post date:** [April 5, 2019, 3:36pm UTC](https://discourse.mc-stan.org/t/including-two-matrices-to-describe-the-covariance-structure-of-the-group-level-effects-with-brm/8322/3 "2019-04-05T15:36:41Z")

</div>

Thank you for your reply!

Yes, essentially I was hoping brms would provide an alternative to lmekin(), as lmekin() cannot deal with binomial data. I am not interested really in the values for the two variances s1 and s2, but I would like to be able to account for two sources of dependence.

---

<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:** [April 5, 2019, 3:52pm UTC](https://discourse.mc-stan.org/t/including-two-matrices-to-describe-the-covariance-structure-of-the-group-level-effects-with-brm/8322/4 "2019-04-05T15:52:32Z")

</div>

I see. I will think of a good way to implement this in brms. In the meantime, would you mind opening an issue on github so that I don’t forget about this feature?

---

<div class="post-metadata">

**Author:** ![AngelaMaria](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@AngelaMaria](https://discourse.mc-stan.org/u/AngelaMaria)\
**Post date:** [April 5, 2019, 3:57pm UTC](https://discourse.mc-stan.org/t/including-two-matrices-to-describe-the-covariance-structure-of-the-group-level-effects-with-brm/8322/5 "2019-04-05T15:57:17Z")

</div>

Sure, will do. That would be fantastic, thank you!
