使用 Pandas OLS,我能够拟合和使用如下模型:
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']
我需要切换到 statsmodels 以获得一些额外的功能(主要是残差图见(问题在这里)
所以现在我有:
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
和:
model=fit_line2(merged2[:-1].lastqu,merged2[:-1].Units)
print fit.summary()
但我无法得到
yrahead2=model.predict(merged2.lastqu[-1:])
或任何变体给我一个预测?请注意,pd.ols 使用相同的 merge2.lastqu[-1:] 来获取我想要“预测”的数据,无论我在 () 中放入什么来预测我都没有任何乐趣。似乎statsmodels 想要 () 中的特定内容,而不是 pandas DF 单元格我什至试图在其中放置一个数字,例如 2696,但仍然没有......我目前的错误是
----> 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