Skip to content

Drop redundant columns from exog #4705

Open

Description

@kshedden

Do we have a way to drop redundant columns automatically from exog during model initialization? This comes up sometimes when working with formulas having complicated interactions and dummy coding. Sometimes there are linear dependencies in the design matrices that cannot easily be removed by modifying the formula. I end up splicing the code below into the model __init__ function.

q, r = np.linalg.qr(exog)                                            
dr = np.abs(np.diag(r))                                              
if np.any(dr < 1e-10):                                               
    ii = np.flatnonzero(dr >= 1e-10)
    exog = exog[:, ii]
    if exog_names != None:
        exog_names = [exog_names[i] for i in ii] 

Activity

  1. josef-pkt commented on Jun 2, 2018

    @josef-pkt
    Member

    I added a fit_collinear, and we will get some code to identify and drop collinear columns.

    However, I don't know if we can include it in model initialization.
    If we drop columns, then the formula design_info does not correspond to the internal exog.
    For example we would need to handle the same column dropping in predict after patsy creates a predict exog.

    In fit_collinear I internally create a model with reduced exog, but the main model of the user has zeros in params and cov_params.
    This is similar to OLS where the pinv sets the params of a column of zeros to zero, but I don't remember the other details of that case.

  2. josef-pkt commented on Jun 2, 2018

    @josef-pkt
    Member

    I added _fit_collinear here https://github.com/statsmodels/statsmodels/pull/4576/files#diff-b165b4bd4e10edd0dfb485e47c562b2cR655
    But the main work for recreating a model and fitting it with zero-constraints is in _fit_zeros

  3. josef-pkt commented on Jun 2, 2018

    @josef-pkt
    Member

    BTW: do you have any information about what a good default threshold for the qr version of identifying collinear columns is?

  4. kshedden commented on Jun 2, 2018

    @kshedden
    ContributorAuthor

    R seems to do this somehow pretty universally across models.

    I need it for models like MixedLM where there is non-standard parameter packing, so it would probably need to be handled there as a special case.

    Maybe we could store the indices of non-redundant columns under an attribute with a standard name, like _use_cols, then predict would look for this and if present expand or reduce the parameter vector as needed.

    Regarding thresholds, no I don't know what would be a good constant.

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