# Modelling group-level variance of random slope/coefficient

**URL:** <https://discourse.mc-stan.org/t/modelling-group-level-variance-of-random-slope-coefficient/26027>\
**Category:** brms\
**Created:** [January 19, 2022, 11:32am UTC](https://discourse.mc-stan.org/t/modelling-group-level-variance-of-random-slope-coefficient/26027 "2022-01-19T11:32:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ewan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ewan/32/13473_2.png) [@ewan](https://discourse.mc-stan.org/u/ewan)\
**Post date:** [January 19, 2022, 11:32am UTC](https://discourse.mc-stan.org/t/modelling-group-level-variance-of-random-slope-coefficient/26027/1 "2022-01-19T11:32:52Z")

</div>

## Summary

- I’d like to model the group-level variance of a random coefficient as a function of other group-level variables.
- I know I can model `sigma` as a function of other variables. But cannot see how to model the random slope as well.

Adapting the `fit_rent2` example from [Bürkner (2018)](https://journal.r-project.org/archive/2018/RJ-2018-017/RJ-2018-017.pdf), suppose we have a model:

```no-highlight
library(brms)
data("rent99", package = "gamlss.data")

bform <- bf(rentsqm ~ area + (area | district),
            sigma ~ area + (1 | district))
m1 <- brm(bform,
          data = rent99,
          chains = 4, cores = 4)

```

What I’d like is something like:

```no-highlight
bform <- bf(rentsqm ~ area + (area | district),
            sigma ~ (1 | district),
---> sigma[area] ~ district) <---
m2 <- brm(bform,
          data = rent99,
          chains = 4, cores = 4)

```

`sigma` allows modelling of the group-level intercept. In theory, **`sigma[area]`** would allow modelling of the random slope for `area`.

Is anything like this possible?

## Motivation

I may be confusing things, so to explain the motivation:

1. We have a continuous outcome `y` that we relate to a binary covariate `x1`.
2. We allow a group-level intercept for `y` and a group-level coefficient for `x1`.
3. The effect of `x1` on `y` is larger for some individuals than others.
4. We want to consider what other variables explain this variation (e.g. `x2`).

So, the thinking was to model predictors of the group-level coefficient for `x1`, as suggested above (`sigma[x1] ~ x2`). A positive coefficient for for `sigma[x1]_x2` would mean that the effect of `x1` on `y` is larger for individuals with greater `x2`.

It’s entirely possible I’m missing something obvious, have the wrong approach and/or question. Any suggestions would be gratefully received.

* * *

- Operating System: Arch Linux x86\_64
- brms Version: 2.16.1

---

<div class="post-metadata">

**Author:** ![jd\_c](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jd_c/32/16759_2.png) [@jd\_c](https://discourse.mc-stan.org/u/jd_c)\
**Post date:** [January 20, 2022, 12:47am UTC](https://discourse.mc-stan.org/t/modelling-group-level-variance-of-random-slope-coefficient/26027/2 "2022-01-20T00:47:09Z")

</div>

It sounds like maybe you are asking something similar as on this [post](https://discourse.mc-stan.org/t/modeling-population-and-group-level-variance-separately/25858)?  
Maybe the suggestion of the LMMELSM package on that thread would help you?

> [@Modeling population and group-level variance separately](https://discourse.mc-stan.org/t/modeling-population-and-group-level-variance-separately/25858/10):
>
> Would the LMMELSM package help you? It can do mixed effects location scale models with between-group variance models. Note: I’m the package author of LMMELSM, so if you have any questions about it I can answer them.

---

<div class="post-metadata">

**Author:** ![ewan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ewan/32/13473_2.png) [@ewan](https://discourse.mc-stan.org/u/ewan)\
**Post date:** [January 21, 2022, 9:16am UTC](https://discourse.mc-stan.org/t/modelling-group-level-variance-of-random-slope-coefficient/26027/3 "2022-01-21T09:16:47Z")

</div>

Thanks for the reply.

Yes, I read that post with interest, because it seemed to have the answer. But from what I could tell, it talks about modelling the variance of the random intercept only. I don’t think (but would love to be wrong) it extends this to also model random slope variance.

I’ll have another read now.

---

<div class="post-metadata">

**Author:** ![Stephen\_Martin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stephen_martin/32/7879_2.png) [@Stephen\_Martin](https://discourse.mc-stan.org/u/Stephen_Martin)\
**Post date:** [January 22, 2022, 1:44am UTC](https://discourse.mc-stan.org/t/modelling-group-level-variance-of-random-slope-coefficient/26027/4 "2022-01-22T01:44:05Z")

</div>

Funny enough - once upon a time, LMMELSM did let you predict the random slope variance. However, this proved to cause enormous convergence problems, especially for the Latent factor models that LMMELSM was primarily created to fit. So I disabled the random slope variance modeling. _Afterward_, I realized it’d be easy to implement non-latent multivariate MELSMs as well, but never bothered to allow random slope variance modeling.

I think if you look at the stan files in lmmelsm, you may be able to just tweak it to allow random slope variance modeling as well.

---

<div class="post-metadata">

**Author:** ![ewan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ewan/32/13473_2.png) [@ewan](https://discourse.mc-stan.org/u/ewan)\
**Post date:** [January 23, 2022, 7:40pm UTC](https://discourse.mc-stan.org/t/modelling-group-level-variance-of-random-slope-coefficient/26027/5 "2022-01-23T19:40:58Z")

</div>

Brillliant, thanks for the pointer. I’ll take a look and see what’s possible.

---

<div class="post-metadata">

**Author:** ![jkalley](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jkalley/32/4247_2.png) [@jkalley](https://discourse.mc-stan.org/u/jkalley)\
**Post date:** [September 5, 2023, 12:41pm UTC](https://discourse.mc-stan.org/t/modelling-group-level-variance-of-random-slope-coefficient/26027/6 "2023-09-05T12:41:23Z")

</div>

Very late to this, but in case someone else runs across it, you can use other factors to modify the slope with: area\*(var1 + var2) + (area | district).

This implies that var1 and var2 change the slope of area in a systematic way, in addition to the random variation by district.

[See this helpful guide to lmer syntax for more.](https://bbolker.github.io/mixedmodels-misc/glmmFAQ.html#model-specification)
