# Multilevel model with responses on different levels?

I’m seeking to fit a model where I have information recorded at two separate levels in a hierarchy. My hope is that including the second response allows for better convergence than just using the group mean. The specification I’m seeking to replicate has n_1 observations of an outcome y and exogenous conditions X taken over k places across t years. Then, there are n_2 samples of a “contextual” variable, s, taken over the same places and years. Then, I’m looking to use:

y_i = \alpha + X\beta + c_{k[i]}\gamma + e_i + u_{t[i]}
s_j = a + c_{k[j]} + v_{t[j]} + \epsilon_j

With residuals modelled as independent. The aim is to use the estimates and variability for c_{k[j]} in the s_j model to inform estimates about y_i. Data is attached at the bottom.

I know I could “bag” it by fitting the s_j model separately, simulate values of c_{k[i]}, and then estimate the y_i model using those replicates, but I was wondering if there was a way to do this in brms. I’ve used the following:

context_f <- bf(c ~ 0 + context
+ (0 + context || place)
+ (0 + context || year))
exog_f <- bf(y ~ 1 + X1 + X2 + X3 + (1 || year) + context)
model_f <- speed_f + exog_f + set_rescor(F)


but when I try to fit this:

library(brms)
test <- brm(model_f, data=exog_df,
data2=context_df %>% mutate(context=1), nl=T)


I get:

Error in eval(predvars, data, env) : object '.' not found


Edit: sorry! prematurely posted!

I’m using the “context” variable there as a way to bring the estimate from the contextual model over to the exog model. Naturally, I’d like c ~ 1 + (1 || place) + (1 || year), but then I’m not sure how to bring the intercept from this model into the y~.... Any pointers?

Also, if it helps, I’ve attached
an analogous stan model for what I’m intending to do.

fake_context.csv (585.7 KB)
fake_exog.csv (559.1 KB)
both_generic.stan (1.6 KB)

1 Like

Unfortunately, I don’t think this type of model is easily possible with brms - Paul had some plans to let you explicitly share coefficients between formulas, but I think that has not happend so far. It is definitely possible to build this model in pure Stan and it might be possible to build it using the non-linear brms syntax (Estimating Non-Linear Models with brms • brms) - which would force you to make all the coefficients in the model explicit and so would be a bit verbose, but would let you have a shared coeffcient across the two formulas.

Best of luck with your model!

1 Like