Loading Library

load("workspace.RData")
## Registered S3 methods overwritten by 'ggplot2':
##   method         from 
##   [.quosures     rlang
##   c.quosures     rlang
##   print.quosures rlang
## 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
## Registered S3 methods overwritten by 'forecast':
##   method             from    
##   fitted.fracdiff    fracdiff
##   residuals.fracdiff fracdiff
library(fpp2)
## Warning: package 'fpp2' was built under R version 3.6.1
## Loading required package: ggplot2
## Loading required package: forecast
## Warning: package 'forecast' was built under R version 3.6.1
## Loading required package: fma
## Warning: package 'fma' was built under R version 3.6.1
## Loading required package: expsmooth
## Warning: package 'expsmooth' was built under R version 3.6.1

Converting into time series

df <- read.csv("Breakfast.csv",header=TRUE)
cbf.ts <- ts(df[,"Omellette"],frequency=7)
str(cbf.ts)
##  Time-Series [1:115] from 1 to 17.3: 15 7 8 10 13 5 12 12 13 13 ...

Visualizing

autoplot(cbf.ts)+
ggtitle("Number of People Ordering Omellette") +
xlab("Week") +
ylab("Number of People")

Calculating the ACF and PCF

acf(cbf.ts)

pacf(cbf.ts)

it seems to be an AR process of the order 1.

The following Models are tried:

  1. ETS: This is a generalised model for Exponential Smoothing
  2. ARIMA:
  3. STL:STL is a versatile and robust method for decomposing time series. STL is an acronym for “Seasonal and Trend decomposition using Loess”, while Loess is a method for estimating nonlinear relationships.
  4. NNAR:Neural Network Autoregression
  5. TBATS:a combination of Fourier terms with an exponential smoothing state space model and a Box-Cox transformation.
  6. Combination: is the average of all models
train <- window(cbf.ts, end=c(12,5))
h <- length(cbf.ts) - length(train)
ETS <- forecast(ets(train), h=h)
ARIMA <- forecast(auto.arima(train, lambda=0, biasadj=TRUE),
h=h)
STL <- stlf(train, lambda=0, h=h, biasadj=TRUE)
NNAR <- forecast(nnetar(train), h=h)
TBATS <- forecast(tbats(train, biasadj=TRUE), h=h)
Combination <- (ETS[["mean"]] + ARIMA[["mean"]] +
STL[["mean"]] + NNAR[["mean"]] + TBATS[["mean"]])/5
autoplot(cbf.ts) +
autolayer(ETS, series="ETS", PI=FALSE) +
autolayer(ARIMA, series="ARIMA", PI=FALSE) +
autolayer(STL, series="STL", PI=FALSE) +
autolayer(NNAR, series="NNAR", PI=FALSE) +
autolayer(TBATS, series="TBATS", PI=FALSE) +
autolayer(Combination, series="Combination") +
xlab("week") + ylab("Number of People Ordering Omellette") +
ggtitle("Number of People Ordering Omellette")

c(ETS = accuracy(ETS, cbf.ts)["Test set","MAPE"],
ARIMA = accuracy(ARIMA, cbf.ts)["Test set","MAPE"],
`STL-ETS` = accuracy(STL, cbf.ts)["Test set","MAPE"],
NNAR = accuracy(NNAR, cbf.ts)["Test set","MAPE"],
TBATS = accuracy(TBATS, cbf.ts)["Test set","MAPE"],
Combination =
accuracy(Combination, cbf.ts)["Test set","MAPE"])
##         ETS       ARIMA     STL-ETS        NNAR       TBATS Combination 
##    33.73031    34.86782    32.32021    35.40539    34.67718    33.77340

As can be seen STL-ETS works the best with MAPE of 32.32%

Checking of demand for stationarity.

library(urca)
## Warning: package 'urca' was built under R version 3.6.1

Lets Apply the KPSS root test to see if the differencing is required

cbf.ts %>%  ur.kpss() %>% summary()
## 
## ####################### 
## # KPSS Unit Root Test # 
## ####################### 
## 
## Test is of type: mu with 4 lags. 
## 
## Value of test-statistic is: 0.4146 
## 
## Critical value for a significance level of: 
##                 10pct  5pct 2.5pct  1pct
## critical values 0.347 0.463  0.574 0.739

So the value of test statistics is 0.4146 which is more than the critical values of 10%, so differencing is required and series is not stationary.

cbf.ts %>% diff() %>% ur.kpss() %>% summary()
## 
## ####################### 
## # KPSS Unit Root Test # 
## ####################### 
## 
## Test is of type: mu with 4 lags. 
## 
## Value of test-statistic is: 0.0521 
## 
## Critical value for a significance level of: 
##                 10pct  5pct 2.5pct  1pct
## critical values 0.347 0.463  0.574 0.739

The Test statistic is less than the critical value after 1 differencing. Which means that present the series is not stationary.

save.image("workspace.Rdata")