bambinos / bambi

BAyesian Model-Building Interface (Bambi) in Python.
https://bambinos.github.io/bambi/
MIT License
1.09k stars 124 forks source link

Add piecewise regression example #437

Open tomicapretto opened 2 years ago

tomicapretto commented 2 years ago

We can define

def truncate(x, l):
    x = (x - l) * (x >= l)
    return x

And then simply do

formula = "response ~ x + truncate(x, 10) + truncate(x, 20)"
hadjipantelis commented 2 years ago

Being somewhat thick here, wouldn't be a smoother (no pun intended) option to use bs and set the degree and knots accordingly? That way, the user could have a linear spline to create a piecewise regression.

tomicapretto commented 2 years ago

Yes, you could also do that, but the following points come to my mind:

However, your point is also a motivation to show the connection between piecewise regression using truncate() and a bs() using linear functions. That could be a good example.

hadjipantelis commented 2 years ago

Fair points. I appreciate that my proposition might be a tad bullish regarding people's familiarity with splines use.

tomicapretto commented 2 years ago

I like when people get involved in the issues and propose new ideas. I really appreciate your comments. So please feel free to share when you want to propose something.

To be a little clearer, I'm not saying that we should only have the example using truncated(). What I'm saying is that we could have both, and explain the connection between them 😉

ivanmkc commented 2 years ago

@tomicapretto I tried using a custom function but I get this error:

KeyError: 'truncate'

It seems to happen because I have my bambi.Model as a property in a wrapper Model class and for some reason the truncate method (defined in the same file) can't be found.

tomicapretto commented 2 years ago

@ivanmkc could you share an example of what you're trying to do? What you're suggesting as the problem seems to make sense though.