Moving average possible in non-linear model?

I am fitting a non-linear model within which I would like to use a moving average of an observed variable to calculate a non-linear parameter so that the parameter reflects changes observed over time in the observed variable.

@paul.buerkner @martinmodrak Is this possible with brms nonlinear syntax?

An example:

# Some data
Data <- expand.grid(r = factor(c(1:5)),
                    t = factor(c(1:8))
) %>%
        mutate(C = round(rnorm(nrow(.),500, 150)),
               D = round(rnorm(nrow(.),10000, 1000)),
               R = C/D,
               S = rnorm(nrow(.),0.5, 0.15))
nl_priors <- c(
        set_prior("normal(0, 10)",
                  class = "b",
                  nlpar = "a"),
        set_prior("normal(0, 10)",
                  class = "b",
                  nlpar = "b"),
        set_prior("normal(0, 10)",
                  class = "b",
                  nlpar = "c")
)

bform <- bf(
        S ~ (C * B)/D,
        nlf(B ~ a * R^b + c),
        B ~ ma(gr = r),
        a ~ 1,
        b ~ 1,
        c ~ 1,
        nl = TRUE
)

NL_Mod <- brm(bform,
              Data,
              family = Beta(), 
              prior = nl_priors,
              inits = "random",
              iter = 2000,
              warmup = 1000,
              chains = 1,
              cores = ncores,
              backend = "cmdstan",
              normalize = FALSE,
              save_pars = save_pars(all = TRUE),
              control = list(adapt_delta = 0.9,
                             max_treedepth = 12)
)

Which results in an error:
Error: Explicit covariance terms can only be specified on 'mu'.

unfortunately this is not yet possible. I have to think if there is a consistent way to implement this but I am not sure yet.

1 Like