R(语言)做时间序列(ARIMA)

原网址:http://blog.sina.com.cn/s/blog_7f8b10ef010134z8.html

如何用R做单变量的时间序列?

---加载时间序列程序包

library(tseries)
--使用该包自带的程序,是指航空乘客的分布
air <- AirPassengers
--作这个时间序列的图,通过图作一个直观判断
ts.plot(air)
--查看自相关图
acf(air)
--查看偏相关图
pacf(air)
--初步判断
--1、有趁势。2、可能有季节性。3、应该使用MA()模型来拟合
--通过decompose 进行分解:随机、趋势、季节
x<-decompose(air)

---作图查看

plot(x)

plot(x$seasonal)

---选择合适的模型拟合
---趋势通过差分来消除
---季节性因素,确定相应的period
air.fit <- arima(air,order=c(0,1,1), seasonal=list(order=c(0,1,1), period=12))
---对结果进行诊断
tsdiag(air.fit)

--加载时间序列包
library(forecast)
--向前预测12期,默认情况下24期
--给出80%,95%置信度下的置信区间
air.forecast <- forecast(air.fit,12)
plot.forecast(air.forecast)

猜你喜欢

转载自blog.csdn.net/sunnyxidian/article/details/9749021
今日推荐