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)
library(readr)
context_df <- read_csv("./fake_context.csv")
exog_df <- read_csv("./fake_exog.csv")
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)