paul-buerkner / brms

brms R package for Bayesian generalized multivariate non-linear multilevel models using Stan
https://paul-buerkner.github.io/brms/
GNU General Public License v2.0
1.29k stars 187 forks source link

Separate priors for hyperparameters of varying effects by categorical variable #874

Open ferberkl opened 4 years ago

ferberkl commented 4 years ago

Hi Paul,

I'm trying to allow the full variance-covariance matrix of the random effects to vary across groups using the gr(re, by = "cat_var") syntax (from issue 365)

One feature request and one (I think) bug report:

The feature request is to be able to specify priors on the varying SDs separately by the "by" variable. This doesn't seem to be possible at the moment (version 2.12.0).

Second, the possible bug:

The syntax works with just varying intercepts, i.e. (1 | gr(re, by = "cat_var")), but I'm running into issues when I have multiple varying effects.

Using the epilepsy data example:

This works:

fit1 <- brm(count ~ zAge + zBase * Trt + (1|gr(patient, by=Trt)),
             data = epilepsy, family = poisson(), chains = 1)

This does not work:

fit2 <- brm(count ~ zAge + zBase * Trt + (1 + visit |gr(patient, by=Trt)),
            data = epilepsy, family = poisson(), chains = 1)

The error I'm getting in this reprex and also when I try this with my data is:

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: poisson_log_lpmf: Log rate parameter[1] is nan, but must not be nan!  (in 'model3e997988b9f9_7b6d2a531910ca110e77173edeb2ac8e' at line 85)

many times, followed by

Chain 1: 
Chain 1: Initialization between (-2, 2) failed after 100 attempts. 
Chain 1:  Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] "Error in sampler$call_sampler(args_list[[i]]) : Initialization failed."

Many thanks.

paul-buerkner commented 4 years ago

Thanks for opening this issue. The bug should already be fixed in the github version of brms I think.

With regard to the feature request, I agree that it would be nice to specify priors separately. Not sure yet how to best intergrate that into the existing interface. Suggestions are welcome!

ferberkl commented 4 years ago

Thanks, Paul. It does seem to be working in the github version on my end.

Regarding how to set priors, I think it would make sense to use the interaction syntax. This is how the parameters are printed in the github version. For example, in the model:

fit2 <- brm(count ~ zAge + zBase * Trt + (1 + visit |gr(patient, by=Trt)),
            data = epilepsy, family = poisson(), chains = 1)

to set priors on the SDs:

set_prior("normal(0, 1)", class = "sd", group = "patient", coef = "Intercept:Trt0")

or perhaps the underscore syntax similar to what is used for correlation matrices.

set_prior("normal(0, 1)", class = "sd", group = "patient_Trt0", coef = "Intercept")
set_prior("normal(0, 1)", class = "sd", group = "patient_Trt1", coef = "Intercept")

For the correlation matrices, maybe something like the following:

set_prior("lkj(1)", class = "cor_patient_Trt0")
set_prior("lkj(2)", class = "cor_patient_Trt1")
paul-buerkner commented 4 years ago

Thanks. I think by should be either part of group of coef or something entirely new. Making it part of class would be inconsistent. I will need to think more of this and its implications.

hallg92 commented 3 years ago

Hi Paul,

I'm wondering if this feature has been added to brms. I checked the documentation, and it did not appear to be, but I just wanted to make sure I was not missing something.

Thank you for all your work on brms. It is a really amazing resource.

paul-buerkner commented 3 years ago

this has not been implemented yet because it needs a new argument in the prior specification functions and I have to think carefully about a name and how that can be generalized later on.

hallg92 @.***> schrieb am So., 24. Okt. 2021, 03:57:

Hi Paul,

I'm wondering if this feature has been added to brms. I checked the documentation, and it did not appear to be, but I just wanted to make sure I was not missing something.

Thank you for all your work on brms. It is a really amazing resource.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/paul-buerkner/brms/issues/874#issuecomment-950246260, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADCW2AFPXJ3NVVC4ZJU6BOLUINRXDANCNFSM4MDHYH6Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

joshgilbert1994 commented 3 weeks ago

Hello,

I notice this is an old thread. I am wondering if this feature has been added. I ask because it would be quite helpful to me in fitting some IRT models where I would like to fix the SD of theta to 1 for the control group but freely estimate it for the treatment group.

Thank you!

Josh

paul-buerkner commented 3 weeks ago

I understand it has important use-cases. I simply didn't have the time to think more about it. sorry.

joshgilbert1994 commented 3 weeks ago

Thanks for the reply. I did find an alternative way to fit the model, so no worries.

Thanks!

On Wed, Nov 6, 2024 at 7:27 AM Paul-Christian Bürkner < @.***> wrote:

I understand it has important use-cases. I simply didn't have the time to think more about it. sorry.

— Reply to this email directly, view it on GitHub https://github.com/paul-buerkner/brms/issues/874#issuecomment-2459620734, or unsubscribe https://github.com/notifications/unsubscribe-auth/AW2VX3CANT4AD6OKHYTYSETZ7IDMVAVCNFSM6AAAAABRFIWMLSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDINJZGYZDANZTGQ . You are receiving this because you commented.Message ID: @.***>

-- Josh Gilbert