Why do I get only one parameter from a statsmodels OLS fit

Tom picture Tom · Dec 20, 2013 · Viewed 42.5k times · Source

Here is what I am doing:

$ python
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
>>> import statsmodels.api as sm
>>> statsmodels.__version__
'0.5.0'
>>> import numpy 
>>> y = numpy.array([1,2,3,4,5,6,7,8,9])
>>> X = numpy.array([1,1,2,2,3,3,4,4,5])
>>> res_ols = sm.OLS(y, X).fit()
>>> res_ols.params
array([ 1.82352941])

I had expected an array with two elements?!? The intercept and the slope coefficient?

Answer

behzad.nouri picture behzad.nouri · Dec 20, 2013

Try this:

X = sm.add_constant(X)
sm.OLS(y,X)

as in the documentation:

An intercept is not included by default and should be added by the user

statsmodels.tools.tools.add_constant