Using Pandas OLS I am able to fit and use a model as follows:
ols_test = pd.ols(y=merged2[:-1].Units, x=merged2[:-1].lastqu) #to exclude current year, then do forecast method
yrahead=(ols_test.beta['x'] * merged2.lastqu[-1:]) + ols_test.beta['intercept']
I needed to switch to statsmodels to get some additional functionality (mainly the residual plots See(question here)
So now I have:
def fit_line2(x, y):
X = sm.add_constant(x, prepend=True) #Add a column of ones to allow the calculation of the intercept
model = sm.OLS(y, X,missing='drop').fit()
"""Return slope, intercept of best fit line."""
X = sm.add_constant(x)
return model
And:
model=fit_line2(merged2[:-1].lastqu,merged2[:-1].Units)
print fit.summary()
But I cannot get
yrahead2=model.predict(merged2.lastqu[-1:])
or any variant to give me a prediction? Note that the pd.ols uses the same merged2.lastqu[-1:] to grab the data I want to 'predict" from, no matter what I put into the () for predict I'm not having any joy. It seems statsmodels wants something specific in the () other than a pandas DF cell I even tried to just put a number eg 2696 there but still nothing... My current error is
----> 3 yrahead2=model.predict(merged2.lastqu[-1:])
/usr/lib/pymodules/python2.7/statsmodels/base/model.pyc in predict(self, exog, transform, *args, **kwargs)
1004 exog = np.atleast_2d(exog) # needed in count model shape[1]
1005
-> 1006 return self.model.predict(self.params, exog, *args, **kwargs)
1007
1008
/usr/lib/pymodules/python2.7/statsmodels/regression/linear_model.pyc in predict(self, params, exog)
253 if exog is None:
254 exog = self.exog
--> 255 return np.dot(exog, params)
256
257 class GLS(RegressionModel):
ValueError: objects are not aligned
> /usr/lib/pymodules/python2.7/statsmodels/regression/linear_model.py(255)predict()
254 exog = self.exog
--> 255 return np.dot(exog, params)
256