es() - Exponential Smoothing

Ivan Svetunkov

2020-12-08

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.22 seconds
## Model estimated: ETS(MNN)
## Persistence vector g:
##  alpha 
## 0.2146 
## Initial values were optimised.
## 
## Loss function type: MSE; Loss function value: 0.2231
## Error standard deviation: 0.4218
## Sample size: 97
## Number of estimated parameters: 3
## Number of degrees of freedom: 94
## Information criteria:
##      AIC     AICc      BIC     BICc 
## 1679.080 1679.338 1686.804 1687.395 
## 
## Forecast errors:
## MPE: 21.7%; sCE: 1738.9%; Bias: 83.2%; MAPE: 38.1%
## MASE: 2.78; sMAE: 113.4%; sMSE: 222.3%; rMAE: 1.188; rRMSE: 1.308

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:

  1. How much time was elapsed for the model construction;
  2. What type of ETS was selected;
  3. Values of persistence vector (smoothing parameters);
  4. What type of initialisation was used;
  5. How many parameters were estimated (standard deviation is included);
  6. Standard deviation of residuals. The model has multiplicative error term, so as a result the standard deviation is small.
  7. Cost function type and the value of that cost function;
  8. Information criteria for this model;
  9. Forecast errors (because we have set holdout=TRUE).

The function has also produced a graph with actual values, fitted values and point forecasts.

If we need prediction interval, then we run:

es(M3$N2457$x, h=18, holdout=TRUE, interval=TRUE, silent=FALSE)
## Forming the pool of models based on... ANN, ANA, AAN, Estimation progress:    100%... Done!
## Time elapsed: 0.21 seconds
## Model estimated: ETS(MNN)
## Persistence vector g:
##  alpha 
## 0.2146 
## Initial values were optimised.
## 
## Loss function type: MSE; Loss function value: 0.2231
## Error standard deviation: 0.4218
## Sample size: 97
## Number of estimated parameters: 3
## Number of degrees of freedom: 94
## Information criteria:
##      AIC     AICc      BIC     BICc 
## 1679.080 1679.338 1686.804 1687.395 
## 
## 95% parametric prediction interval was constructed
## 94% of values are in the prediction interval
## Forecast errors:
## MPE: 21.7%; sCE: 1738.9%; Bias: 83.2%; MAPE: 38.1%
## MASE: 2.78; sMAE: 113.4%; sMSE: 222.3%; rMAE: 1.188; rRMSE: 1.308

Due to multiplicative nature of error term in the model, the interval are asymmetric. This is the expected behaviour. The other thing to note is that the output now also provides the theoretical width of prediction interval 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, interval="np", level=0.93)
## Time elapsed: 0.03 seconds
## Model estimated: ETS(MNN)
## Persistence vector g:
##  alpha 
## 0.2146 
## Initial values were provided by user.
## 
## Loss function type: MSE; Loss function value: 0.2348
## Error standard deviation: 0.4377
## Sample size: 115
## Number of estimated parameters: 3
## Number of provided parameters: 2
## Number of degrees of freedom: 112
## Information criteria:
##      AIC     AICc      BIC     BICc 
## 161.7243 161.7597 164.4693 164.5532 
## 
## 93% nonparametric prediction interval was 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.

If we need actual values from the model, we can use actuals() method from greybox package:

actuals(ourModel)
##         Jan    Feb    Mar    Apr    May    Jun    Jul    Aug    Sep    Oct
## 1983 2158.1 1086.4 1154.7 1125.6  920.0 2188.6  829.2 1353.1  947.2 1816.8
## 1984 1783.3 1713.1 3479.7 2429.4 3074.3 3427.4 2783.7 1968.7 2045.6 1471.3
## 1985 1821.0 2409.8 3485.8 3289.2 3048.3 2914.1 2173.9 3018.4 2200.1 6844.3
## 1986 3238.9 3252.2 3278.8 1766.8 3572.8 3467.6 7464.7 2748.4 5126.7 2870.8
## 1987 3220.7 3586.0 3249.5 3222.5 2488.5 3332.4 2036.1 1968.2 2967.2 3151.6
## 1988 3894.1 4625.5 3291.7 3065.6 2316.5 2453.4 4582.8 2291.2 3555.5 1785.0
## 1989 2102.9 2307.7 6242.1 6170.5 1863.5 6318.9 3992.8 3435.1 1585.8 2106.8
## 1990 6168.0 7247.4 3579.7 6365.2 4658.9 6911.8 2143.7 5973.9 4017.2 4473.0
## 1991 8749.1                                                               
##         Nov    Dec
## 1983 1624.5  868.5
## 1984 2763.7 2328.4
## 1985 4160.4 1548.8
## 1986 2170.2 4326.8
## 1987 1610.5 3985.0
## 1988 2020.0 2026.8
## 1989 1892.1 4310.6
## 1990 3591.9 4676.5
## 1991

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.01 seconds
## Model estimated: ETS(MNN)
## Persistence vector g:
##  alpha 
## 0.3167 
## Initial values were provided by user.
## 
## Loss function type: MSE; Loss function value: 0.2307
## Error standard deviation: 0.4444
## Sample size: 115
## Number of estimated parameters: 2
## Number of provided parameters: 1
## Number of degrees of freedom: 113
## Information criteria:
##      AIC     AICc      BIC     BICc 
## 2028.839 2028.946 2034.329 2034.583
es(M3$N2457$x, model=modelType(ourModel), h=18, holdout=FALSE, persistence=ourModel$persistence, silent="graph")
## Time elapsed: 0.01 seconds
## Model estimated: ETS(MNN)
## Persistence vector g:
##  alpha 
## 0.2146 
## Initial values were optimised.
## 
## Loss function type: MSE; Loss function value: 0.2348
## Error standard deviation: 0.4396
## Sample size: 115
## Number of estimated parameters: 2
## Number of provided parameters: 1
## Number of degrees of freedom: 113
## Information criteria:
##      AIC     AICc      BIC     BICc 
## 2028.689 2028.796 2034.178 2034.433

