Skip to content

GLMGam with patsy's built-in B-Spline Functionality #7609

Open

Description

@pzivich

Request

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:

gam = sm.GLMGam.from_formula(formula="y ~ x1 + x2", data=d,
                             smoother=BSplines(d[['w', ]], df=[5], degree=[2], include_intercept=True),
                             alpha=[5])
fgam = gam.fit()

Instead, the solution would be something along the lines of

gam = sm.GLMGam.from_formula(formula="y ~ x1 + x2 + bs(w, df=3, degree=2)", data=d,
                             alpha=[5])
fgam = gam.fit()

Describe alternatives you have considered

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.

Activity

  1. josef-pkt commented on Jul 29, 2021

    @josef-pkt
    Member

    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

  2. josef-pkt commented on Aug 2, 2021

    @josef-pkt
    Member

    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

  3. pzivich commented on Aug 2, 2021

    @pzivich
    Author

    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?

  4. josef-pkt commented on Aug 2, 2021

    @josef-pkt
    Member

    Hopefully, we get a replacement for patsy #6858

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions