Skip to content

quantreg: exception with singular exog #2597

Closed

Description

@connerxyz

I'm trying to run quantile regression with a number of variables. It appears quantreg.fit() will not allow more than 8 variables/columns to be included in the formula.

import pandas as pd 
import statsmodels.formula.api as smf
from statsmodels.regression.quantile_regression import QuantReg

d = pd.read_csv('<path-to-data>')

#7 vars (lag and sunday through friday), seems to work fine
qr = smf.quantreg('load ~ lag + su + m + t + w + th + f',  d')
qm = qr.fit(q=0.5)

#8 vars (lag and sunday through saturday), throws error
qr = smf.quantreg('load ~ lag + su + m + t + w + th + f + s',  d')
qm = qr.fit(q=0.5)

Here's the trace:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-17-f2074b49225f> in <module>()
---> 32 qm = qr.fit(q=.5)

.../statsmodels/regression/quantile_regression.py in fit(self, q, vcov, kernel, bandwidth, max_iter, p_tol, **kwargs)
    177             resid = np.abs(resid)
    178             xstar = exog / resid[:, np.newaxis]
--> 179             diff = np.max(np.abs(beta - beta0))
    180             history['params'].append(beta)
    181             history['mse'].append(np.mean(resid*resid))

ValueError: operands could not be broadcast together with shapes (9,) (8,) 

Activity

  1. josef-pkt commented on Aug 20, 2015

    @josef-pkt
    Member

    That's a weird error. I don't think any of our models have hardcoded restriction on the number of explanatory variables, at least as long as there is a minimum number of degrees of freedom.

    Looking briefly at the code:
    The initial beta chooses the length of the array by rank not by the number of exog columns. This will break if the exog is singular (or rank deficient according to the numerical threshold).

    My guess base on the names of your variables:
    If you include all seven weekday dummies, and patsy adds a constant, then your exog is singular with 9 columns but rank 8. (dummy trap)

    Using rank is a "usability bug", but I don't know what the behavior with singular exog should be. My guess is that the same pinv regularized solution will go through as in OLS/WLS, when starting beta has the length of k_vars, exog.shape[1]

  2. added this to the 0.8 milestone on Aug 20, 2015
  3. connerxyz commented on Aug 21, 2015

    @connerxyz
    Author

    @josef-pkt dummy trap indeed!

    It's not an issue with the number of variables as I suggested. I am using pd.get_dummies() and removing the last column from each dummy group afterwards cleared this up.

  4. changed the title [-]quantreg: Error with > 8 explanatory variables[/-] [+]quantreg: exception with singular exog[/+] on Aug 21, 2015
  5. MaxGhenis commented on Sep 11, 2018

    @MaxGhenis
    Contributor

    It seems that this results from multicollinearity (I hit it for that reason); if that's the case, would it be possible to print a more informative error message?

  6. modified the milestones: 0.8, 0.10 on Sep 11, 2018
  7. josef-pkt commented on Sep 11, 2018

    @josef-pkt
    Member

    prio-elev so it's less likely to get lost again. (AFAICS in the source, it is still unchanged)

    specific fix is just changing one line.
    Warning or message about collinearity will need some additions.

  8. hpahkala commented on Nov 16, 2018

    @hpahkala

    I second more informative warning or message.
    I had collinearity problem in my data but started to debug the issue from the wrong place due to the message.

  9. eromoe commented on Sep 17, 2020

    @eromoe

    Xshape : (7977, 54)

    got error:

    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-211-44ce090fc516> in <module>
    ----> 1 qr75 = sm.QuantReg(y_train.sale_count, X_train).fit(q=.75)
          2 qr75_pred = qr75.predict(X_test)
          3 
    
    ~/.conda/envs/py3/lib/python3.7/site-packages/statsmodels/regression/quantile_regression.py in fit(self, q, vcov, kernel, bandwidth, max_iter, p_tol, **kwargs)
        175             resid = np.abs(resid)
        176             xstar = exog / resid[:, np.newaxis]
    --> 177             diff = np.max(np.abs(beta - beta0))
        178             history['params'].append(beta)
        179             history['mse'].append(np.mean(resid*resid))
    
    ValueError: operands could not be broadcast together with shapes (54,) (53,) 
    

    How do I solve it ?

  10. bashtage commented on Sep 17, 2020

    @bashtage
    Member

    Can you provide a complete copy-pastable example?

  11. eromoe commented on Sep 17, 2020

    @eromoe

    It is sensitive data , I am sorry I can't expose it .

  12. bashtage commented on Sep 17, 2020

    @bashtage
    Member

    You can always simulate a dataset that has the same characteristics and produces the same problem. This is preferred to using real data which requires sharing a file.

  13. eromoe commented on Sep 18, 2020

    @eromoe

    @bashtage You can use blow data to test
    X_train.txt
    y_train.txt

    Easily get error by:

    qr75 = sm.QuantReg(y_train, X_train).fit(q=.75)
  14. josef-pkt commented on Sep 6, 2021

    @josef-pkt
    Member

    fixed in #7694 based on PR #6397

    The behavior now is that it doesn't raise on singular exog, but instead provides a pinv solution.

  15. modified the milestones: 0.10, 0.13 on Sep 9, 2021
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