Skip to content

Multicollinearity in logit? #1534

Open
Open

Description

@jseabold

It looks like R is dropping some variables here or something? The R results indicate NaNs in the regression, and we don't match the results. See my comments there.

https://stackoverflow.com/questions/22716521/why-cant-statsmodels-reproduce-my-r-logistic-regression-results/22719638

Activity

  1. josef-pkt commented on Mar 29, 2014

    @josef-pkt
    Member

    see #452 and #953

    GLM uses WLS with uses pinv and should be robust (most likely) to singular exog, but will pick "some" solution.

    If it's really singular, then we don't need to match the params, we only need to match fittedvalues, if there are different ways of handling singular matrices. We pinv, Stata drops variables, patsy might drop columns, no idea what R does. discrete_model breaks with default optimizer newton.

  2. jseabold commented on Mar 29, 2014

    @jseabold
    MemberAuthor

    Yeah I have no idea what R's doing there. I didn't get into it. Just noticed that we're having "problems" on this textbook example. I think it's a poor model, but I'd like to dig in a little more.

    Having patsy do the multicollinearity handling for us seems nice, but also potential for more duplicate code when we don't go through patsy.

  3. josef-pkt commented on Mar 29, 2014

    @josef-pkt
    Member

    my guess is that patsy handles only perfect multicollinearity, like dropping a level of a categorical variable (in an interaction term) if there are no observations for it.
    patsy shouldn't decide and drop in the case of almost perfect collinearity (eigenvalue 1e-xxx).

    try fit_regularized on the example, that needs more advertising.

  4. jseabold commented on Mar 29, 2014

    @jseabold
    MemberAuthor

    AFAIK, patsy doesn't do anything.

    [~/]
    [6]: patsy.dmatrix('X+Y+Z', data=dict(X=[1,1,0], Y=[1,1,0], Z=[1,0,0]))
    [6]: 
    DesignMatrix with shape (3, 4)
    Intercept  X  Y  Z
            1  1  1  1
            1  1  1  0
            1  0  0  0
    Terms:
        'Intercept' (column 0)
        'X' (column 1)
        'Y' (column 2)
        'Z' (column 3)
    

    I think it's simply a case of way too many (sparse) dummy variables.

  5. josef-pkt commented on Mar 29, 2014

    @josef-pkt
    Member

    same with categorical variables:

    >>> ex = patsy.dmatrix('X*Y', data=dict(X=['A','A','A','A','A','B'], Y=['a', 'a', 'a', 'a', 'b', 'c']))
    >>> ex
    DesignMatrix with shape (6, 6)
      Intercept  X[T.B]  Y[T.b]  Y[T.c]  X[T.B]:Y[T.b]  X[T.B]:Y[T.c]
              1       0       0       0              0              0
              1       0       0       0              0              0
              1       0       0       0              0              0
              1       0       0       0              0              0
              1       0       1       0              0              0
              1       1       0       1              0              1
      Terms:
        'Intercept' (column 0)
        'X' (column 1)
        'Y' (columns 2:4)
        'X:Y' (columns 4:6)
    >>> linalg.svdvals(ex)
    array([  2.63279889e+00,   1.52420146e+00,   8.63238025e-01,
             1.80290444e-17,   1.09434876e-33,   0.00000000e+00])
    
  6. scollie commented on Oct 26, 2014

    @scollie

    Has anybody thought of how to deal with near-perfect collinearity in patsy? I would like to get the same result as stata, where variables that are almost perfectly collinear are dropped from the regression

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