Logo

Robust Linear ModelsΒΆ

In [1]: import statsmodels.api as sm

Example for using Huber’s T norm with the default median absolute deviation scaling

In [2]: data = sm.datasets.stackloss.load()

In [3]: data.exog = sm.add_constant(data.exog)

In [4]: huber_t = sm.RLM(data.endog, data.exog, M=sm.robust.norms.HuberT())

In [5]: hub_results = huber_t.fit()

In [6]: print hub_results.params
[  0.82938433   0.92606597  -0.12784672 -41.02649835]

In [7]: print hub_results.bse
[ 0.11100521  0.30293016  0.12864961  9.79189854]

Or with the ‘H2’ covariance matrix

In [8]: hub_results2 = huber_t.fit(cov="H2")

In [9]: print hub_results2.params
[  0.82938433   0.92606597  -0.12784672 -41.02649835]

In [10]: print hub_results2.bse
[ 0.11945975  0.32235497  0.11796313  9.08950419]

Example for using Andrew’s Wave norm with Huber’s Proposal 2 scaling and ‘H3’ covariance matrix

In [11]: andrew_mod = sm.RLM(data.endog, data.exog, M=sm.robust.norms.AndrewWave())

In [12]: andrew_results = andrew_mod.fit(scale_est=sm.robust.scale.HuberScale(), cov="H3")

In [13]: print andrew_results.params
[  0.79276138   1.04857556  -0.13360865 -40.8817957 ]

In [14]: print hub_results.summary(yname='y',
   ....:              xname=['var_%d' % i for i in range(len(hub_results.params))])
   ....: 
                    Robust linear Model Regression Results                    
==============================================================================
Dep. Variable:                      y   No. Observations:                   21
Model:                            RLM   Df Residuals:                       17
Method:                          IRLS   Df Model:                            3
Norm:                          HuberT                                         
Scale Est.:                       mad                                         
Cov Type:                          H1                                         
Date:                Wed, 11 Jul 2012                                         
Time:                        19:56:14                                         
No. Iterations:                    19                                         
==============================================================================
                 coef    std err          z      P>|z|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
var_0          0.8294      0.111      7.472      0.000         0.612     1.047
var_1          0.9261      0.303      3.057      0.002         0.332     1.520
var_2         -0.1278      0.129     -0.994      0.320        -0.380     0.124
var_3        -41.0265      9.792     -4.190      0.000       -60.218   -21.835
==============================================================================

If the model instance has been used for another fit with different fit
parameters, then the fit options might not be the correct ones anymore .

This Page