# BRMS: Priors in multi-variate hierarchical models

**URL:** <https://discourse.mc-stan.org/t/brms-priors-in-multi-variate-hierarchical-models/36981>\
**Category:** Modeling\
**Tags:** priors, specification, brms\
**Created:** [October 19, 2024, 1:26pm UTC](https://discourse.mc-stan.org/t/brms-priors-in-multi-variate-hierarchical-models/36981 "2024-10-19T13:26:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alessiolo](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Alessiolo](https://discourse.mc-stan.org/u/Alessiolo)\
**Post date:** [October 19, 2024, 1:26pm UTC](https://discourse.mc-stan.org/t/brms-priors-in-multi-variate-hierarchical-models/36981/1 "2024-10-19T13:26:58Z")

</div>

Heya,  
I am trying to fit a multivariate (multiple-outcome) hierarchical model in brms.  
The data and most of the code was taken from:

[https://cran.r-project.org/web/packages/brms/vignettes/brms\_multivariate.html#more-complex-multivariate-models](https://cran.r-project.org/web/packages/brms/vignettes/brms_multivariate.html#more-complex-multivariate-models)

I just created random binary data for y1, y2. Wish to test this model before using my data.

I am having a hard time figuring out how to set priors - apparently something is wrong with the correspondence between prior and variables. I have checked out get\_priors(), however the error persists even when including all possible “class”, “coef” and “group” arguments.

ERROR:  
The following priors do not correspond to any model parameter:  
sd ~ normal(0, 1)  
b\_hatchdate ~ normal(0, 1.5)  
b\_good\_food\_share ~ normal(0, 1.5)  
b\_sexMale ~ normal(0, 1.5)  
b\_sexUNK ~ normal(0, 1.5)  
sd\_fosternest\_\_good\_food\_share ~ exponential(1)

CODE:

```r
priors <- c(
  # Priors for fixed effects (intercept and slopes)
  prior_string("normal(0, 1)", class = "Intercept"),
  prior_string("normal(0, 1)", class = "sd"),
  
  prior_string("normal(0, 1.5)", class = "b",coef="hatchdate"),#numeric
  prior_string("normal(0, 1.5)", class = "b",coef="good_food_share"),#numeric
  prior_string("normal(0, 1.5)", class = "b",coef="sexMale"),#factor
  prior_string("normal(0, 1.5)", class = "b",coef="sexUNK"),#factor
  
  
  # Priors for random effects (variance of random intercepts and slopes)
  prior_string("exponential(1)", class = "sd", group = "fosternest"), # Random intercept
  prior_string("exponential(1)", class = "sd", group = "fosternest", coef = "good_food_share"), # Random slope for good_food_share
  
  # Prior for correlation between random intercept and random slope
  prior_string("lkj(2)", class = "L", group = "fosternest")
)

model = brm(mvbind(y1,y2)~sex+hatchdate+good_food_share+(1+good_food_share|p|fosternest), 
            data = data, 
            family = bernoulli(link = "logit"),
            prior = priors,
            cores=getOption("mc.cores", 4), 
            iter=1000)

```

Checking out this paper by @paul.bruecker ([https://arxiv.org/pdf/1905.09501](https://arxiv.org/pdf/1905.09501))  
something like this:

```r
prior = set_prior("normal(0, 3)", class = "b") +
set_prior("normal(2, 3)", class = "b", coef = "item1")

```

should be possible.

Would really appreciate a pointer to solve this!  
Thanks

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [November 7, 2024, 4:54pm UTC](https://discourse.mc-stan.org/t/brms-priors-in-multi-variate-hierarchical-models/36981/2 "2024-11-07T16:54:57Z")

</div>

Hi, @Alessiolo and welcome to the Stan forums. Sorry that nobody answered this yet, despite giving us all the info we needed. I’m afraid I don’t know `brms`, so I can’t even decode what `good_food_share|p|fosternest` means, even after looking at @paul.buerkner’s vignette (I’ve never learned brms and Paul’s vignette assumes you already know the expression syntax).

My general advice is to start with the simplest thing that works, then add one thing at a time to see where things go wrong—it’s usually a lot easier than trying to write your final product and getting a lot of interacting errors.

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [November 7, 2024, 5:41pm UTC](https://discourse.mc-stan.org/t/brms-priors-in-multi-variate-hierarchical-models/36981/3 "2024-11-07T17:41:03Z")

</div>

You need to replace terms like

```r
prior_string("normal(0, 1.5)", class = "b",coef="hatchdate")

```

with terms like

```r
prior_string("normal(0, 1.5)", class = "b",coef="hatchdate", resp = "y1") # (or resp = "y2")

```

In the multivariate response setting, you need to specify which element of the response the prior is meant to apply to.

---

<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:** [November 8, 2024, 1:48pm UTC](https://discourse.mc-stan.org/t/brms-priors-in-multi-variate-hierarchical-models/36981/4 "2024-11-08T13:48:46Z")

</div>

I cannot see the OP currently as I cannot access the discourse forums right now. The syntax `good_food_share|p|fosternest`, specifically, the inner |p| is described in more detail in my second brms paper: [https://arxiv.org/abs/1705.11123](https://arxiv.org/abs/1705.11123)

As for the rest, I have to wait until I can access the forums again.

---

<div class="post-metadata">

**Author:** ![Alessiolo](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Alessiolo](https://discourse.mc-stan.org/u/Alessiolo)\
**Post date:** [November 17, 2024, 11:39am UTC](https://discourse.mc-stan.org/t/brms-priors-in-multi-variate-hierarchical-models/36981/5 "2024-11-17T11:39:41Z")

</div>

Thank you all for your replies!

I have marked the correct solution from @jsocolar - for multivariate models its indeed necessary to define both response variable as well as class and coefficient.

Ive been working on a research project using multivariate correlated binary responses, which is somewhat different from @paul.buerkner s example mentioned above, mainly due to the GLM.

Residual correlations are not a thing here and for some reason I get no “significant” posterior correlations on my intercepts, although responses are univariate correlated.

Trying to wrap my head around this class of models and experimenting a lot - really interesting stuff!

Thank you for the responses, might make the code for my publication open access and will then link my workflow here.
