es()
is a part of smooth package. It allows constructing Exponential Smoothing (also known as ETS), selecting the most appropriate one among 30 possible ones, including exogenous variables and many more.
In this vignette we will use data from Mcomp
package, so it is advised to install it.
Let’s load the necessary packages:
require(smooth)
require(Mcomp)
You may note that Mcomp
depends on forecast
package and if you load both forecast
and smooth
, then you will have a message that forecast()
function is masked from the environment. There is nothing to be worried about - smooth
uses this function for consistency purposes and has exactly the same original forecast()
as in the forecast
package. The inclusion of this function in smooth
was done only in order not to include forecast
in dependencies of the package.
The simplest call of this function is:
es(M3$N2457$x, h=18, holdout=TRUE)
## Time elapsed: 0.31 seconds
## Model estimated: ETS(MNN)
## Persistence vector g:
## alpha
## 0.145
## Initial values were optimised.
## 3 parameters were estimated in the process
## Residuals standard deviation: 0.413
## Cost function type: MSE; Cost function value: 1288657.07
##
## Information criteria:
## AIC AICc BIC
## 1645.978 1646.236 1653.702
## Forecast errors:
## MPE: 26.3%; Bias: 87%; MAPE: 39.8%; SMAPE: 49.4%
## MASE: 2.944; sMAE: 120.1%; RelMAE: 1.258; sMSE: 242.7%
In this case function uses branch and bound algorithm to form a pool of models to check and after that constructs a model with the lowest information criterion. As we can see, it also produces an output with brief information about the model, which contains:
holdout=TRUE
).The function has also produced a graph with actuals, fitted values and point forecasts.
If we need prediction intervals, then we run:
es(M3$N2457$x, h=18, holdout=TRUE, intervals=TRUE)
## Time elapsed: 0.25 seconds
## Model estimated: ETS(MNN)
## Persistence vector g:
## alpha
## 0.145
## Initial values were optimised.
## 3 parameters were estimated in the process
## Residuals standard deviation: 0.413
## Cost function type: MSE; Cost function value: 1288657.07
##
## Information criteria:
## AIC AICc BIC
## 1645.978 1646.236 1653.702
## 95% parametric prediction intervals were constructed
## 72% of values are in the prediction interval
## Forecast errors:
## MPE: 26.3%; Bias: 87%; MAPE: 39.8%; SMAPE: 49.4%
## MASE: 2.944; sMAE: 120.1%; RelMAE: 1.258; sMSE: 242.7%
Due to multiplicative nature of error term in the model, the intervals are asymmetric. This is the expected behaviour. The other thing to note is that the output now also provides the theoretical width of prediction intervals and its actual coverage.
If we save the model (and let’s say we want it to work silently):
ourModel <- es(M3$N2457$x, h=18, holdout=TRUE, silent="all")
we can then reuse it for different purposes:
es(M3$N2457$x, model=ourModel, h=18, holdout=FALSE, intervals="np", level=0.93)
## Time elapsed: 0.07 seconds
## Model estimated: ETS(MNN)
## Persistence vector g:
## alpha
## 0.145
## Initial values were provided by user.
## 1 parameter was estimated in the process
## 2 parameters were provided
## Residuals standard deviation: 0.431
## Cost function type: MSE; Cost function value: 1965686.226
##
## Information criteria:
## AIC AICc BIC
## 1994.861 1994.897 1997.606
## 93% nonparametric prediction intervals were constructed
We can also extract the type of model in order to reuse it later:
modelType(ourModel)
## [1] "MNN"
This handy function, by the way, also works with ets() from forecast package.
We can then use persistence or initials only from the model to construct the other one:
es(M3$N2457$x, model=modelType(ourModel), h=18, holdout=FALSE, initial=ourModel$initial, silent="graph")
## Time elapsed: 0.02 seconds
## Model estimated: ETS(MNN)
## Persistence vector g:
## alpha
## 0.151
## Initial values were provided by user.
## 2 parameters were estimated in the process
## 1 parameter was provided
## Residuals standard deviation: 0.432
## Cost function type: MSE; Cost function value: 1965400.549
##
## Information criteria:
## AIC AICc BIC
## 1996.845 1996.952 2002.334
es(M3$N2457$x, model=modelType(ourModel), h=18, holdout=FALSE, persistence=ourModel$persistence, silent="graph")
## Time elapsed: 0.02 seconds
## Model estimated: ETS(MNN)
## Persistence vector g:
## alpha
## 0.145
## Initial values were optimised.
## 2 parameters were estimated in the process
## 1 parameter was provided
## Residuals standard deviation: 0.432
## Cost function type: MSE; Cost function value: 1965686.226
##
## Information criteria:
## AIC AICc BIC
## 1996.861 1996.968 2002.351
or provide some arbitrary values:
es(M3$N2457$x, model=modelType(ourModel), h=18, holdout=FALSE, initial=1500, silent="graph")
## Time elapsed: 0.02 seconds
## Model estimated: ETS(MNN)
## Persistence vector g:
## alpha
## 0.15
## Initial values were provided by user.
## 2 parameters were estimated in the process
## 1 parameter was provided
## Residuals standard deviation: 0.433
## Cost function type: MSE; Cost function value: 1968545.705
##
## Information criteria:
## AIC AICc BIC
## 1997.029 1997.136 2002.518
Using some other parameters may lead to completely different model and forecasts:
es(M3$N2457$x, h=18, holdout=TRUE, cfType="aTMSE", bounds="a", ic="BIC", intervals=TRUE)
## Time elapsed: 0.44 seconds
## Model estimated: ETS(MNN)
## Persistence vector g:
## alpha
## 0.08
## Initial values were optimised.
## 3 parameters were estimated in the process
## Residuals standard deviation: 0.42
## Cost function type: aTMSE; Cost function value: 246.291
##
## Information criteria:
## AIC AICc BIC
## 25551.52 25556.16 25690.55
## 95% parametric prediction intervals were constructed
## 72% of values are in the prediction interval
## Forecast errors:
## MPE: 33.3%; Bias: 90.4%; MAPE: 43.3%; SMAPE: 56.3%
## MASE: 3.232; sMAE: 131.9%; RelMAE: 1.381; sMSE: 277.6%
You can play around with all the available parameters to see what’s their effect on final model.
In order to combine forecasts we need to use “C” letter:
es(M3$N2457$x, model="CCN", h=18, holdout=TRUE, silent="graph")
## Estimation progress: 10%20%30%40%50%60%70%80%90%100%... Done!
## Time elapsed: 0.68 seconds
## Model estimated: ETS(CCN)
## Initial values were optimised.
## Residuals standard deviation: 1408.59
## Cost function type: MSE
##
## Information criteria:
## Combined AICc
## 1647.651
## Forecast errors:
## MPE: 27.8%; Bias: 88.4%; MAPE: 40.5%; SMAPE: 50.8%
## MASE: 3.005; sMAE: 122.6%; RelMAE: 1.284; sMSE: 249.9%
Model selection from a specified pool and forecasts combination are called using respectively:
es(M3$N2457$x, model=c("ANN","AAN","AAdN","ANA","AAA","AAdA"), h=18, holdout=TRUE, silent="graph")
## Estimation progress: 17%33%50%67%83%100%... Done!
## Time elapsed: 0.5 seconds
## Model estimated: ETS(ANN)
## Persistence vector g:
## alpha
## 0.158
## Initial values were optimised.
## 3 parameters were estimated in the process
## Residuals standard deviation: 1439.368
## Cost function type: MSE; Cost function value: 2007704.532
##
## Information criteria:
## AIC AICc BIC
## 1688.987 1689.245 1696.711
## Forecast errors:
## MPE: 25.3%; Bias: 86%; MAPE: 39.4%; SMAPE: 48.6%
## MASE: 2.909; sMAE: 118.7%; RelMAE: 1.243; sMSE: 238.1%
es(M3$N2457$x, model=c("CCC","ANN","AAN","AAdN","ANA","AAA","AAdA"), h=18, holdout=TRUE, silent="graph")
## Estimation progress: 17%33%50%67%83%100%... Done!
## Time elapsed: 0.52 seconds
## Model estimated: ETS(CCC)
## Initial values were optimised.
## Residuals standard deviation: 1386.78
## Cost function type: MSE
##
## Information criteria:
## Combined AICc
## 1690.145
## Forecast errors:
## MPE: 17.2%; Bias: 77.7%; MAPE: 37.3%; SMAPE: 43.2%
## MASE: 2.658; sMAE: 108.5%; RelMAE: 1.136; sMSE: 206.8%
Now let’s introduce some artificial exogenous variables:
x <- cbind(rnorm(length(M3$N2457$x),50,3),rnorm(length(M3$N2457$x),100,7))
and fit a model with all the exogenous first:
es(M3$N2457$x, model="ZZZ", h=18, holdout=TRUE, xreg=x)
## Time elapsed: 0.46 seconds
## Model estimated: ETSX(MNN)
## Persistence vector g:
## alpha
## 0.144
## Initial values were optimised.
## 5 parameters were estimated in the process
## Residuals standard deviation: 0.417
## Xreg coefficients were estimated in a normal style
## Cost function type: MSE; Cost function value: 1285336.801
##
## Information criteria:
## AIC AICc BIC
## 1649.728 1650.387 1662.601
## Forecast errors:
## MPE: 26.5%; Bias: 86.6%; MAPE: 39.8%; SMAPE: 49.6%
## MASE: 2.953; sMAE: 120.5%; RelMAE: 1.261; sMSE: 244.8%
or construct a model with selected exogenous (based on IC):
es(M3$N2457$x, model="ZZZ", h=18, holdout=TRUE, xreg=x, xregDo="select")
## Time elapsed: 0.27 seconds
## Model estimated: ETS(MNN)
## Persistence vector g:
## alpha
## 0.145
## Initial values were optimised.
## 3 parameters were estimated in the process
## Residuals standard deviation: 0.415
## Cost function type: MSE; Cost function value: 1288657.07
##
## Information criteria:
## AIC AICc BIC
## 1647.978 1648.413 1658.277
## Forecast errors:
## MPE: 26.3%; Bias: 87%; MAPE: 39.8%; SMAPE: 49.4%
## MASE: 2.944; sMAE: 120.1%; RelMAE: 1.258; sMSE: 242.7%
or the one with the updated xreg:
ourModel <- es(M3$N2457$x, model="ZZZ", h=18, holdout=TRUE, xreg=x, updateX=TRUE)
If we want to check if lagged x can be used for forecasting purposes, we can use xregExpander()
function:
es(M3$N2457$x, model="ZZZ", h=18, holdout=TRUE, xreg=xregExpander(x), xregDo="select")
## Time elapsed: 0.82 seconds
## Model estimated: ETSX(MNN)
## Persistence vector g:
## alpha
## 0.153
## Initial values were optimised.
## 4 parameters were estimated in the process
## Residuals standard deviation: 0.402
## Xreg coefficients were estimated in a normal style
## Cost function type: MSE; Cost function value: 1211240.671
##
## Information criteria:
## AIC AICc BIC
## 1641.968 1642.403 1652.267
## Forecast errors:
## MPE: 25.2%; Bias: 85.8%; MAPE: 37.8%; SMAPE: 46.2%
## MASE: 2.784; sMAE: 113.6%; RelMAE: 1.189; sMSE: 217%
If we are confused about the type of estimated model, the function formula()
will help us:
formula(ourModel)
## [1] "y[t] = l[t-1] * exp(a1[t-1] * x1[t] + a2[t-1] * x2[t]) * e[t]"
A feature available since 2.1.0 is fitting ets()
model and then using its parameters in es()
:
etsModel <- forecast::ets(M3$N2457$x)
esModel <- es(M3$N2457$x, model=etsModel, h=18)
The point forecasts in the majority of cases should the same, but the prediction intervals may be different (especially if error term is multiplicative):
forecast(etsModel,h=18,level=0.95)
## Point Forecast Lo 95 Hi 95
## Aug 1992 8619.214 1215.16444 16023.26
## Sep 1992 8674.340 1086.29318 16262.39
## Oct 1992 8729.467 958.84520 16500.09
## Nov 1992 8784.593 832.69344 16736.49
## Dec 1992 8839.719 707.72264 16971.71
## Jan 1993 8894.845 583.82787 17205.86
## Feb 1993 8949.971 460.91328 17439.03
## Mar 1993 9005.097 338.89106 17671.30
## Apr 1993 9060.223 217.68056 17902.77
## May 1993 9115.349 97.20746 18133.49
## Jun 1993 9170.475 -22.59688 18363.55
## Jul 1993 9225.602 -141.79599 18593.00
## Aug 1993 9280.728 -260.44882 18821.90
## Sep 1993 9335.854 -378.61019 19050.32
## Oct 1993 9390.980 -496.33117 19278.29
## Nov 1993 9446.106 -613.65942 19505.87
## Dec 1993 9501.232 -730.63954 19733.10
## Jan 1994 9556.358 -847.31330 19960.03
forecast(esModel,h=18,level=0.95)
## Point forecast Lower bound (2.5%) Upper bound (97.5%)
## Aug 1992 8619.214 3661.450 20299.07
## Sep 1992 8674.340 3632.451 20989.49
## Oct 1992 8729.467 3716.069 21453.74
## Nov 1992 8784.593 3758.766 22442.52
## Dec 1992 8839.719 3723.987 23439.23
## Jan 1993 8894.845 3800.617 23844.60
## Feb 1993 8949.971 3845.028 24726.87
## Mar 1993 9005.097 3808.454 26227.38
## Apr 1993 9060.223 3862.929 26397.50
## May 1993 9115.349 3810.332 28467.30
## Jun 1993 9170.475 3945.801 28627.68
## Jul 1993 9225.602 4051.315 29298.88
## Aug 1993 9280.728 4027.109 30490.10
## Sep 1993 9335.854 4028.643 31563.58
## Oct 1993 9390.980 4023.093 32449.11
## Nov 1993 9446.106 4194.926 34166.21
## Dec 1993 9501.232 4190.670 34691.90
## Jan 1994 9556.358 4181.474 35819.70