You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
GLMGam with patsy's built-in B-Spline Functionality #7609
Following the provided example for GLMGam, it appears that the b-spline terms to have penalized estimation must be input via the smoother argument and must be a BSpline() object. However, this seems to ignore the built-in specification of splines within patsy. This interrupts how I have a workflow set-up.
Describe the solution you'd like
Here is a simple example for a data set with columns 'x1', 'x2', and 'w.' Both 'x1' and 'x2' are binary, while 'w' is continuous (and the variable we want to use b-splines for. Currently, this would have to be input like the following:
I've written a hack-y solution that is a wrapper around GLMGam. Briefly it takes the patsy-specified formula and parses it. For terms not wrapped by bs(...), it passes them individual to patsy, applies the patsy calculation, and stacks them to a numpy array (to pass to exog). For the b-spline part, the options are read from the formula and collected in lists.
The b-spline extracted lists are then passed to BSplines. The the BSpline object and extracted exog are passed into GLMGam`.
There is probably a better solution to this, but this was my first processing attempt to get around this formula issue. Either way, a feature like this for GLMGam would be nice to have.
Yes, would be nice to have.
However, patsy doesn’t provide the necessary things like second derivatives for the penalization matrix.
When we initially started with gam, patsy didn’t provide enough information about the splines used in the formula.
Even if we use splines from the formula, we would need extra information about the penalization that the user wants to have.
So, feature of using formulas for splines would be nice to have, but it’s work to extract the spline information and then to add the missing parts for penalization.
Also, patsy is not really maintained anymore.
I’m travelling and cannot look for more detailed information for now.
If you find a way to support formulas, then it would be good to discuss and see how far we can support this in statsmodels
Can you open a pull request or show your code in a gist?
I think your approach with parsing the formula directly will be the easiest way to support this and will allow us to include the options that patsy doesn’t have like setting boundary knots.
So far we never parse the formula in statsmodels, but it would be a useful solution when we have to work around patsy’s limitations
Sure thing! I have a basic example that I will create a gist for (I will try to put it together this evening).
My first attempt reads the string, does the text splitting by ~ then +, strips the leading and training white spaces, and then processes conditional on whether the string for a variable starts with 'bs('. But this won't work with / support all the patsy operators. So, I need to come up with a better way to parse (before it could be added to statsmodels).
I also don't have the full BSplines options available. I had trouble adding boundary knots because I was having difficulty implementing constraints and knot_kwds as optional arguments when not provided in the formula. I think they need to be set to None, but the code I wrote creates a list of None, which seemed to be an issue.
A more general question, since patsy is no longer actively maintained what is statsmodels plan for formulas going forward? Would the plan be to adopt some sort of formula functionality internally and transition away from patsy?
Activity
josef-pkt commentedon Jul 29, 2021
Yes, would be nice to have.
However, patsy doesn’t provide the necessary things like second derivatives for the penalization matrix.
When we initially started with gam, patsy didn’t provide enough information about the splines used in the formula.
Even if we use splines from the formula, we would need extra information about the penalization that the user wants to have.
So, feature of using formulas for splines would be nice to have, but it’s work to extract the spline information and then to add the missing parts for penalization.
Also, patsy is not really maintained anymore.
I’m travelling and cannot look for more detailed information for now.
If you find a way to support formulas, then it would be good to discuss and see how far we can support this in statsmodels
josef-pkt commentedon Aug 2, 2021
After reading your proposal again:
Can you open a pull request or show your code in a gist?
I think your approach with parsing the formula directly will be the easiest way to support this and will allow us to include the options that patsy doesn’t have like setting boundary knots.
So far we never parse the formula in statsmodels, but it would be a useful solution when we have to work around patsy’s limitations
pzivich commentedon Aug 2, 2021
Sure thing! I have a basic example that I will create a gist for (I will try to put it together this evening).
My first attempt reads the string, does the text splitting by ~ then +, strips the leading and training white spaces, and then processes conditional on whether the string for a variable starts with 'bs('. But this won't work with / support all the patsy operators. So, I need to come up with a better way to parse (before it could be added to statsmodels).
I also don't have the full BSplines options available. I had trouble adding boundary knots because I was having difficulty implementing
constraintsandknot_kwdsas optional arguments when not provided in the formula. I think they need to be set toNone, but the code I wrote creates a list ofNone, which seemed to be an issue.A more general question, since patsy is no longer actively maintained what is statsmodels plan for formulas going forward? Would the plan be to adopt some sort of formula functionality internally and transition away from patsy?
josef-pkt commentedon Aug 2, 2021
Hopefully, we get a replacement for patsy #6858