library(fpp2)
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.6.2
## Loading required package: forecast
## Warning: package 'forecast' was built under R version 3.6.2
## Registered S3 method overwritten by 'xts':
## method from
## as.zoo.xts zoo
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Loading required package: fma
## Loading required package: expsmooth
#I found an interesting time series data from one of the ffp2 dataset - uschange - consumption. It shows quarterly percentage changes in US consumption expenditure.
consum.ts<-ts(uschange[,'Consumption'],start=c(1970,1),frequency=4)
str(consum.ts)
## Time-Series [1:187] from 1970 to 2016: 0.616 0.46 0.877 -0.274 1.897 ...
autoplot(consum.ts)

# We can see this data does not have a seasonal pattern.
# Now let's build arima and ets models.
arima.consum<-auto.arima(consum.ts)
ets.consum<-ets(consum.ts,model='ZZZ')
summary(arima.consum)
## Series: consum.ts
## ARIMA(1,0,3)(1,0,1)[4] with non-zero mean
##
## Coefficients:
## ar1 ma1 ma2 ma3 sar1 sma1 mean
## -0.3548 0.5958 0.3437 0.4111 -0.1376 0.3834 0.7460
## s.e. 0.1592 0.1496 0.0960 0.0825 0.2117 0.1780 0.0886
##
## sigma^2 estimated as 0.3481: log likelihood=-163.34
## AIC=342.67 AICc=343.48 BIC=368.52
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.0008112515 0.5788296 0.432668 65.97513 187.9184 0.6779023
## ACF1
## Training set -0.007489122
summary(ets.consum)
## ETS(A,N,N)
##
## Call:
## ets(y = consum.ts, model = "ZZZ")
##
## Smoothing parameters:
## alpha = 0.3298
##
## Initial states:
## l = 0.6889
##
## sigma: 0.6215
##
## AIC AICc BIC
## 804.3353 804.4665 814.0287
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.0007327055 0.6181848 0.4517208 14.72506 157.9753 0.7077542
## ACF1
## Training set -0.003046691
# It choose (1,0,3) as the arima parameters, and 'ANN' as ets model. There are all consistent with our prediction - this data does not have a seasonal pattern.
# The result infers that arima model performs better, according to the lower AIC, AICc, and BIC.
#Then, we split the whole dataset into train and test data, and train our model.
consum.ts.train<-ts(consum.ts[1:(length(consum.ts)-6)],end=c(2014,4),frequency=4)
consum.ts.test<-ts(consum.ts[(length(consum.ts)-6):length(consum.ts)],start=c(2014,4),frequency=4)
train_arima<- auto.arima(consum.ts.train)
train_ets<-ets(consum.ts.train,model='ZZZ')
# Forecast the future
fc.arima<-forecast(train_arima,h=7)
fc.ets<-forecast(train_ets,h=7)
#plot the results. We can find that both models performs bad in forecasting the peak and trough, but arima model seems relatively better.
consum.ts1<-ts(consum.ts[(length(consum.ts)-60):length(consum.ts)],end=c(2016,3),frequency=4) # In this step, I rule out the time period which is useless to make the plot more understandable.
autoplot(consum.ts1)+
autolayer(fc.arima,PI=FALSE, series='auto_arima')+
autolayer(fc.ets, PI=FALSE, series='ETS')

# post the accuracy
acr.arima<-accuracy(fc.arima,consum.ts.test)
acr.ets<-accuracy(fc.ets,consum.ts.test)
acr.arima
## ME RMSE MAE MPE MAPE
## Training set 0.0008144303 0.5867672 0.4411611 68.58369 193.24065
## Test set -0.0799592743 0.2245815 0.1891320 -21.32188 31.74204
## MASE ACF1 Theil's U
## Training set 0.6780495 -0.007563896 NA
## Test set 0.2906894 -0.274729703 0.6564371
acr.ets
## ME RMSE MAE MPE MAPE
## Training set 0.001480167 0.6269126 0.4607959 15.71795 162.29630
## Test set -0.090282125 0.2149287 0.1807139 -22.83492 31.46631
## MASE ACF1 Theil's U
## Training set 0.7082275 -0.004246123 NA
## Test set 0.2777510 -0.214028685 0.611579
# It is easy to see that arima is better, since it has lower bias and variance.