 Multilevel modeling in brms

I am working on a model in which I would like the coefficient of one variable to be modeled as explicitly dependent on other variables. Generally speaking, the scenario is one in which we have a dichotomous outcome Y, a three-level predictor of interest A (0, 1, 2), and multiple additional covariates (x1, x2, x3). I would like to estimate the overall effect of A on Y, but also allow this effect to vary with respect to the covariates. I run into an error when running the model included below (“Error: Factors with more than two levels are not allowed as covariates.”). Any thoughts on how to best proceed (or approach this problem in a different way)?

``````df <-
tibble(
a = rep(c(0, 1, 2), each = 50),
x1 = rnorm(150),
x2 = rnorm(150),
x3 = rnorm(150),
y = rbinom(150, size = 1, prob = 0.5)
) %>%
mutate(
a = factor(a)
)

model <-
brm(
data = df,
family = bernoulli,
formula = bf(y ~ (b1 * a) + b2 + b3 + b4,
b1 ~ 1 + x1 + x2 + x3,
b2 ~ 0 + x1,
b3 ~ 0 + x2,
b4 ~ 0 + x3,
nl = TRUE),
prior = c(prior(normal(0, 1.5), nlpar = "b1"),
prior(normal(0, 1.5), nlpar = "b2"),
prior(normal(0, 1.5), nlpar = "b3"),
prior(normal(0, 1.5), nlpar = "b4")),
iter = 1000, warmup = 200, chains = 1, cores = 1,
seed = 19
)
``````
• Operating System: macOS Mojave 10.4.3
• brms Version: 2.8.0

Hi, not an expert on `brms`, but since others have not chimed in, I will give it a try.

This is a problem only because you are using the non-linear features of `brms` which work quite differently than the base version. In particular, there is no coefficient associated with the `b1` term in the formula for `y` and so it doesn’t make sense to vary this coefficient for each level of the factor. If you want to do this, you IMHO should instead have something like `y ~ b1 + b2 + b3 + b4` and `b1 ~ a * (1 + x1 + x2 + x3)`

But the main question is: Why do you want to use the non-linear features, why isn’t something like

``````y ~ a + a*x1 + a *x2 +a *x3
``````

acceptable?

1 Like

Apologies for the delay - thanks so much for your response! I think your main question is absolutely right – I had run myself in circles trying to solve a problem that was actually more simple than I had thought.

1 Like