# Rolling regression (Gaussian random walk)

I am trying to implement in brms the rolling regression I found in the PyMC3 examples: https://docs.pymc.io/notebooks/GLM-rolling-regression.html

Briefly, the model looks like

GLD_T = \alpha_T + \beta_T *GFI_T
\alpha_t \sim \mathcal{N}(\alpha_{t-1}, \sigma_\alpha)
\beta_t \sim \mathcal{N}(\beta_{t-1}, \sigma_\beta)

so that the regression coefficients vary as a Gaussian random walk.

I thought I could use the non linear formulas in brms but I fear it is not possible. Below you find my attempt, which does not work (Error: Explicit covariance terms can only be specified on ‘mu’.). Would you be able to help?

library('tidyverse')
library('brms')

bf <- bf(
GLD ~ a + GFI*b,
nl = TRUE
) +
gaussian() +
lf(a ~ ar(p = 1)) +
lf(b ~ ar(p = 1))


EDIT: Just realized my question is similar to Possible to fit this time series model in brms?

1 Like

I think you are at the edge of what brms can offer. For a clean solution I think you would need to use Stan. However, you might be able to hack around this limitation. If we define:

\bar\alpha_t = \alpha_t - \alpha_{t-1}

then

\alpha_t = \alpha_0 + \sum_i^t \bar \alpha_i \\ \alpha_t \sim N(0, \sigma_\alpha)

Similarly for \beta.
Sou you can then create a normal linear formula (possibly machine generated) of the form:

GLD ~ 1 + GFI +
(1 + GFI | mm(a1, a2, a3,  ..., a15,
weights = cbind(t1, t2, t3, ..., t15), scale = FALSE))


Now aN would always be "N" for all N and tN would be 1 if the time of the observation is <= N and 0 otherwise. This way only the terms up to the current time point are included. The mm term means that all the coefficients share the same standard deviation.

I didn’t test this, so please double check the generated Stan code, but I think it could work.

Best of luck with your model!

Smart idea! I need to try tomorrow. I’ll let you know. Thanks!