Friday, February 3, 2017

ARIMA examples

To better understand specifics of different ARIMA models (incl. some interesting prototypes), I fitted the models on sample time series. I chose a temperature time series measured hourly, where it is reasonable to assume both - cycle and drift (in this sense the temperature series is not much different from daily measured series of current account balances).


The predicted mean and 99% interval is illustrated. Models with zero differencing have constant for level, models with one differencing (ordinary or seasonal) have constant for drift and models with two differencings (ordinary or seasonal or mixed) do not have constant as the trend is modeled as random here.


It was generated in R:


library("zoo")
library("forecast")

d<-read.csv(file="Temperature.csv",sep=";")
y<-ts(d$Temperature,frequency = 24)
cut=13 # division between in and out sample
h=8 # forecast horizon
fitModel<-function(p,d,q,P,D,Q,title)
{
insample=window(y,start=c(1,1),end=c(cut,frequency(y)))
outsample=window(y,start=c(cut+1,1),end=c(cut+h,frequency(y)))
# in sample fit
fitIn <- Arima(insample,order=c(p,d,q),seasonal=c(P,D,Q),include.constant = T)
print(fitIn)
plot(forecast(fitIn,h=h*frequency(y)),main=paste(title,": (",paste(p,d,q,P,D,Q,sep=","),")"),ylim=c(-20,20),cex.main=.9)
lines(outsample,col="red",lt=1)+abline(h=c(-10,0,10),col="darkgreen",lt=2)
#lines(fitted(fitIn),col="green",lw=1)
# out of sample accuracy
#fitOut <- Arima(outsample,model=fitIn)
#accuracy(fitIn) # out-of-sample one-step forecasts.
#accuracy(fitOut) # out-of-sample multi-step forecasts
#print(fitIn)
#print(fitOut)
}
par (mfrow=c(2,2))
fitModel(0,0,0,0,0,0,"white noise")
fitModel(0,1,0,0,0,0,"random walk")
fitModel(1,1,0,0,0,0,"differenced autoregressive")
fitModel(5,1,0,0,0,0,"differenced autoregressive")
fitModel(0,1,1,0,0,0,"differenced moving average (exponential smoothing)")
fitModel(0,1,5,0,0,0,"differenced moving average")
fitModel(0,2,2,0,0,0,"linear exponential smoothing")
fitModel(1,1,2,0,0,0,"damped linear exponential smoothing")
fitModel(0,0,0,1,1,0,"seasonal differenced autoregressive")
fitModel(1,0,0,1,1,0,"seasonal differenced autoregressive")
fitModel(0,0,0,0,1,1,"seasonal differenced moving average")
fitModel(0,0,1,0,1,1,"seasonal differenced moving average")
fitModel(1,1,0,0,1,0,"seasonal random trend autoregressive")
fitModel(1,1,0,1,1,0,"seasonal random trend autoregressive")
fitModel(0,1,1,0,1,0,"seasonal random trend moving average")
fitModel(0,1,1,0,1,1,"seasonal random trend moving average")
par (mfrow=c(1,1))

No comments:

Post a Comment