Syntax for model with mean parameter defined by data

I want to fit a model where my data, y, are distributed normally, such that the mean of the distribution is taken from another variable in my data file, x (i.e., I only want to estimate the sd of the distribution). Both y and x are vectors of equal length, with unique x values for each y. I know how to accomplish this in Stan, but am new to brms and am not sure how to define my model in a brms formula; and the options I’ve tried thus far have not come close to working. If anyone could point me in the right direction to achieve this, I’d appreciate it—it seems like it should be trivially easy but I am clearly lacking the necessary insight.

1 Like

Hi,
sorry for not getting to you earlierm your question is relevant and well written.

Actually, this is not “trivially easy”, it’s a use case that brms does not support very well, so unless you plan to put some fancy predictors on the sd, you are most likely best served by staying with pure Stan.

With that said, I think using x is a predictor and putting a constant prior on both intercept and slope should do the trick, i.e. something like:

# Generate some very arbitrary means
dd <- data.frame(x = rgamma(100, shape = 5, rate = 3) + rcauchy(100, scale = 0.5 )) 
dd$y <- rnorm(100, dd$x, sd = 4)

priors <- c(prior(constant(0), class = b, coef = Intercept), prior(constant(1), class = b, coef = x))
brm(y ~ x + 0 + Intercept, data = dd, prior = priors)

The + 0 + Intercept is brms-specific syntax to avoid default centering of the intercept (because this then messes with our attempts to set it to constant).

Best of luck with your model!

2 Likes

Yes, this works perfectly. Many thanks for your assistance!

1 Like