or provide some arbitrary values:

es(M3$N2457$x, model=modelType(ourModel), h=18, holdout=FALSE, initial=1500, silent="graph")
## Time elapsed: 0.01 seconds
## Model estimated: ETS(MNN)
## Persistence vector g:
## alpha 
##  0.32 
## Initial values were provided by user.
## 
## Loss function type: MSE; Loss function value: 0.2321
## Error standard deviation: 0.4418
## Sample size: 115
## Number of estimated parameters: 2
## Number of provided parameters: 1
## Number of degrees of freedom: 113
## Information criteria:
##      AIC     AICc      BIC     BICc 
## 2027.504 2027.611 2032.994 2033.248

Using some other parameters may lead to completely different model and forecasts:

es(M3$N2457$x, h=18, holdout=TRUE, loss="aTMSE", bounds="a", ic="BIC", interval=TRUE)
## Time elapsed: 0.21 seconds
## Model estimated: ETS(ANN)
## Persistence vector g:
##  alpha 
## 0.0798 
## Initial values were optimised.
## 
## Loss function type: aTMSE; Loss function value: 39565651.9
## Error standard deviation: 1466.912
## Sample size: 97
## Number of estimated parameters: 3
## Number of degrees of freedom: 94
## Information criteria:
##      AIC     AICc      BIC     BICc 
## 1972.365 1974.736 1985.865 1986.455 
## 
## 95% parametric prediction interval was constructed
## 44% of values are in the prediction interval
## Forecast errors:
## MPE: 33.4%; sCE: 2196.8%; Bias: 90.4%; MAPE: 43.4%
## MASE: 3.235; sMAE: 132%; sMSE: 278%; rMAE: 1.382; rRMSE: 1.463

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.43 seconds
## Model estimated: ETS(CCN)
## Initial values were optimised.
## 
## Loss function type: MSE
## Error standard deviation: 1398.833
## Sample size: 97
## Information criteria:
## (combined values)
##      AIC     AICc      BIC     BICc 
## 1679.860 1680.184 1687.907 1688.448 
## 
## Forecast errors:
## MPE: 20.7%; sCE: 1705.2%; Bias: 82.4%; MAPE: 38%
## MASE: 2.759; sMAE: 112.5%; sMSE: 219.6%; rMAE: 1.178; rRMSE: 1.3

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.41 seconds
## Model estimated: ETS(ANN)
## Persistence vector g:
##  alpha 
## 0.1582 
## Initial values were optimised.
## 
## Loss function type: MSE; Loss function value: 2007704.5316
## Error standard deviation: 1439.368
## Sample size: 97
## Number of estimated parameters: 3
## Number of degrees of freedom: 94
## 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%; sMSE: 238.1%; rMAE: 1.243; rRMSE: 1.354
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.41 seconds
## Model estimated: ETS(CCC)
## Initial values were optimised.
## 
## Loss function type: MSE
## Error standard deviation: 1386.675
## Sample size: 97
## Information criteria:
## (combined values)
##      AIC     AICc      BIC     BICc 
## 1689.864 1690.147 1696.984 1697.488 
## 
## Forecast errors:
## MPE: 17.1%; sCE: 1568.3%; Bias: 77.7%; MAPE: 37.3%
## MASE: 2.658; sMAE: 108.4%; sMSE: 206.7%; rMAE: 1.135; rRMSE: 1.261

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.32 seconds
## Model estimated: ETSX(MNN)
## Persistence vector g:
##  alpha 
## 0.2077 
## Initial values were optimised.
## Xreg coefficients were estimated in a normal style
## 
## Loss function type: MSE; Loss function value: 0.2214
## Error standard deviation: 0.4268
## Sample size: 97
## Number of estimated parameters: 7
## Number of degrees of freedom: 90
## Information criteria:
##      AIC     AICc      BIC     BICc 
## 1682.838 1683.498 1695.712 1697.220 
## 
## Forecast errors:
## MPE: 22.8%; sCE: 1782.4%; Bias: 84.2%; MAPE: 38.1%
## MASE: 2.81; sMAE: 114.7%; sMSE: 229.9%; rMAE: 1.201; rRMSE: 1.33

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.78 seconds
## Model estimated: ETS(MNN)
## Persistence vector g:
##  alpha 
## 0.2146 
## Initial values were optimised.
## 
## Loss function type: MSE; Loss function value: 0.2231
## Error standard deviation: 0.4218
## Sample size: 97
## Number of estimated parameters: 3
## Number of degrees of freedom: 94
## Information criteria:
##      AIC     AICc      BIC     BICc 
## 1679.080 1679.338 1686.804 1687.395 
## 
## Forecast errors:
## MPE: 21.7%; sCE: 1738.9%; Bias: 83.2%; MAPE: 38.1%
## MASE: 2.78; sMAE: 113.4%; sMSE: 222.3%; rMAE: 1.188; rRMSE: 1.308

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.39 seconds
## Model estimated: ETSX(MNN)
## Persistence vector g:
##  alpha 
## 0.2196 
## Initial values were optimised.
## Xreg coefficients were estimated in a normal style
## 
## Loss function type: MSE; Loss function value: 0.2178
## Error standard deviation: 0.4238
## Sample size: 97
## Number of estimated parameters: 5
## Number of degrees of freedom: 92
## Information criteria:
##      AIC     AICc      BIC     BICc 
## 1678.802 1679.236 1689.100 1690.095 
## 
## Forecast errors:
## MPE: 19.2%; sCE: 1634.7%; Bias: 79%; MAPE: 37%
## MASE: 2.697; sMAE: 110%; sMSE: 214.1%; rMAE: 1.152; rRMSE: 1.284

