R Language Time series Arima model method _r language

Source: Internet
Author: User
Tags diff

The principle of what Baidu a bunch of search, do not understand, first learn to use this tool.
ARIMA: All called autoregressive integral sliding average model (autoregressive integrated moving Average model, denoted Arima), is by Boxe (Box) and Jenkins (Jenkins) A famous time series prediction method was proposed in the early 70, so it is also called Box-jenkins model and Boxe-Jenkins method. Arima (P,D,Q) is called the differential autoregressive moving average model, AR is autoregressive, P is autoregressive, MA is moving average, Q is the moving average number, and D is the difference number when the time series becomes stationary. The Arima model is a model that converts a Non-stationary time series into a stationary time series, and then returns the dependent variable to its hysteresis value and the present and hysteresis values of the random error term. The Arima model is based on the stability of the original sequence and the different parts of the regression, including the moving average process (MA), the autoregressive process (AR), the autoregressive moving average process (ARMA), and the Arima process. (This is the-_-I dug from the Baidu Encyclopedia)
The steps are summarized as follows:

The #加载时间序列程序包
Library (tseries)
Library (forecast)
#使用该包自带的程序, refers to the distribution of air passengers, air
<-airpassengers
# Make a graph of this time series, make an intuitive judgment by graph
plot (air)

#也可以直接使用tsdisplay来观察, it contains a sequence diagram, as well as ACF, PACF two related graphs
tsdisplay (AIR)

#可以拆掉最后一年来做样本的训练集, the last year to do the sample test set
sair<-ts (As.vector (air[1:132)), Frequency=12,start=c (1949,1))
# You can also look at the training set that was removed. Graphics
Tsdisplay (Sair)

#sair的明显存在一个向上的趋势, using the difference method to kill, first look at the lag 1 times the graphics
S1<-diff (sair,1)
#从图上看, basically around 0 in vibration, basically stable, further use of ADF test, See if there is a unit root (verify smoothness, if there is not smooth)
adf.test (S1)
#单位根检验通过 (p<0.05, significantly reject the existence of the unit root), and then look at the difference after the graph.
Tsdisplay (S1)

#图形显示acf图存在拖尾, the Q=0,PACF figure is more than the dotted line, but in general, from the 16-order truncated, and the above line can probably see (1,8,10,12,16) The number of these five more than the dotted line, so you can test each. Formation (1,1,0) (8,1,0), 10,1,0), (12,1,0), (16,1,0), and then determine the respective AIC value, take the minimum value can be.
#且acf图存在一个12阶的季节性影响因素 and then look at the elimination of
Tsdisplay (diff (s1,12)) by drawing a 12-order lag.

#图形可以看出, has eliminated the seasonal influence, therefore the Arima season parameter middle period should be 12, because the seasonal map's ACF graph and the PACF see last one exceeds the dotted line the order to be 1, the quarter parameter may so set (1,1,1) [a] #拟合模型, look at which model is best
, note that the data used by the fitting model is the raw data, not the difference. Arima (Sair,order=c (1,1,0), Seasonal=list (Order=c (1,1,1), period=12)) #AIC =899.95 Arima (Sair,order=c (8,1,0), Seasonal=list (Order=c (1,1,1), period=12)) #AIC =907.17 Arima (Sair,order=c (10,1,0), Seasonal=list (Order=c (1,1,1), period=12) #AIC =909 Arima (Sair,order=c (12,1,0), Seasonal=list (Order=c (1,1,1), period=12)) #AIC =905.38 Arima (Sair, Order=c (16,1,0), Seasonal=list (Order=c (1,1,1), period=12)) #NaNs #从上面的拟合可以看出, select (1,1,0) (1,1,1) [12] The model AIC value is the smallest, You can also use the Auto.arima function to automatically determine these parameters: Auto.arima (Sair) #Series: Sair #ARIMA (0,1,1) (0,1,0) [A] #Coefficients: # ma1 # 0.   2263 #s. E. 0.0900 #sigma ^2 estimated as 110.5:log likelihood=-448.34 aic=900.69 aicc=900.79 bic=906.24 #auto. Arima suggests (0,1,1) (0,1,0)
[12], the AIC is 900.69, the difference is not large, you can use two of the plan to look at the results of the forecast. #先进行拟合 Fit1<-arima (Sair,order=c (1,1,0), Seasonal=list (Order=c (1,1,1), period=12)) Fit2<-arima (Sair,order=c ( 0,1,1), SeasonaL=list (Order=c (0,1,0), period=12)) #然后使用tsdiag看一下各自的结果, the figure shows that the residual standard deviation is basically between [ -1,1], the residuals of the autoregressive are 0 (two dashed lines),
The P value of the Ljung-box test is above 0.05, and the result is good.
 Tsdiag (FIT1)

Tsdiag (FIT2)

#预测
F.p1<-forecast (fit1,h=12,level=c (99.5))
F.p2<-forecast (Fit2,h=12,level=c (99.5))
# Take a look at the effect of fit1 "that is (1,1,0) (1,1,1) [12]"
plot (F.p1,ylim=c (100,700))
lines (f.p1$fitted,col= "green")
lines ( Air,col= "Red")

Effect of the #再看一下fit2 "that is (0,1,1) (0,1,0) [12]"
plot (F.p2,ylim=c (100,700))
lines (f.p2$fitted,col= "green")
lines (air,col= "Red")

#从上面两个图 (red for air data, green for the use of Sair calculated raw data fitting value, blue field prediction), you can see that there is little difference, and the prediction effect is very good.

Time series prediction, Arima model summary so far, the coefficient of AR and Ma and the coefficient of the seasonal parameters of the choice is not a clue, can only use this kind of poverty to find a way, time-consuming and laborious, However, Auto.arima seemingly very strong, that is to give the coefficient and the internet appears to be the experience of the coefficient of discrepancy, obviously is the ACF mop tail, Q should be 0, that is, AR model, the use of MA, do not understand, there is no time to carefully study its theory, so it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.