lnccbrown / HSSM

Development of HSSM package
Other
70 stars 10 forks source link

Syntax issues when defining hyperpriors #450

Open gingjehli opened 1 month ago

gingjehli commented 1 month ago

Michael and I just noticed the following syntax issue for prior specification. Specifically, it matters whether one uses: "1 |participant_id" or "1|participant_id".

Example with the spacing:

sm6 = hssm.HSSM( data=data, include=[ { "name": "v", "formula": "v ~ 0 + (1 | participant_id)", "prior": { "1 |participant_id": {"name": "Normal", "mu": {"name": "Normal", "mu": 1, "sigma": 2, "initval": 1}, "sigma": {"name": "HalfNormal", "sigma": 1 }, }, }, }, ], noncentered = False, )

provides this output:

v: Formula: v ~ 0 + (1 | participant_id) Priors: v_1|participant_id ~ Normal(mu: 0.0, sigma: HalfNormal(sigma: 2.5)) Link: identity Explicit bounds: (-inf, inf)

Example without the spacing:

sm6 = hssm.HSSM( data=data, include=[ { "name": "v", "formula": "v ~ 0 + (1 | participant_id)", "prior": { "1|participant_id": {"name": "Normal", "mu": {"name": "Normal", "mu": 1, "sigma": 2, "initval": 1}, "sigma": {"name": "HalfNormal", "sigma": 1 }, }, }, }, ], noncentered = False, )

provides this output:

v: Formula: v ~ 0 + (1 | participant_id) Priors: v_1|participant_id ~ Normal(mu: Normal(mu: 1.0, sigma: 2.0, initval: 1.0), sigma: HalfNormal(sigma: 1.0)) Link: identity Explicit bounds: (-inf, inf)

note that the model should be specified with hyperpriors (so the example without the spacing is correct!)

digicosmos86 commented 1 month ago

Hi @gingjehli,

This is expected behavior. When specifying priors there should not be spaces. Spaces in formulas are simply parsed out, like in many programming languages, so even if you write (1 | participant_id), the correct name for the term is still 1|participant_id.

I am going to close this issue for now. Please feel free to reopen it if you still run into problems :).

frankmj commented 1 month ago

Hi Paul, the problem is that it would be great if it did parse out the space as if it wasn’t there but it turns out it does not actually do that (ie. With the space it doesn’t actually implement the hierarchy but the user would not know that unless they carefully checked how the priors get implemented in the model output). It took us a while to figure out why Nadja couldn't replicate my approach to using hyperpriors until she finally realized she had inserted a space.

If this is not simple to change (eg to remove spaces prior to |) we can simply make sure to clearly document this for the user that they need to make sure not to put any spaces!

On Fri, May 31, 2024 at 9:55 AM Paul Xu @.***> wrote:

Hi @gingjehli https://github.com/gingjehli,

This is expected behavior. When specifying priors there should not be spaces. Spaces in formulas are simply parsed out, like in many programming languages, so even if you write (1 | participant_id), the correct name for the term is still 1|participant_id.

I am going to close this issue for now. Please feel free to reopen it if you still run into problems :).

— Reply to this email directly, view it on GitHub https://github.com/lnccbrown/HSSM/issues/450#issuecomment-2142226244, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAG7TFFGYLBO62BUPMQJRQ3ZFB6LXAVCNFSM6AAAAABIRUL6Q2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNBSGIZDMMRUGQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

digicosmos86 commented 1 month ago

Hi Michael,

I agree that it can be confusing that the formula does the parsing but not prior specifications. I think the best solution to this is for bambi to throw an error when there are prior specification without corresponding terms, which it currently doesn't do yet. This will handle this issue and also accidental typos. We could do that but it's probably best to fix this at the bambi level so we don't duplicate the effort.

Let me coordinate with Tomi about this and see what the best way forward. For now, it'd probably be best to document this as a rule in our documentation. I'll work on this.

digicosmos86 commented 1 month ago

Bambi issue here https://github.com/bambinos/bambi/issues/815