#Packages used 
library(readr)
library(ggplot2)
library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(tseries)
#Loading data
#Nestle Stock <- using thee monthly adjusted closing prices from 08/01/2015-08/01/2020

Nest <- read.csv("/Users/dorothymensah/Desktop/NESTLE.csv")

DATA EXPLORATION

#Creating time series
Nest.ts <- ts(Nest$Adj.Close, frequency = 12, start = c(2015,1))
str(Nest.ts)
##  Time-Series [1:61] from 2015 to 2020: 63.7 65.1 65.9 64 64.4 ...
summary(Nest.ts)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   60.03   68.59   77.53   81.41   96.65  119.33
##Plotting time series data 
autoplot(Nest.ts)+xlab("Month")+ylab("Adjusted Monthly Close Price")

ggseasonplot(Nest.ts)

MODELS

#Model 1- ETS auto selection 

model1 <- ets(Nest.ts, model = "ZZZ") #M,N,N was chosen 
summary(model1)
## ETS(M,N,N) 
## 
## Call:
##  ets(y = Nest.ts, model = "ZZZ") 
## 
##   Smoothing parameters:
##     alpha = 0.9999 
## 
##   Initial states:
##     l = 63.5764 
## 
##   sigma:  0.0418
## 
##      AIC     AICc      BIC 
## 400.6718 401.0928 407.0044 
## 
## Training set error measures:
##                     ME     RMSE      MAE       MPE    MAPE      MASE      ACF1
## Training set 0.9140821 3.322571 2.525031 0.9504486 3.06955 0.2268723 0.1362703
#Forecast of model1 
model1.fcast <- forecast(model1,h=12) #12 months 
model1.fcast
##          Point Forecast     Lo 80    Hi 80     Lo 95    Hi 95
## Feb 2020       119.3299 112.93185 125.7279 109.54495 129.1148
## Mar 2020       119.3299 110.27820 128.3815 105.48655 133.1732
## Apr 2020       119.3299 108.23923 130.4205 102.36820 136.2915
## May 2020       119.3299 106.51800 132.1417  99.73582 138.9239
## Jun 2020       119.3299 104.99957 133.6601  97.41358 141.2461
## Jul 2020       119.3299 103.62499 135.0347  95.31134 143.3484
## Aug 2020       119.3299 102.35927 136.3004  93.37559 145.2841
## Sep 2020       119.3299 101.17962 137.4801  91.57147 147.0882
## Oct 2020       119.3299 100.07022 138.5895  89.87478 148.7849
## Nov 2020       119.3299  99.01954 139.6402  88.26791 150.3918
## Dec 2020       119.3299  98.01889 140.6408  86.73755 151.9222
## Jan 2021       119.3299  97.06153 141.5982  85.27340 153.3863
plot(forecast(model1,h=12))

hist(residuals(model1))

##Plotting the residuals of model1 
cbind('Residuals' = residuals(model1),
      'Forecast errors' = residuals(model1,type='response')) %>%
  autoplot(facet=TRUE) + xlab("Year") + ylab("")

Model1 :
I decided to utilize the autoselection feature of the ETS model. This lets R fit the best possible ETS model to the time series of my choice. Because this is a seasonal dataset. When evaluating the ETS model chosen, i focued on the effectiveness;
AIC          AICc       BIC 
400.6718  401.0928  407.0044 

Model 2- ARIMA MODEL

auto.arima(Nest.ts)
## Series: Nest.ts 
## ARIMA(0,1,0) with drift 
## 
## Coefficients:
##        drift
##       0.9275
## s.e.  0.4156
## 
## sigma^2 estimated as 10.54:  log likelihood=-155.28
## AIC=314.57   AICc=314.78   BIC=318.75
model2 <- Arima(Nest.ts, order = c(0,1,0), seasonal = c(0,1,0))
checkresiduals(model2)

## 
##  Ljung-Box test
## 
## data:  Residuals from ARIMA(0,1,0)(0,1,0)[12]
## Q* = 30.093, df = 12, p-value = 0.002704
## 
## Model df: 0.   Total lags used: 12
summary(model2)
## Series: Nest.ts 
## ARIMA(0,1,0)(0,1,0)[12] 
## 
## sigma^2 estimated as 19.82:  log likelihood=-139.79
## AIC=281.58   AICc=281.66   BIC=283.45
## 
## Training set error measures:
##                      ME     RMSE      MAE         MPE     MAPE     MASE
## Training set 0.04001831 3.949114 2.838207 0.007309302 3.383306 0.255011
##                   ACF1
## Training set 0.2337213
#Forecastign 12 months
model2.fcast <- forecast(model2,h=12)
model2.fcast
##          Point Forecast     Lo 80    Hi 80     Lo 95    Hi 95
## Feb 2020       115.4412 109.73587 121.1465 106.71566 124.1667
## Mar 2020       114.2326 106.16410 122.3012 101.89287 126.5724
## Apr 2020       111.0943 101.21240 120.9762  95.98124 126.2074
## May 2020       115.3047 103.89410 126.7154  97.85367 132.7558
## Jun 2020       117.2540 104.49654 130.0115  97.74313 136.7649
## Jul 2020       110.0904  96.11530 124.0655  88.71732 131.4635
## Aug 2020       110.1684  95.07354 125.2633  87.08280 133.2540
## Sep 2020       112.2249  96.08780 128.3620  87.54534 136.9044
## Oct 2020       117.8203 100.70437 134.9363  91.64373 143.9969
## Nov 2020       120.2303 102.18853 138.2721  92.63777 147.8229
## Dec 2020       127.6403 108.71793 146.5627  98.70101 156.5797
## Jan 2021       129.1203 109.35653 148.8841  98.89420 159.3465
#Plotting Arima model forecast 
autoplot(model2.fcast)+xlab("Month")+ylab("Adjusted Monthly Close Price")

