Skip to content

BUG(?): GLMGam fit_regularized needs handling of 1D np array #6506

Closed

Description

@weianwang

It looks like we iterate through all regressors one by one in fit_regularized (i.e. fit_elasticnet). For each iteration, we initialize a GAM with that regressor and pass in a numpy 1darray of shape (nobs, ).

The problem is that when initializing GAM with single regressor, you're trying to call .shape[1] on the numpy 1darray (sttasmodels/gam/generalized_additive_model.py, line 529 to 531). Don't we need to make this 2d before calling shape[1], similar to what is done in statsmodels/base/model.py at line 1092-1097?

Specifically in line 1097 of model.py, there is a comment: "needed in count model shape[1]"

What am I missing?

Activity

  1. weianwang commented on Feb 13, 2020

    @weianwang
    Author

    @josef-pkt 65ba8f4

    This is the latest iteration, no?

  2. josef-pkt commented on Feb 13, 2020

    @josef-pkt
    Member

    The 1-dim case might be a bug.

    However, I'm not sure whether it makes sense to use glmnet fit_regularized with GAM, I guess not.
    GLMGam uses it's own L2 penalization where the penalization depends on the smoothness, i.e. integrated 2nd derivative of the nonlinear function.

  3. weianwang commented on Feb 14, 2020

    @weianwang
    Author

    this issue is not just on fit_regularized--it's the 1 dim case for just any GAM that will break, isn't it? though i recognize there may rarely be a use for the 1dim case outside of fit_regularized

  4. josef-pkt commented on Feb 14, 2020

    @josef-pkt
    Member

    Yes, it's a bug.

    test_penalized TestGAM6ExogBfgs is a unit test with a constant only for exog_linear. The test breaks when I used 1-dim constant instead of the current 2-dim constant.

    self.data_linear has the data for the linear part from handle_data.

    (Pdb) self.data_linear.exog.shape
    (133, 1)
    

    This should be used instead of using the user provided exog.
    I guess that was incomplete refactoring after adding pandas support to GAM.

  5. josef-pkt commented on Feb 14, 2020

    @josef-pkt
    Member

    statsmodels/base/model.py at line 1092-1097

    this is for predict to create a 2-dim row exog.
    The data handling for the model endog and exog is delegated to the data classes.

  6. josef-pkt commented on Feb 14, 2020

    @josef-pkt
    Member

    Thanks for catching this

  7. josef-pkt commented on Feb 14, 2020

    @josef-pkt
    Member

    fix using data_linear works for TestGAM6ExogBfgs test case

            if exog is not None:
                exog_linear = self.data_linear.exog
    

    (Aside:
    According to code comments, there are still open issues how to handle this in general. currently we use handle_data twice. It might be possible to add missing handling when we do the first handle_data)

  8. added a commit that references this issue on Feb 16, 2020
    2d5bb91
  9. josef-pkt commented on Feb 16, 2020

    @josef-pkt
    Member

    fix in #6520

  10. added 2 commits that reference this issue on Feb 17, 2020
    7285c8d
    c51c176
  11. added this to the 0.12 milestone on Jul 27, 2020
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions