Skip to content
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 piecewise regression example #437

Open
tomicapretto opened this issue Jan 9, 2022 · 6 comments · May be fixed by #879
Open

Add piecewise regression example #437

tomicapretto opened this issue Jan 9, 2022 · 6 comments · May be fixed by #879

Comments

@tomicapretto
Copy link
Collaborator

tomicapretto commented Jan 9, 2022

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
Copy link

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
Copy link
Collaborator Author

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

  • You need to know or understand what a spline basis is.
  • In many cases (or most?) piecewise linear regression is not introduced as a special case of a basis spline.
  • This example would also show that you can write and use your own functions in the model formula.

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
Copy link

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

@tomicapretto
Copy link
Collaborator Author

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
Copy link

ivanmkc commented Apr 12, 2022

@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
Copy link
Collaborator Author

@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.

speco29 added a commit to speco29/bambi that referenced this issue Jan 23, 2025
Added piecewise regression example to example docs.
speco29 added a commit to speco29/bambi that referenced this issue Jan 23, 2025
Adding piecewise regression examples in example docs
@speco29 speco29 linked a pull request Jan 23, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants