# Test correlation between random slopes

**URL:** <https://discourse.mc-stan.org/t/test-correlation-between-random-slopes/21277>\
**Category:** Modeling\
**Tags:** specification, brms\
**Created:** [March 13, 2021, 1:21pm UTC](https://discourse.mc-stan.org/t/test-correlation-between-random-slopes/21277 "2021-03-13T13:21:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jugross](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jugross](https://discourse.mc-stan.org/u/jugross)\
**Post date:** [March 13, 2021, 1:21pm UTC](https://discourse.mc-stan.org/t/test-correlation-between-random-slopes/21277/1 "2021-03-13T13:21:14Z")

</div>

* * *

Hi everyone,  
and thanks in advance for your help!

I would like to test the correlation between random slopes by running a model comparison.

**Model**

> model\<-brm(Y ~ Phase + (1+Phase|subject) + (1|item),  
> data=Con, sample\_prior = TRUE, save\_all\_pars = TRUE, iter=10000)

- Y is a continous variable
- Phase is a categorical variable (factor) with three levels (0, 1, 2)

With the default contrast coding, I get estimates for the following fixed effects:

- Intercept (estimate at Phase = 0)
- Slope 1 (difference between 0 and 1)
- Slope 2 (difference between 0 and 2).

I also get estimates for the following random effects

- Correlation between random intercepts and random slopes 1
- Correlation between random intercepts and random slopes 2
- Correlation between random slopes 1 and random slopes 2

**My question**

I would like to test the hypothesis that the correlation between random slopes 1 and random slopes 2 is different from 0. I’d like to run a model comparison (e.g., by using bayes\_factor), in which I compare a model with all three correlations to a model where the third correlation is set to 0.

Is that possible?  
From the brms documentation I learned that it is possible to set parameters to constants in the prior specification. Is that also possible for (parts of the) variance-covariance matrix?

Thank you!  
Julia

* * *

---

<div class="post-metadata">

**Author:** ![mike-lawrence](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mike-lawrence/32/59_2.png) [@mike-lawrence](https://discourse.mc-stan.org/u/mike-lawrence)\
**Post date:** [March 15, 2021, 11:34am UTC](https://discourse.mc-stan.org/t/test-correlation-between-random-slopes/21277/2 "2021-03-15T11:34:40Z")

</div>

> [@jugross](#):
>
> From the brms documentation I learned that it is possible to set parameters to constants in the prior specification. Is that also possible for (parts of the) variance-covariance matrix?

@paul.buerkner might chime in to correct me, but I suspect this level of granularity is going to be difficult to achieve, but…

> [@jugross](#):
>
> I would like to test the hypothesis that the correlation between random slopes 1 and random slopes 2 is different from 0.

If you’re willing to adopt an estimation framework (rather than an “evidence” framework embodied by BFs), then you get this comparison for free from the single full model; you can just compute the difference between the two correlations in each sample from the posterior, yielding a difference posterior you can visualize and summarize with an eye to how much mass is near zero.

> [@jugross](#):
>
> a model with all three correlations to a model where the third correlation is set to 0.

Could you elaborate on what inference you are seeking to achieve with this comparison? If merely about whether the third correlation has much posterior credibility away from zero, again, this comes for free with a single model in the estimation framework.

---

<div class="post-metadata">

**Author:** ![jugross](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jugross](https://discourse.mc-stan.org/u/jugross)\
**Post date:** [March 15, 2021, 12:27pm UTC](https://discourse.mc-stan.org/t/test-correlation-between-random-slopes/21277/3 "2021-03-15T12:27:52Z")

</div>

@mike-lawrence Thank you for your suggestions! I actually do want to quantify the evidence in favor of the correlation in terms of a BF (although, of course, looking at the posterior distributions is already extremely informative!). Also, I am not sure if I missed something here, but I don’t think the difference between the posterior distributions for correlation1 - correlation2 would tell me something about correlation2? These correlations are conceptually very different.

---

<div class="post-metadata">

**Author:** ![mike-lawrence](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mike-lawrence/32/59_2.png) [@mike-lawrence](https://discourse.mc-stan.org/u/mike-lawrence)\
**Post date:** [March 15, 2021, 2:39pm UTC](https://discourse.mc-stan.org/t/test-correlation-between-random-slopes/21277/4 "2021-03-15T14:39:32Z")

</div>

> [@jugross](#):
>
> Also, I am not sure if I missed something here, but I don’t think the difference between the posterior distributions for correlation1 - correlation2 would tell me something about correlation2? These correlations are conceptually very different.

Ah, sorry, my error. I was misreading and no difference is required to evaluate whether zero remains a credible value for the correlations between slope 1 and slope 2.

---

<div class="post-metadata">

**Author:** ![mike-lawrence](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mike-lawrence/32/59_2.png) [@mike-lawrence](https://discourse.mc-stan.org/u/mike-lawrence)\
**Post date:** [March 15, 2021, 2:42pm UTC](https://discourse.mc-stan.org/t/test-correlation-between-random-slopes/21277/5 "2021-03-15T14:42:15Z")

</div>

Bah, another error, this time in expression, so here’s a more explicit expression:

If you are interested in whether zero remains a credible value for the correlation between the intercept and slope1, just look at the posterior.

If you are interested in whether zero remains a credible value for the correlation between the intercept and slope2, just look at the posterior.

If you are interested in whether zero remains a credible value for the _difference_ between [correlation between the intercept and slope1] and [correlation between the intercept and slope2], compute the difference in each sample of the posterior and look at the difference posterior.

---

<div class="post-metadata">

**Author:** ![jugross](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jugross](https://discourse.mc-stan.org/u/jugross)\
**Post date:** [March 15, 2021, 2:54pm UTC](https://discourse.mc-stan.org/t/test-correlation-between-random-slopes/21277/6 "2021-03-15T14:54:33Z")

</div>

Ok, thanks for clarifying this! I am interested a different question, namely testing the presence of the correlation between slope1 and slope2 by comparing a model with that correlation to a model without that correlation (i.e., correlation set to a constant = 0). The two other other correlations, intercept - slope1, intercept - slope 2, should be part of both models.

---

<div class="post-metadata">

**Author:** ![mike-lawrence](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mike-lawrence/32/59_2.png) [@mike-lawrence](https://discourse.mc-stan.org/u/mike-lawrence)\
**Post date:** [March 15, 2021, 3:14pm UTC](https://discourse.mc-stan.org/t/test-correlation-between-random-slopes/21277/7 "2021-03-15T15:14:29Z")

</div>

Ah, ok, well the full model with all the correlations in play would have a posterior on the correlation between slope1 and slope2.

---

<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 2, 2021, 8:53am UTC](https://discourse.mc-stan.org/t/test-correlation-between-random-slopes/21277/8 "2021-04-02T08:53:11Z")

</div>

You cannot set parts of the correlation matrix to zero currently.  
However, If you want to run a Bayes factor analysis, you can also use the `hypothesis()` function (see the Evid.Ratio column for the BF) which applies Savage-Dickey Ratio rather than bridgesampling as `bayes_factor()` does.

So, you check out the name of the correlation parameter of interest via `parnames(model)`. Then you write `hypothesis(model, "<cor> = 0", class = "")`, where `<cor>` is the name of the relevant correlation parameter.

---

<div class="post-metadata">

**Author:** ![jugross](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jugross](https://discourse.mc-stan.org/u/jugross)\
**Post date:** [April 3, 2021, 8:13am UTC](https://discourse.mc-stan.org/t/test-correlation-between-random-slopes/21277/9 "2021-04-03T08:13:18Z")

</div>

Thanks! The Savage-Dickey method is indeed an alternative. I had hoped for a possibility to use bridge sampling (because I decided to use bridge sampling for all my other hypothesis tests, and I think it’s good to be consistent with regard to hypothesis testing). @paul.buerkner Are there any plans to work on this possibility? Again, thanks a lot!

---

<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 3, 2021, 8:17am UTC](https://discourse.mc-stan.org/t/test-correlation-between-random-slopes/21277/10 "2021-04-03T08:17:40Z")

</div>

no plans in the nearer future unfortunately. :-(
