AR样本预测Python Statsmodels

时间:2015-01-07 16:23:29

标签: python pandas forecasting statsmodels

我想使用训练模型中的参数来使用statsmodel预测测试模型上的值。

我的代码:

import pandas as pd
import numpy as np
import statsmodels.api as sm

#Generate data
index = pd.date_range('2000-1-1', periods=200, freq='M')
df = pd.DataFrame({'data':np.random.random(200)}, index=index)
df_train = df[df.index < df.index[100]]
df_test = df

#Set up model
mod_train = sm.tsa.AR(df_train)
res_train = mod_train.fit(max_lag=20,trend='nc')
params_train = res_train.params
mod_test = sm.tsa.AR(df_test)

#Use parameters to predict test data
mod_test.predict(params_train,start = df.index[100],dynamic=False)

错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-309-a6eb40a5ff54> in <module>()
      9 params_train = res_train.params
     10 mod_test = sm.tsa.AR(df_test)
---> 11 mod_test.predict(params_train,start = df.index[100],dynamic=False)

C:\Anaconda\lib\site-packages\statsmodels\tsa\ar_model.pyc in predict(self, params, start, end, dynamic)
    198             raise ValueError("end is before start")
    199 
--> 200         k_ar = self.k_ar
    201         k_trend = self.k_trend
    202         method = self.method

AttributeError: 'AR' object has no attribute 'k_ar'

有人可以建议一个解决方法吗?我也对其他模块开放。谢谢!

1 个答案:

答案 0 :(得分:0)

为什么不只使用res_train对象进行预测? 请参见下面适用于我的示例:

import pandas as pd
import numpy as np
import statsmodels.api as sm

#Generate data
index = pd.date_range("2000-1-1", periods=200, freq="M")
df = pd.DataFrame({"data": np.random.random(200)}, index=index)
df_train, df_test = df.iloc[:100], df.iloc[100:]

# Set up model
mod_train = sm.tsa.AR(df_train)
res_train = mod_train.fit(max_lag=20, trend="nc")
print("Lag: %d" % res_train.k_ar)
print("Coeffs: %s" % res_train.params)

res_train.predict(start=df_test.index[0], end=df_test.index[-1], dynamic=False)