summary(model2.fcast)
## 
## Forecast method: ARIMA(0,1,0)(0,1,0)[12]
## 
## Model Information:
## Series: Nest.ts 
## ARIMA(0,1,0)(0,1,0)[12] 
## 
## sigma^2 estimated as 19.82:  log likelihood=-139.79
## AIC=281.58   AICc=281.66   BIC=283.45
## 
## Error measures:
##                      ME     RMSE      MAE         MPE     MAPE     MASE
## Training set 0.04001831 3.949114 2.838207 0.007309302 3.383306 0.255011
##                   ACF1
## Training set 0.2337213
## 
## Forecasts:
##          Point Forecast     Lo 80    Hi 80     Lo 95    Hi 95
## Feb 2020       115.4412 109.73587 121.1465 106.71566 124.1667
## Mar 2020       114.2326 106.16410 122.3012 101.89287 126.5724
## Apr 2020       111.0943 101.21240 120.9762  95.98124 126.2074
## May 2020       115.3047 103.89410 126.7154  97.85367 132.7558
## Jun 2020       117.2540 104.49654 130.0115  97.74313 136.7649
## Jul 2020       110.0904  96.11530 124.0655  88.71732 131.4635
## Aug 2020       110.1684  95.07354 125.2633  87.08280 133.2540
## Sep 2020       112.2249  96.08780 128.3620  87.54534 136.9044
## Oct 2020       117.8203 100.70437 134.9363  91.64373 143.9969
## Nov 2020       120.2303 102.18853 138.2721  92.63777 147.8229
## Dec 2020       127.6403 108.71793 146.5627  98.70101 156.5797
## Jan 2021       129.1203 109.35653 148.8841  98.89420 159.3465
The auto.arima function chose ARIMA(0,1,0) to be the most effective model for this specific time series data.

Model 3-HOLT WINTERS

#Decided to try my hands again at forecasting with the Holt winters model as i have not been able to do so in the past. 

#Model3 <- multiplicative
model3 <- hw(Nest.ts,seasonal = "multiplicative", damped = FALSE,h=12)
summary(model3)
## 
## Forecast method: Holt-Winters' multiplicative method
## 
## Model Information:
## Holt-Winters' multiplicative method 
## 
## Call:
##  hw(y = Nest.ts, h = 12, seasonal = "multiplicative", damped = FALSE) 
## 
##   Smoothing parameters:
##     alpha = 0.5282 
##     beta  = 0.1124 
##     gamma = 3e-04 
## 
##   Initial states:
##     l = 60.7751 
##     b = 1.875 
##     s = 1.0522 1.0185 1.0048 0.9691 0.9737 0.9511
##            0.9845 0.9784 0.9697 1.0037 1.0283 1.066
## 
##   sigma:  0.0463
## 
##      AIC     AICc      BIC 
## 425.8770 440.1096 461.7619 
## 
## Error measures:
##                      ME     RMSE      MAE        MPE     MAPE      MASE
## Training set -0.1203339 3.069761 2.470688 -0.2460678 3.180286 0.2219896
##                   ACF1
## Training set 0.4855082
## 
## Forecasts:
##          Point Forecast     Lo 80    Hi 80     Lo 95    Hi 95
## Feb 2020       115.8890 109.01826 122.7596 105.38114 126.3968
## Mar 2020       114.1621 106.14284 122.1813 101.89772 126.4264
## Apr 2020       111.2998 102.08050 120.5191  97.20009 125.3996
## May 2020       113.3133 102.34852 124.2780  96.54414 130.0824
## Jun 2020       115.0419 102.17759 127.9063  95.36760 134.7163
## Jul 2020       112.1339  97.79773 126.4700  90.20863 134.0591
## Aug 2020       115.8086  99.04873 132.5684  90.17660 141.4406
## Sep 2020       116.2719  97.39562 135.1481  87.40313 145.1406
## Oct 2020       121.5941  99.62788 143.5603  87.99968 155.1885
## Nov 2020       124.3129  99.50262 149.1231  86.36887 162.2569
## Dec 2020       129.5158 101.14167 157.8900  86.12128 172.9104
## Jan 2021       132.3230 100.68323 163.9628  83.93414 180.7119
checkresiduals(model3)

