本文摘自php中文网,作者coldplay.xixi,侵删。
相关免费学习推荐:python视频教程
导入必要包和模块
1 2 | 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.读取数据并画图
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= '客运量' )#画图
|
#本文所使用的客流量时间序列数据:https://download.csdn.net/download/weixin_45590329/14143811
#时间序列折线图如下所示,显然数据有递增趋势,初步判断数据不平稳
2.平稳性检验
1 | sm.tsa.adfuller(data,regression= 'c' )sm.tsa.adfuller(data,regression= 'nc' )sm.tsa.adfuller(data,regression= 'ct' )
|
进行三种形式的ADF单位根检验,如部分结果所示,发现序列不平稳
3.对数据作一阶差分处理
1 | diff=data.diff(1)diff.dropna(inplace=True)diff.plot(figsize=(12,8),marker= 'o' ,color= 'black' )#画图
|
作出数据一阶差分后折线图,初步判断平稳
4.对一阶差分数据进行平稳性检验
1 | sm.tsa.adfuller(diff,regression= 'c' )sm.tsa.adfuller(diff,regression= 'nc' )sm.tsa.adfuller(diff,regression= 'ct' )
|
如图所示,说明序列平稳
5.确定ARIMA(p,d,q)阶数
1 | 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.参数估计
1 | 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.模型检验
1 | #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.预测
1 | #预测至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' )
|
大量免费学习推荐,敬请访问python教程(视频)
以上就是介绍使用python的statsmodels模块拟合ARIMA模型的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python如何一次性输入多个数
Python如何将字母转化为数字
Python中%是什么意思
Python源程序执行的方式有什么
Python实现对文件中图片生成带标签的txt文件方法
Python对mysql数据库操作的实例详解
什么是Python中的序列化?(实例解析)
Python是面向对象还是面向过程?
Python后端是什么
Python中sqlite3的基本操作实例讲解
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » 介绍使用python的statsmodels模块拟合ARIMA模型