If we are confused about the type of estimated model, the function formula() will help us:

formula(ourModel)
## [1] "y[t] = l[t-1] * 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 interval 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       9351.357           3657.047            19817.58
## Sep 1992       9559.479           3682.623            20701.03
## Oct 1992       9749.117           3688.749            21224.15
## Nov 1992       9971.425           3694.740            21930.44
## Dec 1992      10165.921           3721.709            22584.13
## Jan 1993      10412.779           3749.593            23379.55
## Feb 1993      10632.588           3761.134            24217.59
## Mar 1993      10836.208           3799.723            24896.75
## Apr 1993      11040.344           3803.535            25654.38
## May 1993      11297.872           3823.643            26559.38
## Jun 1993      11538.860           3870.391            27278.52
## Jul 1993      11801.310           3911.847            28233.26
## Aug 1993      12029.708           3917.220            28992.66
## Sep 1993      12271.229           3941.575            30061.35
## Oct 1993      12541.829           3989.167            30708.16
## Nov 1993      12789.072           4011.796            31615.07
## Dec 1993      13073.160           4043.443            32685.09
## Jan 1994      13316.293           4074.353            33407.82

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, interval=TRUE, silent=FALSE)
## Forming the pool of models based on... ANN, ANA, AAN, Estimation progress:    40%50%60%70%80%90%100%... Done!
## Time elapsed: 0.61 seconds
## Model estimated: ETS(MMN)
## Persistence vector g:
## alpha  beta 
## 0.177 0.000 
## Initial values were optimised.
## 
## Loss function type: MSE; Loss function value: 0.1586
## Error standard deviation: 0.5113
## Sample size: 115
## Number of estimated parameters: 4
## Number of provided parameters: 1
## Number of degrees of freedom: 111
## Information criteria:
##      AIC     AICc      BIC     BICc 
## 2044.478 2044.841 2055.458 2056.320 
## 
## 95% parametric prediction interval was constructed
## 33% of values are in the prediction interval
## Forecast errors:
## MPE: -404.6%; sCE: -5567.4%; Bias: -100%; MAPE: 404.6%
## MASE: 7.542; sMAE: 309.3%; sMSE: 1167%; rMAE: 6.274; rRMSE: 5.044

This command has taken the data, split it into in-sample and holdout and produced the forecast of appropriate length to the holdout.