我正在尝试使用Python估计一个简单的OLS模型,并且能够获得回归结果,预测值,但是这给了我错误:
AttributeError:“ OLSResults”对象没有属性“ get_prediction”
这是我正在运行的代码-我阅读了在https://gist.github.com/josef-pkt/1417e0473c2a87e14d76b425657342f5上找到的文档和要旨,并且认为我正确地复制了它,但是显然我做错了。任何帮助将不胜感激!
import statsmodels.api as sm
import numpy as np
import pandas as pd
x = [51, 41, 52, 52, 43, 40, 50, 55, 53, 43]
y = [-13, -64, 34, -8, 3, -54, 7, 5, 27, -15]
mod = sm.OLS(y, x)
res = mod.fit()
print(res.summary())
predicted = res.predict()
pred = res.get_prediction() ## This line gives me the error
编辑:这是完整的追溯
AttributeError: 'OLSResults' object has no attribute 'get_prediction'
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-1862-8a0a76dec686> in <module>()
----> 1 pred = res.get_prediction()
/Users/chadmurphy/anaconda/lib/python3.6/site-packages/statsmodels/base/wrapper.py in __getattribute__(self, attr)
33 pass
34
---> 35 obj = getattr(results, attr)
36 data = results.model.data
37 how = self._wrap_attrs.get(attr)
AttributeError: 'OLSResults' object has no attribute 'get_prediction'
>>>
答案 0 :(得分:0)
我对statsmodels.api
不熟悉,但是在查看要点之后,我认为问题可能出在您的数据上。根据要点,X和y是通过intercept
X = sm.add_constant(X, prepend=False) # add intercept
y = sort_data.loc[:,'Production']
在拟合模型后,错误还表明get_prediction
不存在。希望您能朝正确的方向前进。