Search code examples
pythonregressionstatsmodelsrobust

Python statsmodels return values missing


I am trying to use Robust Linear Models from statsmodels on a simple test set of x-y data. However, as return values with model.params I only get one single value. How can I get slope and intercept of the fit? Minimal example (in which I'm trying to exclude the outliers from the fit, hence rlm):

import statsmodels.api as sm
a = np.array([1,2,3,4,5,6,7,8,9])
b = 2. * np.array([1,2,9,4,5,6,7,13,9])
model = sm.RLM(b, a, M=sm.robust.norms.HuberT()).fit()
model.params

The last line only returns array([2.]). I tried the same thing with ols from the same package, which does give me intercept and slope in return.


Solution

  • statsmodels is not automatically adding a constant or intercept if you use arrays. There is a helper function add_constant to add a constant.

    >>> import statsmodels.api as sm
    >>> a = np.array([1,2,3,4,5,6,7,8,9])
    >>> b = 2. * np.array([1,2,9,4,5,6,7,13,9])
    >>> model = sm.RLM(b, a, M=sm.robust.norms.HuberT()).fit()
    >>> model.params
    array([ 2.])
    

    with a constant

    >>> a2 = sm.add_constant(a)
    >>> model = sm.RLM(b, a2, M=sm.robust.norms.HuberT()).fit()
    >>> model.params
    array([  2.85893087e-10,   2.00000000e+00])
    >>> print model.summary()
    ...
    

    This is the same for all models except for some time series models which have an option to add a constant or trend.

    In the formula interface a constant is added by default.