# Help in designing the best model for my data

Hi Everyone,

I’m having some hard time in designing the hierarchical model to fit the task I would to achieve, and I’m looking for some help/discussion.

I have a simple regression that I want to perform y ~ beta*x and I’m interested in the beta. Very easy. In the table I have, x and y belong to different categories, so that they look something like:

``````| y | x | cat 1 | cat 2 | cat 3 |
| ----- | ----- | ------ | ------ | ----- |
| 11 | 1 | A1 | B1 | C3 |
| 12 | 2 | A1 | B2 | C3 |
| 13 | 3 | A1 | B3 | C2 |
| 13 | 3 | A1 | B3 | C2 |
| 110 | 11 | A2 | B2 | C1 |
| 210 | 12 | A2 | B2 | C1 |
| 310 | 13 | A2 | B3 | C3 |
| 310 | 13 | A2 | B3 | C3 |
| 1 | -5 | A3 | B1 | C2 |
| 2 | -4 | A3 | B1 | C2 |
| 3 | -3 | A3 | B3 | C3 |
``````

I have reason to believe that the beta described in the different groups refers to the same latent variable, so I was interested in a hierarchical model.

Should the model look like (after categorical factorisation):

``````for (entry_i in 1:cat1 ){
for (entry_j in 1:cat2) {
for(entry_z in 1:cat3){
y~beta*x
}}}
``````

I can’t figure out how it should be represented

Howdy!
I’m not sure I quite understand what you are trying to do, but it sounds like you may be looking for a hierarchical model with nested group-level effects. In the notation of the `brms` package, this might look something like:

``````brm(y ~ 1 + x + (x | cat1/cat2/cat3), data=data, family=...)
``````

This would give you varying intercepts and slopes for each level of nested groups. See https://cran.r-project.org/web/packages/brms/vignettes/brms_multilevel.pdf for an explanation of what the syntax means: “If, for instance, you write (1 | g1/g2), it will be expanded to (1 | g1) + (1 | g1:g2).” If you want the model written in Stan, then you can use the `make_stancode()` function to see the Stan code written for the above model, to give you some idea of how to write it:

``````make_stancode(y ~ 1 + x + (x | cat1/cat2/cat3), data=data, family=...)
``````

I hope that helps!

2 Likes