构建ARIMA模型,利用R里面的auto.arima()函数,自动筛选p、d、q

构建ARIMA模型,利用R里面的auto.arima()函数,自动筛选p、d、q

# 构建ARIMA模型##################################
library(forecast)
library(tseries)
library(fUnitRoots)
terrorism_num=c(1534,1295,1183,1277,1316,1168,1263,1290,1107,1269,1172,1091, 
                1162,1153,1145,1120,1353,1156,1114,1162,1045,1140,1114,923,
                879,879,961,865,1081,1077,994,968,838,805,804,749 )


# 时间序列(使用ts()函数进行时间序列转化)
terr_num=ts(terrorism_num)

# 绘图
opar<-par(mfrow=c(2,1))
plot(terr_num,type="l",xlab="时间",ylab="恐怖袭击次数",main="恐怖袭击次数随时间分布")
acf(terr_num,main="自相关图",xlab="滞后阶数")
par(opar)

# R里面的auto.arima()函数就是通过选取AIC和BIC最小来选取模型的
p_d_q=auto.arima(terr_num)
p_d_q
# Series: terr_num 
# ARIMA(2,1,0) with drift 

# method 	模型参数估计的方法,“ML”极大似然估计,”CSS”条件最小二乘估计,“CSS-ML”
model2<-arima(terr_num,order=c(2,1,0),method="ML")
model2
accuracy(model2)
# 模型评价###############################
qqnorm(model2$residuals)
qqline(model2$residuals)
Box.test(model2$residuals,type="Ljung-Box")

# 从画出的QQ图和LB检验的结果来看,残差符合正态性假设且不相关,
# 则认为模型拟合数据比较充分,可以用来进行下一步预测。

# 预测###################################
forecast=forecast(model2, h=3, level = c(99.5))
forecast
plot(forecast(model2, h=3, level = c(99.5)),xlab="时间",ylab="恐怖袭击次数")

# Point    Forecast  Lo 99.5  Hi 99.5
# 37       772.2535 483.5940 1060.913
# 38       780.2674 446.1244 1114.410
# 39       769.4389 412.4901 1126.388

你可能感兴趣的:(数据挖掘&R语言)