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
# The data I choose is the total retail in America during Mar81 and Dec05.
sale <- read.csv('/Users/jiahaolin/Downloads/Forecasting/week 7/tute1.csv')
retail <- ts(sale$Sales,start = c(1981,3),frequency = 12)
autoplot(retail)

# It is a seasonal data.
retail.nn <- nnetar(retail, lambda=NULL)
retail.fc <- retail.nn %>% forecast(h=36)
autoplot(retail.fc)

# Well, I don't think it is a good forecast. Let's compare with arima model. The magnitude of fluctuation is weird.
retail.arima <- auto.arima(retail)
retail.arima.fc <- retail.arima %>% forecast(h=36)
autoplot(retail)+
autolayer(retail.fc)+
autolayer(retail.arima.fc, PI=FALSE, color='red')

accuracy(retail.nn)
## ME RMSE MAE MPE MAPE MASE
## Training set -0.009101628 50.892 40.92319 -0.3000234 4.282704 0.8000445
## ACF1
## Training set 0.09004581
accuracy(retail.arima)
## ME RMSE MAE MPE MAPE MASE
## Training set 1.549629 40.8383 29.47964 0.03514132 3.078629 0.5763242
## ACF1
## Training set 0.001316496
# This result meets our expectation that arima is better. Since arima model has lower RMSE, MASE, and ACF1
retail.sim <- ts(matrix(0, nrow=36L, ncol=5L),
start=end(retail)[1L]+1L,
frequency = 12)
for(i in seq(5))
retail.sim[,i] <- simulate(retail.nn, nsim=36L)
autoplot(retail) + autolayer(retail.sim)
## For a multivariate timeseries, specify a seriesname for each timeseries. Defaulting to column names.

# Simulation of the result.