Modeling sigma in brms using mean

Is it possible to have brms do the following model:

in pseudocode:

y ~ N(exp(log_mu), exp(log_sigma))
log_mu = X * Beta
log_sigma = alpha0 + alpha1 * log_mu

clearly the log link on the mean and variance is easy and well documented but I wasn’t sure if I was able to utilize the mean in predicting the variance.

I think you can achieve this readily though the nonlinear modeling syntax in brms. Do the examples here clarify the way forward?
https://cran.r-project.org/web/packages/brms/vignettes/brms_nonlinear.html

1 Like

Btw, I’ve found location-scale models tend to behave better when modelling the log-variance (rather than log-sd as you have).

Isn’t the log variance just exactly twice the log sd, which would just rescale the coefficients?

1 Like

Lol, I never realized that, but of course! 🤦‍♂️ I guess my “better behaved” anecdote is spurious

3 Likes

Thanks that’s what I needed

Oh wait! I just remembered why I like log-variance; because then a std_normal() prior has a nice pushforward density for the standard deviation:

I can’t seem to get the syntax right

mm <- brm(
  bf(size | trunc(lb=0) ~ eta) + stats::gaussian(link="log") + 
    lf(eta ~ log(time) + (1|id)) + 
    nlf(sigma ~ b0 + b1 * log(eta), b0+b1~1),
          data=tmp, 
          prior = c(prior(normal(0,5),class = "Intercept"), 
                    prior(normal(0, 5), nlpar = "b0"), 
                    prior(normal(0, 5), nlpar = "b1")),
          chains=4, cores=4, backend = "cmdstanr", refresh=100)
Error: The following variables can neither be found in 'data' nor in 'data2':
'eta'
In addition: Warning message:
Arguments '...' and 'flist' in nlf() will be reworked at some point. Please avoid using them if possible. 

Seems similar to

I also tried

get_prior(
  bf(size | trunc(lb=0) ~ mu, 
     mu ~ log(time) + (1|id), 
     sigma ~ b0 + b1 * log(mu), b0~1, b1~1, nl=TRUE),
          data=tmp)
Error: The parameter 'b0' is not a valid distributional or non-linear parameter. Did you forget to set 'nl = TRUE'?

You need to specify nl = TRUE inside bf(), that is bf(size | trunc(lb=0) ~ eta, nl = TRUE). Otherwise, brms will parse this formula as a linear formula and thus expects eta to be data.

1 Like