Python Statsmodels QuantReg拦截

Jar*_*rad 6 python regression quantile statsmodels

问题设置statsmodels分位数回归问题中,它们的最小绝对偏差摘要输出显示截距.在该示例中,他们使用公式

from __future__ import print_function
import patsy
import numpy as np
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf
import matplotlib.pyplot as plt
from statsmodels.regression.quantile_regression import QuantReg

data = sm.datasets.engel.load_pandas().data

mod = smf.quantreg('foodexp ~ income', data)
res = mod.fit(q=.5)
print(res.summary())

                         QuantReg Regression Results                          
==============================================================================
Dep. Variable:                foodexp   Pseudo R-squared:               0.6206
Model:                       QuantReg   Bandwidth:                       64.51
Method:                 Least Squares   Sparsity:                        209.3
Date:                Fri, 09 Oct 2015   No. Observations:                  235
Time:                        15:44:23   Df Residuals:                      233
                                        Df Model:                            1
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept     81.4823     14.634      5.568      0.000        52.649   110.315
income         0.5602      0.013     42.516      0.000         0.534     0.586
==============================================================================

The condition number is large, 2.38e+03. This might indicate that there are
strong multicollinearity or other numerical problems.
Run Code Online (Sandbox Code Playgroud)

问题

如何在Intercept 使用statsmodels.formula.api as smf公式方法的情况下实现摘要输出?

Jar*_*rad 7

当然,当我把这个问题放在一起时,我想出来了.而不是删除它,我将分享以防有人在那里遇到过这种情况.

我怀疑,我需要add_constant(),但我不确定如何.我做了一些愚蠢的事情并将常量添加到Y(endog)变量而不是X(exog)变量.

答案

from __future__ import print_function
import patsy
import numpy as np
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt
from statsmodels.regression.quantile_regression import QuantReg

data = sm.datasets.engel.load_pandas().data
data = sm.add_constant(data)

mod = QuantReg(data['foodexp'], data[['const', 'income']])
res = mod.fit(q=.5)
print(res.summary())

                         QuantReg Regression Results                          
==============================================================================
Dep. Variable:                foodexp   Pseudo R-squared:               0.6206
Model:                       QuantReg   Bandwidth:                       64.51
Method:                 Least Squares   Sparsity:                        209.3
Date:                Fri, 09 Oct 2015   No. Observations:                  235
Time:                        22:24:47   Df Residuals:                      233
                                        Df Model:                            1
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
const         81.4823     14.634      5.568      0.000        52.649   110.315
income         0.5602      0.013     42.516      0.000         0.534     0.586
==============================================================================

The condition number is large, 2.38e+03. This might indicate that there are
strong multicollinearity or other numerical problems.
Run Code Online (Sandbox Code Playgroud)

作为一个FYI,我觉得有趣的是,add_constant()只需1在数据中添加一列s.更多相关信息add_constant()可以在这里找到.