使用熊猫OLS我能够适应和使用模型如下:Pandas统计模型ols回归预测使用DF预测?
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所以现在得到一些额外的功能(主要是残差图见(question here)
我有:
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使用相同的merged2.lastqu [-1:]来获取我想要“预测”的数据,无论我为了预测而放入()中,我都没有任何快乐。 statsmodels想比大熊猫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
谢谢,但..我用SM API的...我遇到的问题是model.predict(merged2.lastqu [-1:])这是一个看起来像日期的DF 2014-12-31 2651 名称:lastqu,dtype:float64 <<<我想使用2651作为“exog” – dartdog
没有看到如何使用公式有助于直sm? ,并且对于我已经构建的函数中的DF的用例,不知道如何设置..当然有一种方法可以预测接受DF单元吗?我只是想预测一个时期。 – dartdog
mmm有没有办法得到模型名称并重命名预测结果,如果这是问题?抓住这里 – dartdog