I want to make a model which does two separate regressions, each with species-level effects, and then does a (phylogenetic) regression on those two slopes (with a pair of slope values for each species).
b ~ beta0 + beta1*year // Year regression on b
beta0 + beta1 ~ 1 | species // Make it species-level
a ~ hatbeta0 + hatbeta1*year // Year regression on a
hatbeta0 ~ 1 | species // Make it species-level
hatbeta1 ~ 1 + beta1 // Do a regression on the two slopes
Is this possible in brms? I wrote a Stan version of this model, but I’m in integration hell right now trying to get it work, and I wondered if maybe I’m rebuilding the wheel here.
This is what I have in the way of brms code. (My data frame is species,year,b,a.)
brm(
bf(
b ~ beta0 + beta1*year,
beta0 + beta1 ~ 1 | species,
nl=TRUE
) + bf(
a ~ hatbeta0 + hatbeta1*year,
hatbeta0 ~ 1 | species,
hatbeta1 ~ 1 + beta1,
nl=TRUE
) + set_rescor(FALSE),
data=species_measures,
chains=1
)
But it doesn’t like that I have beta1 in in the second bf and defined in the first bf… If I put them in the same bf then it suddenly doesn’t recognize a as data and the whole thing breaks.
I’m very excited to hear what you all have to say!
(More formally, this is the model:
Let b_i be b measurements associated with year observations y_{i}, and a_j be a measurements associated with year observations \hat y_{i}. For each of these s_{i}\in\{1,\cdots,n\} and \hat s_{j}\in\{1,\cdots,n\} are the corresponding species indices, for n species total. Then let \beta_{0,k},\beta_{1,k},\sigma_{k} and \hat\beta_{0,k},\hat\beta_{1,k},\hat\sigma_{k} be species-level intercepts, slopes, and standard deviations for each species, for b and a measurements respectively.
We want to fit the parameters B_0,B_1, and \Sigma (eventually \Sigma will be the combination of a Pagel’s lambda parameter, a scaling coefficient, and a phylogenetic distance matrix; for now, it is simply a scalar).
The model is as follows:
b_i\sim\mathcal N(\beta_{0,s_{i}}+\beta_{1,s_{i}}y_{i}, \sigma_{i})
a_j\sim\mathcal N(\hat \beta_{0,\hat s_{j}}+\hat \beta_{1,\hat s_{j}}\hat y_{j}, \hat\sigma_{j})
\hat\beta_k\sim\mathcal N(B_0+B_1\beta_k,\Sigma)
I care about the estimation of B_1.)