Why do I get only one parameter from a statsmodels OLS fit
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
Just to be complete, this works:
>>> import numpy >>> import statsmodels.api as sm>>> y = numpy.array([1,2,3,4,5,6,7,8,9])>>> X = numpy.array([1,1,2,2,3,3,4,4,5])>>> X = sm.add_constant(X)>>> res_ols = sm.OLS(y, X).fit()>>> res_ols.paramsarray([-0.35714286, 1.92857143])
It does give me a different slope coefficient, but I guess that figures as we now do have an intercept.
Try this, it worked for me:
import statsmodels.formula.api as smfrom statsmodels.api import add_constantX_train = add_constant(X_train)X_test = add_constant(X_test)model = sm.OLS(y_train,X_train)results = model.fit()y_pred=results.predict(X_test)results.params