使用statsmodels线性回归拟合下降趋势(负斜率)

时间:2013-11-04 22:21:06

标签: python linear-regression statsmodels

我无法在python StatsModels中获得线性回归以适应具有负斜率的数据系列 - RLM和OLS都不适合我。举一个非常简单的情况,我希望斜率为-1:

In [706]: ts12 = pandas.TimeSeries(data=[5,4,3,2,1],index=[1,2,3,4,5])
In [707]: ts12_h = sm.RLM(ts12.values, ts12.index, M=sm.robust.norms.HuberT())
In [708]: ts12_fit = ts12_h.fit()
In [710]: ts12_fit.fittedvalues
Out[710]: array([ 0.62321739,  1.24643478,  1.86965217,  2.49286956,  3.11608696])

In [729]: ts12_fit.params
Out[729]: array([ 0.62321739])

In [733]: ts12_ols = sm.OLS(ts12.values, ts12.index)
In [734]: ts12_ols_fit = ts12_ols.fit()
In [736]: ts12_ols_fit.fittedvalues
Out[736]: array([ 0.63636364,  1.27272727,  1.90909091,  2.54545455,  3.18181818])

RLM和OLS的拟合参数给出了0.6的斜率......拟合值反映了上升趋势。来自scipy的普通最小二乘回归给出了斜率为-1的预期结果:

In [737]: from scipy import stats
In [738]: stats.linregress([1,2,3,4,5], [5,4,3,2,1])
Out[738]: (-1.0, 6.0, -1.0, 1.2004217548761408e-30, 0.0)

我必须遗漏一些显而易见的东西,但通常的方法并没有发现任何东西。

1 个答案:

答案 0 :(得分:2)

默认情况下,

statsmodels不会添加常量,除非使用公式接口。

在这种情况下,您强制回归线经历零。

>>> x = sm.add_constant(ts12.index)
>>> x
array([[ 1.,  1.],
       [ 1.,  2.],
       [ 1.,  3.],
       [ 1.,  4.],
       [ 1.,  5.]])
>>> ts12_ols_fit = sm.OLS(ts12.values, ts12.index).fit()
>>> ts12_ols_fit.params
array([ 0.63636364])
>>> ts12_ols_fit = sm.OLS(ts12.values, x).fit()
>>> ts12_ols_fit.params
array([ 6., -1.])
>>> ts12_ols_fit.fittedvalues
array([ 5.,  4.,  3.,  2.,  1.])

修改

OLS参数估计可以处理完美契合。

RLM需要估算噪音。完美契合时,方差为零,RLM不起作用。

添加一点噪音RLM得到的结果基本相同。

>>> ts12_rlm_fit = sm.RLM(ts12.values+ 1e-4*np.random.randn(5), x).fit()
>>> print ts12_rlm_fit.summary()
                    Robust linear Model Regression Results                    
==============================================================================
Dep. Variable:                      y   No. Observations:                    5
Model:                            RLM   Df Residuals:                        3
Method:                          IRLS   Df Model:                            1
Norm:                          HuberT                                         
Scale Est.:                       mad                                         
Cov Type:                          H1                                         
Date:                Mon, 04 Nov 2013                                         
Time:                        20:38:00                                         
No. Iterations:                    50                                         
==============================================================================
                 coef    std err          z      P>|z|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
const          5.9999    9.8e-05   6.12e+04      0.000         6.000     6.000
x1            -1.0000   2.96e-05  -3.38e+04      0.000        -1.000    -1.000
==============================================================================