You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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,)
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]
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.
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?
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.
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.
Activity
josef-pkt commentedon Aug 20, 2015
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
betachooses 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]
connerxyz commentedon Aug 21, 2015
@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.[-]quantreg: Error with > 8 explanatory variables[/-][+]quantreg: exception with singular exog[/+]MaxGhenis commentedon Sep 11, 2018
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?
josef-pkt commentedon Sep 11, 2018
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.
hpahkala commentedon Nov 16, 2018
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.
eromoe commentedon Sep 17, 2020
Xshape : (7977, 54)
got error:
How do I solve it ?
bashtage commentedon Sep 17, 2020
Can you provide a complete copy-pastable example?
eromoe commentedon Sep 17, 2020
It is sensitive data , I am sorry I can't expose it .
bashtage commentedon Sep 17, 2020
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.
eromoe commentedon Sep 18, 2020
@bashtage You can use blow data to test
X_train.txt
y_train.txt
Easily get error by:
josef-pkt commentedon Sep 6, 2021
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.