How to constrain regression parameters (eg. slopes <= 0) in stan_lmer (or in any rstanarm functions)

Hello! I’m trying to add some constraint to the regression slopes in my model below:

stan_lmer(Response ~ Time:Temp + (Time:Temp|Batch), data = d, …)

In this model, I have 3 fixed time slopes (as Temp has 3 levels).
I hope to constrain all those 3 slopes to be <= 0. While this could be easily done in stan parameters block, I am not sure how to do this in rstanarm functions.

I couldn’t figure out how to add this as a constraint, so alternatively I tried to express this constraint as priors to add to stan_lmer, but could not find a relevant choice. I would really appreciate your advice on how to add constraints to the parameters!

Unfortunately there’s no way to do the strict lower bound in rstanarm (you can put informative priors on the “fixed effects” using the prior argument but no strict bound).

If you definitely want the lower bound and don’t want to code it up in Stan yourself then I think it should be possible with the brms R package. I‘ve never tried it myself with brms so I’m not exactly sure how to do it. Maybe via the set_prior() function. If you try to figure that out with brms but can’t I recommend starting a new topic here on the forum in the brms category.

Hi Jonah,
Thanks so much for your response! It’s helpful to confirm that there’s no way to do the lower bound in rstanarm. I had tried informative priors based on the priors supported in rstanarm, but as you noted I could not find a way to impose <= 0 through priors. Is there a way to put uniform prior with (-Inf, 0) in rstanarm? Or maybe express the prior as the negative of a positive distribution?

In the meantime, I will look at brms to see if I can impose constraints as you advised. I may indeed create a new topic in brms for follow up discussions. Thank you again!

In brms, the prior specification would go like this, where ub is the upper bound on the prior:


m = brm(formula = Response ~ Time:Temp + (Time:Temp|Batch), 
        family = gaussian(),
        data = d, 
        prior = prior(normal(0,2), class="b", ub=0),
        backend = "cmdstanr")

I used cmdstanr as the backend (so you’d need to have CmdStan and cmdstanr installed), because setting a bound on the prior with rstan, which is the default backend, crashed R when I tried this on my computer. sample_prior="yes" means that the returned model object will include draws from the prior in addition to the posterior.

Note that when setting a bounded prior as in the example above, you can’t set different priors for the individual parameters, but can only set that one prior and it applies to all of the population-level parameters in the regression formula (except for the intercept). See here for a way to set individual priors for each parameter when using bounded priors, using brms’s non-linear formula syntax.


Not at the moment, unfortunately. The brms version that @joels gave seems promising though.

1 Like

Hi Joels,
Thanks so much for the response! It’s really helpful to know that we can specify upper bound easily in there. brms runs really slow on my PC so I usually went with rstanarm but I think backend = “cmdstanr” might be the solution for that! I am in the process of installing CmdStan and cmdstanr as you advised. I will share any update once I try them out! Looking forward to running your brm script once I install them.

1 Like

Hi Jonah,
Thanks for confirming that there’s no uniform prior available in rstanarm! Yes, I’m in the process of trying out brms code as Joels suggested. 😊

1 Like