• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

使用python的statsmodels模块拟合ARIMA模型

武飞扬头像
PHP中文网
帮助18

导入必要包和模块

from scipy import statsimport pandas as pdimport matplotlib.pyplot as pltimport statsmodels.api as smfrom statsmodels.tsa.arima.model import ARIMAfrom statsmodels.graphics.tsaplots import plot_predict
plt.rcParams['font.sans-serif']=['simhei']#用于正常显示中文标签plt.rcParams['axes.unicode_minus']=False#用于正常显示负号

1.读取数据并画图

data=pd.read_csv('数据/客运量.csv',index_col=0)data.index = pd.Index(sm.tsa.datetools.dates_from_range('1949', '2008'))#将时间列改为专门时间格式,方便后期操作data.plot(figsize=(12,8),marker='o',color='black',ylabel='客运量')#画图

2.平稳性检验

sm.tsa.adfuller(data,regression='c')sm.tsa.adfuller(data,regression='nc')sm.tsa.adfuller(data,regression='ct')

进行三种形式的ADF单位根检验,如部分结果所示,发现序列不平稳

3.对数据作一阶差分处理

diff=data.diff(1)diff.dropna(inplace=True)diff.plot(figsize=(12,8),marker='o',color='black')#画图

作出数据一阶差分后折线图,初步判断平稳

4.对一阶差分数据进行平稳性检验

sm.tsa.adfuller(diff,regression='c')sm.tsa.adfuller(diff,regression='nc')sm.tsa.adfuller(diff,regression='ct')

如图所示,说明序列平稳

5.确定ARIMA(p,d,q)阶数

fig = plt.figure(figsize=(12,8))ax1 = fig.add_subplot(211)fig = sm.graphics.tsa.plot_acf(diff.values.squeeze(), lags=12, ax=ax1)#自相关系数图1阶截尾,决定MA(1)ax2 = fig.add_subplot(212)fig = sm.graphics.tsa.plot_pacf(diff, lags=12, ax=ax2)#偏相关系数图1阶截尾,决定AR(1)

根据自相关系数图ACF和偏自相关系数图PACF,将原始数据确定为ARIMA(1,1,1)模型

6.参数估计

model = ARIMA(data, order=(1, 1, 1)).fit()#拟合模型model.summary()#统计信息汇总#系数检验params=model.params#系数tvalues=model.tvalues#系数t值bse=model.bse#系数标准误pvalues=model.pvalues#系数p值#绘制残差序列折线图resid=model.resid#残差序列fig = plt.figure(figsize=(12,8))ax = fig.add_subplot(111)ax = model.resid.plot(ax=ax)#计算模型拟合值fit=model.predict(exog=data[['TLHYL']])

7.模型检验

#8.1.检验序列自相关sm.stats.durbin_watson(model.resid.values)#DW检验:靠近2——正常;靠近0——正自相关;靠近4——负自相关#8.2.AIC和BIC准则model.aic#模型的AIC值model.bic#模型的BIC值#8.3.残差序列正态性检验stats.normaltest(resid)#检验序列残差是否为正态分布#最终检验结果显示无法拒绝原假设,说明残差序列为正态分布,模型拟合良好#8.4.绘制残差序列自相关图和偏自相关图fig = plt.figure(figsize=(12,8))ax1 = fig.add_subplot(211)fig = sm.graphics.tsa.plot_acf(resid.values.squeeze(), lags=12, ax=ax1)ax2 = fig.add_subplot(212)fig = sm.graphics.tsa.plot_pacf(resid, lags=12, ax=ax2)#如果两图都零阶截尾,这说明模型拟合良好

8.预测

#预测至2016年的数据。由于ARIMA模型有两个参数,至少需要包含两个初始数据,因此从2006年开始预测predict = model.predict('2006', '2016', dynamic=True)print(predict)#画预测图及置信区间图fig, ax = plt.subplots(figsize=(10,8))fig = plot_predict(model, start='2002', end='2006', ax=ax)legend = ax.legend(loc='upper left')

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tangbgiie
系列文章
更多 icon
同类精品
更多 icon
继续加载