-
-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add polynomial regression example #782
Conversation
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #782 +/- ##
==========================================
+ Coverage 89.86% 90.03% +0.16%
==========================================
Files 46 47 +1
Lines 3810 3934 +124
==========================================
+ Hits 3424 3542 +118
- Misses 386 392 +6 ☔ View full report in Codecov by Sentry. |
A bit of a side note that I thought this was really nice. I found the bit on orthogonal polynomials really interesting, perhaps as a separate notebook? In particular, I was disappointed when you tee'd up "these help with inference", then followed with "...but we won't use them". Show us the |
I wanted to add another example with poly but I was worried it was getting long as-is! A separate notebook seems like a good solution, one more focused on the orthogonal polynomials with more of a statistical model lean and then the current notebook with the I(x**2) approach. |
Also, my only productive feedback is that
is a neat trick to create a copy of |
@tjburch thanks a lot! I quickly skimmed through it and I like it! I will have a more in-depth read and come back with feedback :) |
@tjburch This is great! I love how you move between the science, the data, and the model, and a reader can see how the science is captured in the model, and how the model's posterior artifacts link back to the data and science. We also see the choices one has to make. I find the most valuable insights I have gleaned in Bayesianism is seeing people like Solomon Kurz and Andrew Heiss work through real examples and the internal dialogue they have. |
@tjburch I just checked the example. It is fantastic, very well done. A couple of notes
Currently, it's specified as But I think it's clearer and more explicit if we write it as Changes
Then in Bambi, you can specify the priro for the group-specific effect as priors = {
"I(Time ** 2):Planet": bmb.Prior(
'TruncatedNormal',
mu=[
-9.81 / 2, # Earth
-3.72 / 2, # Mars
-6.0 / 2 # PlanetX
],
sigma=[
0.025 / 2, # Earth
0.02 / 2, # Mars
4 # PlanetX
],
upper=[0, 0, 0]
),
# Here it starts
"Time|Astronaut": bmb.Prior(
"Normal",
mu=bmb.Prior("Normal", mu=4, sigma=1), # Population mean
sigma=bmb.Prior("HalfNormal", sigma=1), # Population standard deviation
# Here it ends
)
}
measurement_mod_hier_prior = bmb.Model(
formula='Height ~ I(Time**2):Planet + (0 + Time|Astronaut) + 0',
data=df,
priors =priors,
noncentered=False
) Important note this is not the most common pattern in Bambi, but it's allowed. In Bambi the group-specific effects are usually a deflection around some common value (that's why we have common and group-specific effects). For example, when there are "random" slopes, we write As a sidenote, my physics knowledge is 0.1 on a scale from 0 to 10, so I cannot check whether all those things are correct or not, I simply trust you here 😄 Just let me know if you have other questions and/or you need help with something |
@tjburch regarding what you shared as "immediate questions"
|
Thanks @tomicapretto. I'll work on updating with the correct poly reference and give this a test! Much appreciated you taking a look at it. Would a separate example with |
If you feel like you want to do it, yeah, I think two separate notebooks would be better. But it's not a problem if you want to leave it as it is (we can split things it in the future) |
I'm still working to clean up the original notebook, but I've just added a second notebook that's purely dedicated to just the orthogonal polynomials and using |
View / edit / reply to this conversation on ReviewNB tomicapretto commented on 2024-03-04T15:06:47Z Line #1. diagonal = np.diag(np.diag(r)) I think there's a small mistake here (two calls to tjburch commented on 2024-03-04T15:20:34Z This is actually something I learned while putting this together. The first call of
In [1]: import numpy as np I will make a note of that though. |
@tjburch the orthogonal polynomial notebook is just AMAZING! Thanks!! |
Thanks! Hoping to have the original cleaned up shortly. You may have seen that I deleted a previous comment, I'm running into errors when truncating the response variable, which I think is contributing to the underestimates. I was able to reproduce the toy example in #752 this morning though, so I just need to side-by-side to try to understand what's different. |
@tjburch I could swear I saw a comment and when I came back today, I couldn't find it, haha! If a truncated normal does not work, you could try another distribution with a positive domain (the problem is that you'll likely need a non-identity link function and then the meaning of parameter estimates change and that's another problem) |
This is actually something I learned while putting this together. The first call of
In [1]: import numpy as np
View entire conversation on ReviewNB |
|
Alright, I think this is probably ready to no longer be a WIP and good for final review. I took out the last example with multiple astronauts in the original notebook, to me it didn't seem to help users understand more information and injecting priors didn't really help the estimates I hoped it would, so it seemed like not a huge value add. If anyone feels strongly otherwise, I can go put it back in, but I think that the notebooks as-is do a pretty good job conveying the ins-and-outs of polynomial regression. |
Amazing @tjburch! I'll review it soon |
@tjburch I didn't mean to close this. I tried to add some changes to this branch but I broke something :S. I'm trying to fix it. |
Whoops! If you're unable to recover, let me know and I can open a new PR or something, but all things equal it'd be nice to keep some of this prior discussion. |
This PR creates a new example notebook of polynomial regression using Bambi that I've been working on for the last few weeks. There's still some tweaks on the last example that need to be done, I'm hoping to get some feedback here.
Immediate questions:
Thanks in advance for feedback, hopefully it's an interesting notebook regardless if it's suited for the example gallery or not.