# Fitting more than two levels in a multilevel model using rstanarm

**URL:** <https://discourse.mc-stan.org/t/fitting-more-than-two-levels-in-a-multilevel-model-using-rstanarm/17608>\
**Category:** Modeling\
**Tags:** rstanarm\
**Created:** [August 22, 2020, 11:32pm UTC](https://discourse.mc-stan.org/t/fitting-more-than-two-levels-in-a-multilevel-model-using-rstanarm/17608 "2020-08-22T23:32:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vitaaquaticus](https://avatars.discourse-cdn.com/v4/letter/v/ebca7d/32.png) [@vitaaquaticus](https://discourse.mc-stan.org/u/vitaaquaticus)\
**Post date:** [August 22, 2020, 11:32pm UTC](https://discourse.mc-stan.org/t/fitting-more-than-two-levels-in-a-multilevel-model-using-rstanarm/17608/1 "2020-08-22T23:32:57Z")

</div>

It might be a simple question but I cannot seem to see examples anywhere on how to fit more than two levels for a multilevel (or hierarchical) model using rstanarm. I am learning how to do Bayesian analyses. and I have data I would like to run a Bayesian analysis on and my data has four levels. I saw examples that could fit two levels like this below:

```
TwoLevelModel <- stan_lmer(y ~ x + (1 + x | variable), data = dat)

```

If I want to fit four levels, how would I write a model using rstanarm? My four levels are: Quadrat nested into Transect nested into Site nested into Cape. Thank you!

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [August 22, 2020, 11:36pm UTC](https://discourse.mc-stan.org/t/fitting-more-than-two-levels-in-a-multilevel-model-using-rstanarm/17608/2 "2020-08-22T23:36:32Z")

</div>

It is the same as with lme4. Table 2 of

> **[lmer.pdf](https://cran.r-project.org/web/packages/lme4/vignettes/lmer.pdf)**
>
> 483.59 KB

implies that it should involve `| Quadrat / Transect / Site / Cape`