## 
##  Ljung-Box test
## 
## data:  Residuals from Holt-Winters' multiplicative method
## Q* = 63.449, df = 3, p-value = 1.077e-13
## 
## Model df: 16.   Total lags used: 19
#Plotting 
autoplot(model3)+ xlab("Month") + ylab("Adjusted Monthly Close Price")

#Model4 = additive 
model4 <- hw(Nest.ts,seasonal = "additive",damped = FALSE,h=12)
summary(model4)
## 
## Forecast method: Holt-Winters' additive method
## 
## Model Information:
## Holt-Winters' additive method 
## 
## Call:
##  hw(y = Nest.ts, h = 12, seasonal = "additive", damped = FALSE) 
## 
##   Smoothing parameters:
##     alpha = 0.9995 
##     beta  = 1e-04 
##     gamma = 1e-04 
## 
##   Initial states:
##     l = 61.6949 
##     b = 0.7875 
##     s = 3.3925 2.2529 0.9135 -2.4489 -2.0405 -3.8676
##            -0.2752 -2.5435 -2.2125 0.3138 2.3743 4.1413
## 
##   sigma:  2.962
## 
##      AIC     AICc      BIC 
## 398.6838 412.9164 434.5687 
## 
## Error measures:
##                     ME     RMSE      MAE        MPE    MAPE      MASE      ACF1
## Training set 0.0900898 2.544094 2.005167 -0.0324234 2.52433 0.1801629 0.1378108
## 
## Forecasts:
##          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## Feb 2020       118.3514 114.5554 122.1474 112.5459 124.1569
## Mar 2020       117.0792 111.7118 122.4466 108.8705 125.2879
## Apr 2020       115.3410 108.7675 121.9146 105.2877 125.3944
## May 2020       115.7983 108.2077 123.3889 104.1894 127.4072
## Jun 2020       118.8540 110.3671 127.3410 105.8744 131.8337
## Jul 2020       116.0505 106.7531 125.3478 101.8314 130.2696
## Aug 2020       118.6654 108.6226 128.7083 103.3063 134.0246
## Sep 2020       119.0455 108.3087 129.7823 102.6250 135.4660
## Oct 2020       123.1962 111.8075 134.5850 105.7787 140.6138
## Nov 2020       125.3240 113.3185 137.3294 106.9632 143.6847
## Dec 2020       127.2520 114.6598 139.8441 107.9940 146.5100
## Jan 2021       128.7883 115.6355 141.9412 108.6728 148.9039
checkresiduals(model4)

## 
##  Ljung-Box test
## 
## data:  Residuals from Holt-Winters' additive method
## Q* = 26.959, df = 3, p-value = 6.004e-06
## 
## Model df: 16.   Total lags used: 19
#Plotting
autoplot(model4)+ xlab("Month") + ylab("Adjusted Monthly Close Price")

BEST MODEL (accuracy)

model1.accuracy <- accuracy(model1.fcast)
model1.accuracy
##                     ME     RMSE      MAE       MPE    MAPE      MASE      ACF1
## Training set 0.9140821 3.322571 2.525031 0.9504486 3.06955 0.2268723 0.1362703
model2.accuracy <- accuracy(model2.fcast)
model2.accuracy
##                      ME     RMSE      MAE         MPE     MAPE     MASE
## Training set 0.04001831 3.949114 2.838207 0.007309302 3.383306 0.255011
##                   ACF1
## Training set 0.2337213
model3.accuracy <- accuracy(model3)
model3.accuracy
##                      ME     RMSE      MAE        MPE     MAPE      MASE
## Training set -0.1203339 3.069761 2.470688 -0.2460678 3.180286 0.2219896
##                   ACF1
## Training set 0.4855082
model4.accuracy <- accuracy(model4)
model4.accuracy
##                     ME     RMSE      MAE        MPE    MAPE      MASE      ACF1
## Training set 0.0900898 2.544094 2.005167 -0.0324234 2.52433 0.1801629 0.1378108
#Plotting all models 

Nest.ts %>% autoplot(series='Nest.ts')+
  model1.fcast %>% autolayer(series='ETS',PI=FALSE)+  
  model2.fcast %>% autolayer(series='ARIMA',PI=FALSE)+
  model3 %>% autolayer(series='Multiplicative',PI=FALSE)+
  model4 %>% autolayer(series='Additive',PI=FALSE)

Of these 4 models, the Holt winters - additive model has the lowest RMSE, while the ETS model has the lowest ACF1. Model 4 is the best model used (comparing each model’s MSAE)