Help with brms non-linear syntax

Hi! I am trying to wrap my head around rewriting the following model using the non-linear syntax in brms, but I can’t quite get it right.

bf(
  mvbind(Y1, Y2, Y3) ~
    X1 * X2 +
    (X1 * X2 | p | W)
)

This is what I got so far

bf(
  mvbind(Y1, Y2, Y3) ~
    0 + a + b * c,
  a ~ 0 + X1 + (1 |p| W),
  b ~ 0 + X1 + (1 |p| W),
  c ~ 0 + X2 + (1 |p| W),
  nl = TRUE
)

But that doesn’t model the correlation between the group-level slopes and I am not sure how to go about it. (I am trying to get the non-linear syntax so that I can remove the intercept and use McElreath “indexing” approach in brms but 0 + ... only works with the first predictor in the formula).

I realised I can just create a new column with the combination of categorical predictors…

bf(
  mvbind(Y1, Y2, Y3) ~ 0 + a,
  a ~ 0 + X1.X2 + (0 + X1X2 |p| W)
  nl = TRUE
)

Which could well just be:

brm(mvbind(Y1, Y2, Y3) ~ 0 + X1.X2 + (0 + X1.X1 |  w), ...)

if you want to suppress the intercept and have an explicit level for each combination of factors, you can omit the main effects and specify only and interaction:

bf(
  mvbind(Y1, Y2, Y3) ~ 0 +
    X1:X2 +
    (0 + X1:X2 | p | W)
)

This achieves the same as your example of creating another column, but you don’t have to do that manually. This is true for any modeling in r. For example:

dat <- data.frame(y = rnorm(8), 
                  x1 = rep(c('A', 'B'), each = 4),
                  x2 = rep(c('C', 'D'), times = 4))

summary(lm(y ~ 0 +x1:x2, data = dat))

#>  Coefficients:
#>         Estimate Std. Error t value Pr(>|t|)  
#> x1A:x2C  -0.5352     0.3272  -1.636   0.1772  
#> x1B:x2C  -1.1125     0.3272  -3.400   0.0273 *
#> x1A:x2D  -1.2772     0.3272  -3.904   0.0175 *
#> x1B:x2D   0.1448     0.3272   0.443   0.6809  
2 Likes

Oh wow! Thank you, that’s it! I had no idea that would work and never occurred to me to to try it. :) Thank you!

1 Like