 # Formula notation of nested varying effects

In the documentation to `brmsformula` stands the following:

If levels of the grouping factor belong to different sub-populations, it may be reasonable to assume a different covariance matrix for each of the sub-populations. For instance, the variation within the treatment group and within the control group in a randomized control trial might differ. Suppose that y is the outcome, and x is the factor indicating the treatment and control group. Then, we could estimate different hyper-parameters of the varying effects (in this case a varying intercept) for treatment and control group via y ~ x + (1 | gr(subject, by = x)).

I am wondering whether `y ~ x + (1 | gr(subject, by = x))` is equivalent to `y ~ x + (1 | x/subject)`?

Welcome to the Stan Forums, @stfnrpplngr!

I don’t think `y ~ x + (1 | gr(subject, by = x))` is equivalent to `y ~ x + (1 | x/subject)`. For example, `y ~ x + (1 | x/subject)` also includes varying intercepts for `x`. This is not the case for `y ~ x + (1 | gr(subject, by = x))` (at least in my understanding; I haven’t needed it yet).

Some extra info (perhaps you already know that): For the nesting syntax (`y ~ x + (1 | x/subject)`), I think brms follows lme4. And this lme4 vignette says on page 7 that `(1 | g1/g2)` is equivalent to `(1 | g1) + (1 | g1:g2)`.