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. We also use some of the functions of the greybox
package.
Let’s load the necessary packages:
require(smooth)
require(greybox)
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, silent=FALSE)
## Forming the pool of models based on... ANN, ANA, AAN, Estimation progress: 100%... Done!
## Time elapsed: 0.48 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 BICc
## 1645.978 1646.236 1653.702 1654.292
## Forecast errors:
## MPE: 26.3%; sCE: -1919.1%; Bias: 87%; MAPE: 39.8%
## 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, silent=FALSE)
## Forming the pool of models based on... ANN, ANA, AAN, Estimation progress: 100%... Done!
## Time elapsed: 0.4 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 BICc
## 1645.978 1646.236 1653.702 1654.292
## 95% parametric prediction intervals were constructed
## 72% of values are in the prediction interval
## Forecast errors:
## MPE: 26.3%; sCE: -1919.1%; Bias: 87%; MAPE: 39.8%
## 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.08 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.434
## Cost function type: MSE; Cost function value: 1965686.226
##
## Information criteria:
## AIC AICc BIC BICc
## 1998.861 1999.078 2007.096 2007.609
## 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 BICc
## 1996.845 1996.952 2002.334 2002.589
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 BICc
## 1996.861 1996.968 2002.351 2002.605
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 BICc
## 1997.028 1997.136 2002.518 2002.773
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.43 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 BICc
## 815.4061 815.6641 823.1302 823.7205
## 95% parametric prediction intervals were constructed
## 72% of values are in the prediction interval
## Forecast errors:
## MPE: 33.3%; sCE: -2194.4%; Bias: 90.4%; MAPE: 43.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.7 seconds
## Model estimated: ETS(CCN)
## Initial values were optimised.
## Residuals standard deviation: 1408.59
## Cost function type: MSE
##
## Information criteria:
## (combined values)
## AIC AICc BIC BICc
## 1647.337 1647.651 1654.083 1654.546
## Forecast errors:
## MPE: 27.8%; sCE: -1977.3%; Bias: 88.4%; MAPE: 40.5%
## 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.99 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 BICc
## 1688.987 1689.245 1696.711 1697.301
## Forecast errors:
## MPE: 25.3%; sCE: -1880.4%; Bias: 86%; MAPE: 39.4%
## 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.91 seconds
## Model estimated: ETS(CCC)
## Initial values were optimised.
## Residuals standard deviation: 1386.689
## Cost function type: MSE
##
## Information criteria:
## (combined values)
## AIC AICc BIC BICc
## 1689.857 1690.146 1696.984 1697.488
## Forecast errors:
## MPE: 17.1%; sCE: -1568.3%; Bias: 77.7%; MAPE: 37.3%
## MASE: 2.658; sMAE: 108.4%; RelMAE: 1.135; sMSE: 206.7%
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.66 seconds
## Model estimated: ETSX(MNN)
## Persistence vector g:
## alpha
## 0.161
## Initial values were optimised.
## 5 parameters were estimated in the process
## Residuals standard deviation: 0.409
## Xreg coefficients were estimated in a normal style
## Cost function type: MSE; Cost function value: 1237795.928
##
## Information criteria:
## AIC AICc BIC BICc
## 1646.072 1646.731 1658.945 1660.454
## Forecast errors:
## MPE: 22.2%; sCE: -1785.5%; Bias: 83.4%; MAPE: 39.4%
## MASE: 2.862; sMAE: 116.8%; RelMAE: 1.223; sMSE: 235%
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.71 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 BICc
## 1645.978 1646.236 1653.702 1654.292
## Forecast errors:
## MPE: 26.3%; sCE: -1919.1%; Bias: 87%; MAPE: 39.8%
## 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 from greybox
package:
es(M3$N2457$x, model="ZZZ", h=18, holdout=TRUE, xreg=xregExpander(x), xregDo="select")
## Time elapsed: 1.14 seconds
## Model estimated: ETSX(MNN)
## Persistence vector g:
## alpha
## 0.153
## Initial values were optimised.
## 5 parameters were estimated in the process
## Residuals standard deviation: 0.405
## Xreg coefficients were estimated in a normal style
## Cost function type: MSE; Cost function value: 1215723.726
##
## Information criteria:
## AIC AICc BIC BICc
## 1644.326 1644.986 1657.200 1658.708
## Forecast errors:
## MPE: 28.2%; sCE: -1982.4%; Bias: 86.4%; MAPE: 41.4%
## MASE: 3.03; sMAE: 123.6%; RelMAE: 1.294; sMSE: 247.2%
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 8523.456 853.30277 16193.61
## Sep 1992 8563.040 719.69262 16406.39
## Oct 1992 8602.625 587.42532 16617.82
## Nov 1992 8642.209 456.39433 16828.02
## Dec 1992 8681.794 326.50223 17037.09
## Jan 1993 8721.379 197.65965 17245.10
## Feb 1993 8760.963 69.78442 17452.14
## Mar 1993 8800.548 -57.19924 17658.29
## Apr 1993 8840.132 -183.36139 17863.63
## May 1993 8879.717 -308.76695 18068.20
## Jun 1993 8919.302 -433.47621 18272.08
## Jul 1993 8958.886 -557.54529 18475.32
## Aug 1993 8998.471 -681.02653 18677.97
## Sep 1993 9038.055 -803.96882 18880.08
## Oct 1993 9077.640 -926.41794 19081.70
## Nov 1993 9117.225 -1048.41679 19282.87
## Dec 1993 9156.809 -1170.00570 19483.62
## Jan 1994 9196.394 -1291.22258 19684.01
forecast(esModel,h=18,level=0.95)
## Point forecast Lower bound (2.5%) Upper bound (97.5%)
## Aug 1992 8523.456 3619.902 19980.14
## Sep 1992 8563.040 3675.815 20809.41
## Oct 1992 8602.625 3653.907 21704.02
## Nov 1992 8642.209 3605.851 22533.51
## Dec 1992 8681.794 3723.961 23187.15
## Jan 1993 8721.379 3739.127 23527.88
## Feb 1993 8760.963 3746.172 24146.83
## Mar 1993 8800.548 3816.164 25091.96
## Apr 1993 8840.132 3865.369 26339.54
## May 1993 8879.717 3811.999 27252.61
## Jun 1993 8919.302 3900.096 27497.78
## Jul 1993 8958.886 3807.699 28926.73
## Aug 1993 8998.471 3964.526 29092.78
## Sep 1993 9038.055 3948.787 30655.83
## Oct 1993 9077.640 3968.640 31811.66
## Nov 1993 9117.225 3897.822 33105.81
## Dec 1993 9156.809 4002.001 33231.50
## Jan 1994 9196.394 4042.808 34916.03
Finally, if you work with M or M3 data, and need to test a function on a specific time series, you can use the following simplified call:
es(M3$N2457, intervals=TRUE, silent=FALSE)
## Forming the pool of models based on... ANN, ANA, AAN, Estimation progress: 100%... Done!
## Time elapsed: 0.4 seconds
## Model estimated: ETS(MNN)
## Persistence vector g:
## alpha
## 0.151
## Initial values were optimised.
## 3 parameters were estimated in the process
## Residuals standard deviation: 0.434
## Cost function type: MSE; Cost function value: 1965396.467
##
## Information criteria:
## AIC AICc BIC BICc
## 1998.844 1999.061 2007.079 2007.592
## 95% parametric prediction intervals were constructed
## 50% of values are in the prediction interval
## Forecast errors:
## MPE: -127.6%; sCE: 1618.3%; Bias: -92.4%; MAPE: 129.2%
## MASE: 2.278; sMAE: 93.4%; RelMAE: 1.895; sMSE: 115.4%
This command has taken the data, split it into in-sample and holdout and produced the forecast of appropriate length to the holdout.