library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(fpp2)
## -- Attaching packages ---------------------------------------------- fpp2 2.4 --
## v ggplot2   3.3.3     v expsmooth 2.3  
## v fma       2.4
## 
# SARIMA(1,1,1)(1,1,1)4
set.seed(123)
ggtsdisplay(euretail)

ggtsdisplay(diff(euretail))

ggtsdisplay(diff(diff(euretail),lag=4)) # (1-B^4)*(1-B)

print(euretail,calendar = TRUE)
##        Qtr1   Qtr2   Qtr3   Qtr4
## 1996  89.13  89.52  89.88  90.12
## 1997  89.19  89.78  90.03  90.38
## 1998  90.27  90.77  91.85  92.51
## 1999  92.21  92.52  93.62  94.15
## 2000  94.69  95.34  96.04  96.30
## 2001  94.83  95.14  95.86  95.83
## 2002  95.73  96.36  96.89  97.01
## 2003  96.66  97.76  97.83  97.76
## 2004  98.17  98.55  99.31  99.44
## 2005  99.43  99.84 100.32 100.40
## 2006  99.88 100.19 100.75 101.01
## 2007 100.84 101.34 101.94 102.10
## 2008 101.56 101.48 101.13 100.34
## 2009  98.93  98.31  97.67  97.44
## 2010  96.53  96.56  96.51  96.70
## 2011  95.88  95.84  95.79  95.94
fit <- Arima(euretail,order=c(1,1,1),seasonal=c(1,1,1))
fit
## Series: euretail 
## ARIMA(1,1,1)(1,1,1)[4] 
## 
## Coefficients:
##          ar1      ma1     sar1     sma1
##       0.8836  -0.5202  -0.0286  -0.9389
## s.e.  0.1376   0.1715   0.1630   0.3874
## 
## sigma^2 estimated as 0.1549:  log likelihood=-30.1
## AIC=70.2   AICc=71.33   BIC=80.59
summary(fit)
## Series: euretail 
## ARIMA(1,1,1)(1,1,1)[4] 
## 
## Coefficients:
##          ar1      ma1     sar1     sma1
##       0.8836  -0.5202  -0.0286  -0.9389
## s.e.  0.1376   0.1715   0.1630   0.3874
## 
## sigma^2 estimated as 0.1549:  log likelihood=-30.1
## AIC=70.2   AICc=71.33   BIC=80.59
## 
## Training set error measures:
##                       ME      RMSE       MAE         MPE     MAPE      MASE
## Training set -0.02600165 0.3648673 0.2753599 -0.02429513 0.285308 0.2239913
##                     ACF1
## Training set -0.04063608
fit.aa <- auto.arima(euretail,trace=TRUE)
## 
##  ARIMA(2,1,2)(1,1,1)[4]                    : 74.62219
##  ARIMA(0,1,0)(0,1,0)[4]                    : 95.27908
##  ARIMA(1,1,0)(1,1,0)[4]                    : 77.00764
##  ARIMA(0,1,1)(0,1,1)[4]                    : 75.71724
##  ARIMA(2,1,2)(0,1,1)[4]                    : 72.20713
##  ARIMA(2,1,2)(0,1,0)[4]                    : Inf
##  ARIMA(2,1,2)(0,1,2)[4]                    : 74.77941
##  ARIMA(2,1,2)(1,1,0)[4]                    : 77.25877
##  ARIMA(2,1,2)(1,1,2)[4]                    : Inf
##  ARIMA(1,1,2)(0,1,1)[4]                    : 70.37525
##  ARIMA(1,1,2)(0,1,0)[4]                    : 83.82329
##  ARIMA(1,1,2)(1,1,1)[4]                    : 72.83265
##  ARIMA(1,1,2)(0,1,2)[4]                    : 72.84282
##  ARIMA(1,1,2)(1,1,0)[4]                    : 76.71941
##  ARIMA(1,1,2)(1,1,2)[4]                    : Inf
##  ARIMA(0,1,2)(0,1,1)[4]                    : 74.26992
##  ARIMA(1,1,1)(0,1,1)[4]                    : Inf
##  ARIMA(1,1,3)(0,1,1)[4]                    : 70.75227
##  ARIMA(0,1,3)(0,1,1)[4]                    : 68.39031
##  ARIMA(0,1,3)(0,1,0)[4]                    : 77.10225
##  ARIMA(0,1,3)(1,1,1)[4]                    : 69.89516
##  ARIMA(0,1,3)(0,1,2)[4]                    : 70.40051
##  ARIMA(0,1,3)(1,1,0)[4]                    : 72.30399
##  ARIMA(0,1,3)(1,1,2)[4]                    : Inf
## 
##  Best model: ARIMA(0,1,3)(0,1,1)[4]
autoplot(euretail,series="Data") +
  autolayer(fit$fitted,series="SARIMA(1,1,1)(1,1,1)[4]") +
  autolayer(fit.aa$fitted,series="Auto arima")

cor(data.frame(euretail,fit$fitted,fit.aa$fitted))
##                euretail fit.fitted fit.aa.fitted
## euretail      1.0000000  0.9955354     0.9955344
## fit.fitted    0.9955354  1.0000000     0.9996528
## fit.aa.fitted 0.9955344  0.9996528     1.0000000
summary(fit)
## Series: euretail 
## ARIMA(1,1,1)(1,1,1)[4] 
## 
## Coefficients:
##          ar1      ma1     sar1     sma1
##       0.8836  -0.5202  -0.0286  -0.9389
## s.e.  0.1376   0.1715   0.1630   0.3874
## 
## sigma^2 estimated as 0.1549:  log likelihood=-30.1
## AIC=70.2   AICc=71.33   BIC=80.59
## 
## Training set error measures:
##                       ME      RMSE       MAE         MPE     MAPE      MASE
## Training set -0.02600165 0.3648673 0.2753599 -0.02429513 0.285308 0.2239913
##                     ACF1
## Training set -0.04063608
summary(fit.aa)
## Series: euretail 
## ARIMA(0,1,3)(0,1,1)[4] 
## 
## Coefficients:
##          ma1     ma2     ma3     sma1
##       0.2630  0.3694  0.4200  -0.6636
## s.e.  0.1237  0.1255  0.1294   0.1545
## 
## sigma^2 estimated as 0.156:  log likelihood=-28.63
## AIC=67.26   AICc=68.39   BIC=77.65
## 
## Training set error measures:
##                       ME      RMSE       MAE         MPE      MAPE      MASE
## Training set -0.02965298 0.3661147 0.2787802 -0.02795377 0.2885545 0.2267735
##                     ACF1
## Training set 0.006455781
checkresiduals(fit)

## 
##  Ljung-Box test
## 
## data:  Residuals from ARIMA(1,1,1)(1,1,1)[4]
## Q* = 4.4884, df = 4, p-value = 0.3439
## 
## Model df: 4.   Total lags used: 8
checkresiduals(fit.aa)

## 
##  Ljung-Box test
## 
## data:  Residuals from ARIMA(0,1,3)(0,1,1)[4]
## Q* = 0.51128, df = 4, p-value = 0.9724
## 
## Model df: 4.   Total lags used: 8
library(gridExtra)
grid.arrange(autoplot(fit),autoplot(fit.aa))

# Forecasting
grid.arrange(autoplot(forecast(fit.aa)), autoplot(forecast(fit)))