【问题标题】:Python Statsmodels QuantReg InterceptPython Statsmodels QuantReg 拦截
【发布时间】:2016-01-08 03:10:51
【问题描述】:

问题设置statsmodels Quantile Regression 问题中,他们的最小绝对偏差摘要输出显示截距。在那个例子中,他们使用了一个公式

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.

问题

如何使用Intercept不使用使用statsmodels.formula.api as smf 公式方法实现摘要输出?

【问题讨论】:

    标签: python regression statsmodels quantile


    【解决方案1】:

    当然,当我把这个问题放在一起时,我想通了。与其删除它,我会分享以防有人遇到此问题。

    正如我所怀疑的,我需要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.
    

    作为一个 FYI,我发现有趣的是 add_constant() 只是在您的数据中添加了一列 1s。更多关于add_constant()的信息可以found here

    【讨论】:

    • 常量也可以这样添加:mod = QuantReg(data['foodexp'], sm.add_constant(data.income))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    • 2012-04-01
    • 1970-01-01
    • 2013-06-25
    • 2018-08-09
    • 2014-03-10
    • 2023-03-13
    相关资源
    最近更新 更多