Predicting with ARIMA Models in R
Practice
The R built-in data UKDriverDeaths
contains monthly data on road casualties in the UK from 1969 to 1984. It obviously follows a seasonal ARIMA model, and finding the actual model is not very difficult.
However, performing calculations directly using the formula from the final model is quite laborious and complex. Therefore, we use the predict()
function.
- You can set how far into the future you would like to predict with the
n.ahead
option, - and also find the prediction interval using the predicted error
$pred
와 - 표준오차
$se
.
Visualization
Regardless of what is possible, writing the code to actually draw the prediction interval is also quite cumbersome, but thankfully, if you are using the TSA
package, you can simply draw the prediction interval using the plot()
function.
Here are some useful options:
n.ahead
: Like with thepredict()
function, it sets how far into the future you want to predict.n1
: It decides where to start the plot.- newxreg: Gives the data needed for time series regression analysis.
Note that the plot()
function itself also returns predicted values and prediction intervals. Unfortunately, there is no option to set the confidence level of the prediction interval, so if you want to change it, you’ll have to draw it manually using the predict()
function.
Caution
Meanwhile, inserting a model obtained from auto.arima()
directly into plot()
, instead of one from arima()
, will result in the following kind of (usually undesired) plot. This is not an error but a difference in results due to the differences between arima()
and auto.arima()
. Just knowing this fact can save you from some unnecessary work in searching for examples and re-installing packages, so it is highly recommended to remember these kinds of plot results.
Code
library(TSA)
win.graph(6,4); plot(UKDriverDeaths,main='UKDriverDeaths')
out<-arima(UKDriverDeaths,order=c(1,0,1)
,seasonal = list(order=c(1,0,1),period=12)); out
predict(out,n.ahead = 24)
win.graph(6,4); plot(out,n.ahead = 24, type='l'
,n1=1980, main='95% 예측구간')
library(forecast)
wrong<-auto.arima(UKDriverDeaths); summary(wrong)
win.graph(6,4); plot(wrong)