Question

I can't get linear regression in python StatsModels to fit a data series with a negative slope - neither RLM nor OLS are working for me. Take a very simple case where I'd expect a slope of -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])

The fitted params for both RLM and OLS give a slope of 0.6... and the fitted values reflect that with an upward trend. Ordinary least squares regression from scipy gives the expected result with a slope of -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)

I must be missing something obvious but the usual means are not turning up anything.

Was it helpful?

Solution

statsmodels doesn't add a constant by default, except when using the formula interface.

In this case you are forcing the regression line to go through zero.

>>> 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.])

edit

OLS parameter estimates can handle a perfect fit.

RLM requires a noise scale estimate. With a perfect fit, the variance is zero, and RLM doesn't work.

Adding a bit of noise RLM gets essentially the same result.

>>> 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
==============================================================================
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top