使用 DF 预测器进行 Pandas Statsmodels ols 回归预测? [英] Pandas Statsmodels ols regression prediction using DF predictor?
问题描述
使用 Pandas OLS,我可以按如下方式拟合和使用模型:
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']
我需要切换到 statsmodels 以获得一些额外的功能(主要是残差图 See(问题在这里)
I needed to switch to statsmodels to get some additional functionality (mainly the residual plots See(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 使用相同的 merge2.lastqu[-1:] 来获取我想要预测"的数据,无论我在 () 中放入什么来预测我都没有任何乐趣.似乎statsmodels 想要在 () 中特定的东西而不是熊猫 DF 单元格我什至试图在那里放一个数字,例如 2696 但仍然没有......我目前的错误是
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
推荐答案
您的 merged2.lastqu[-1:]
不包含常量
yrahead2=model.predict(sm.add_constant(merged2.lastqu[-1:], prepend=True))
应该可以.
另一种方法是以与模型中的 X 相同的方式将常量添加到数据框,并使用数据框的适当列 df[['const', my_other_X]]
An alternative is to add the constant to the dataframe in the same way as to the X in the model, and use the appropriate columns of the dataframe df[['const', my_other_X]]
这篇关于使用 DF 预测器进行 Pandas Statsmodels ols 回归预测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!