<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/smooth-package-for-r-es-function-part-v-essential-parameters/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://forecasting.svetunkov.ru/en/2017/03/05/smooth-package-for-r-es-function-part-v-essential-parameters/"> R – Modern Forecasting</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>While the previous posts on
<pre>es()</pre>
<p> function contained two parts: theory of ETS and then the implementation – this post will cover only the latter. We won’t discuss anything new, we will mainly look into several parameters that the exponential smoothing function has and what they allow us to do.</p>
<p>We start with initialisation of
<pre>es()</pre>
<p>.</p>
<p>History of exponential smoothing counts dozens of methods of initialisation. Some of them are fine, some of them are very wrong. Some of those methods allow preserving data, the others unnecessarily consume parts of time series. I have implemented ETS in a way that allows initialising it before the sample starts. So the state vector \(v_t\) discussed in parts <a target="_blank" rel="nofollow" href="http://forecasting.svetunkov.ru/en/2016/11/02/smooth-package-for-r-es-function-part-ii-pure-additive-models/">2</a> and <a target="_blank" rel="nofollow" href="http://forecasting.svetunkov.ru/en/2016/11/18/smooth-package-for-r-es-function-part-iii-multiplicative-models/">3</a>, is defined before the very first observation \(y_1\). This is consistent with Hyndman et al. 2006 approach. Still this initial value can be defined using different methods:</p>
<ol>
<li><strong>Optimisation</strong>. This means that the initial value is found along with the smoothing parameters. This can be triggered by
<pre>initial=&quot;optimal&quot;</pre>
<p> and is the default method in
<pre>es()</pre>
<p>.
<p>While the optimisation works perfectly fine on monthly data, there may be some problems with weekly and daily seasonal data. The reason for that is a high number of parameters that need to be estimated. For example, ETS(M,N,M) on weekly seasonal data will have 52 + 1 + 2 + 1 = 56 parameters to estimate (52 seasonal indices, 1 level component, 2 smoothing parameters and 1 variance of residuals). This is not an easy task which sometimes cannot be efficiently solved. That is why we may need other initialisation methods.</p>
<p>Let’s see what happens when we encounter this problem in an example. I will use time series
<pre>taylor</pre>
<p> from
<pre>forecast</pre>
<p> package. This is half-hourly electricity demand data. It has frequency of 336 (7 days * 48 half-hours) and is really hard to work with when the standard initialisation is used. Let’s see what happens when
<pre>es()</pre>
<p> is applied with model selection and a holdout of one week of data:</p>
<pre>es(taylor,&quot;ZZZ&quot;,h=336,holdout=TRUE)</pre>
<pre>Forming the pool of models based on... ANN, ANA, ANM, AAA, Estimation progress: 100%... Done! 
Time elapsed: 18.47 seconds
Model estimated: ETS(ANA)
Persistence vector g:
alpha gamma 
0.850 0.001 
Initial values were optimised.
340 parameters were estimated in the process
Residuals standard deviation: 250.546
Cost function type: MSE; Cost function value: 56999

Information criteria:
     AIC     AICc      BIC 
51642.90 51712.02 53756.01 
Forecast errors:
MPE: 1%; Bias: 50%; MAPE: 1.8%; SMAPE: 1.8%
MASE: 0.798; sMAE: 1.8%; RelMAE: 0.078; sMSE: 0.1%</pre>
<p>We had to estimate 340 parameters and the model selection took 18 seconds (checking only 5 models in the pool). We ended up with the following graph:</p>
<div class="wp-caption"><a target="_blank" rel="nofollow" href="https://i0.wp.com/forecasting.svetunkov.ru/wp-content/uploads/2017/03/taylor-es-ANA-optimal.png"><img sizes="(max-width: 300px) 100vw, 300px" alt="" class="wp-image-1120" src="https://i1.wp.com/forecasting.svetunkov.ru/wp-content/uploads/2017/03/taylor-es-ANA-optimal-300x175.png?resize=300%2C175" data-recalc-dims="1"></a>
<p class="wp-caption-text">Electricity demand series with ETS(A,N,A) initialised using optimisation and its forecast</p>
</div>
<p>The first thing that can be noticed is the initial value of level, which results in a wrong one step ahead forecast for one of the first observations. This is because of the high number of parameters – the optimiser could not find the appropriate values. This could be not important, taking number of observations in the data, but still may influence the final forecast and, what is more important, the model selection. Some of models could have been initialised slightly better than the others, which could lead to a smaller value of information criterion for those models that should not have been selected.</p>
<p>This example motivates the other initialisation mechanisms.</p>
<li><strong>Backcasting</strong>. In order to define the initial value model is fitted to data several times going forward and backwards. For example, for ETS(A,N,N) the formula used for the forward is:</li>
<p>\begin{equation} \label{eq:ETSANN_Forward}<br>
\begin{matrix}<br>
y_t = l_{t-1} + \epsilon_t \\<br>
l_t = l_{t-1} + \alpha \epsilon_t<br>
\end{matrix}<br>
\end{equation}<br>
while for the backwards it should be changed to:<br>
\begin{equation} \label{eq:ETSANN_Backward}<br>
\begin{matrix}<br>
y_t = l_{t+1} + \epsilon_t \\<br>
l_t = l_{t+1} + \alpha \epsilon_t<br>
\end{matrix}<br>
\end{equation}</p>
<p>As you see, the only thing that changes is the lower index of level component. The formula \eqref{eq:ETSANN_Forward} is used for fitting of the model to the data starting from the first observation till the end of series. When we reach the end of series, we use formula \eqref{eq:ETSANN_Backward} and move from the last observation to the very first one. Then we produce forecast back in time before the initial \(y_1\) and obtain the initial values for the components. The model is then fit to the data using these initials. This procedure can be repeated several times in order to get more accurate estimates of the initial values. In
<pre>es()</pre>
<p> it is done 3 times and can be triggered by
<pre>initial=&quot;backcasting&quot;</pre>
<p>. As mentioned above this method of initialisation is recommended for data with high seasonality frequencies (weekly, daily, hourly etc).</p>
<p>As an example, we will use the same time series from forecast package:</p>
<pre>es(taylor,&quot;ZZZ&quot;,h=336,holdout=TRUE,initial=&quot;b&quot;)</pre>
<p>This time all the process takes around 7 seconds:</p>
<pre>Forming the pool of models based on... ANN, ANA, ANM, AAA, Estimation progress: 100%... Done! 
Time elapsed: 6.81 seconds
Model estimated: ETS(MNA)
Persistence vector g:
alpha gamma 
    1     0 
Initial values were produced using backcasting.
3 parameters were estimated in the process
Residuals standard deviation: 0.007
Cost function type: MSE; Cost function value: 38238

Information criteria:
     AIC     AICc      BIC 
49493.46 49493.47 49512.11 
Forecast errors:
MPE: 0.8%; Bias: 40.6%; MAPE: 1.7%; SMAPE: 1.8%
MASE: 0.784; sMAE: 1.7%; RelMAE: 0.076; sMSE: 0.1%</pre>
<p><p>The function has checked the same pool of models and selected ETS(M,N,A) as the optimal model (estimating only 3 parameters). We ended up with a peculiar model, where smoothing parameter for the level is equal to one (meaning that we have random walk in the level) and the other parameter equal to zero (meaning that we have deterministic seasonality).</p>
<p>The graph of the model now looks reasonable:</p>
<div class="wp-caption"><a target="_blank" rel="nofollow" href="https://i0.wp.com/forecasting.svetunkov.ru/wp-content/uploads/2017/03/taylor-es-MNA-backcasting.png"><img sizes="(max-width: 300px) 100vw, 300px" alt="" class="wp-image-1123" src="https://i2.wp.com/forecasting.svetunkov.ru/wp-content/uploads/2017/03/taylor-es-MNA-backcasting-300x175.png?resize=300%2C175" data-recalc-dims="1"></a>
<p class="wp-caption-text">Electricity demand series with ETS(M,N,A) initialised using backcasting and its forecast</p>
</div>
<p>As it can be seen from the Figure, backcasting is not a bad technique and it can be useful in cases when we have a data with high frequencies. Furthermore, there is a proof that backcasting asymptotically gives the same estimates as least squares, meaning that both optimal and backcasted estimates of initial values should eventually converge to the same values.</p>
<li><strong>Arbitrary values</strong>. If for some reason we know initial values (either from a previous data or from similar data), then we can provide them to
<pre>es()</pre>
<p>. In this case we may provide two parameters:
<pre>initial</pre>
<p> and
<pre>initialSeason</pre>
<p>. The function will then use the provided values and fit the model. We can provide both of them or just one of them, meaning that the other will be estimated during the optimisation. Obviously if we deal with non-seasonal model, we don’t need
<pre>initialSeason</pre>
<p> at all. A thing to note is that we cannot use backcasting when we define parameters manually. The other important thing to note is that the model selection and combinations do not work with predefined initial values, so this method of initialisation is not available when we need to <a target="_blank" rel="nofollow" href="http://forecasting.svetunkov.ru/en/2017/01/24/smooth-package-for-r-es-function-part-iv-model-selection-and-combination-of-forecasts/">select the best model or combine several forecasts</a> using
<pre>es()</pre>
<p>.

<p>Continuing our example, we will use classical decomposition and construct ETS(M,N,M) model</p>
<pre>ourFigure &lt;- decompose(taylor,type=&quot;m&quot;)$figure
es(taylor,&quot;MNM&quot;,h=336,holdout=TRUE,initial=mean(taylor),initialSeason=ourFigure)</pre>
<pre>Time elapsed: 3.97 seconds
Model estimated: ETS(MNM)
Persistence vector g:
alpha gamma 
    1     0 
Initial values were provided by user.
340 parameters were estimated in the process
Residuals standard deviation: 0.007
Cost function type: MSE; Cost function value: 37783

Information criteria:
     AIC     AICc      BIC 
50123.24 50192.36 52236.34 
Forecast errors:
MPE: 0.7%; Bias: 35%; MAPE: 1.6%; SMAPE: 1.7%
MASE: 0.738; sMAE: 1.6%; RelMAE: 0.072; sMSE: 0.1%</pre>
<p>We can even compare results of this call with other initialisation methods:</p>
<pre>es(taylor,&quot;MNM&quot;,h=336,holdout=TRUE,initial=&quot;o&quot;)</pre>

<pre>Time elapsed: 13.13 seconds
Model estimated: ETS(MNM)
Persistence vector g:
alpha gamma 
0.919 0.000 
Initial values were optimised.
340 parameters were estimated in the process
Residuals standard deviation: 0.009
Cost function type: MSE; Cost function value: 56381

Information criteria:
     AIC     AICc      BIC 
51602.59 51671.70 53715.69 
Forecast errors:
MPE: 0.9%; Bias: 40.6%; MAPE: 1.8%; SMAPE: 1.8%
MASE: 0.791; sMAE: 1.7%; RelMAE: 0.077; sMSE: 0.1%</pre>

<pre>es(taylor,&quot;MNM&quot;,h=336,holdout=TRUE,initial=&quot;b&quot;)</pre>

<pre>Time elapsed: 6.52 seconds
Model estimated: ETS(MNM)
Persistence vector g:
alpha gamma 
    1     0 
Initial values were produced using backcasting.
3 parameters were estimated in the process
Residuals standard deviation: 0.007
Cost function type: MSE; Cost function value: 37272

Information criteria:
     AIC     AICc      BIC 
49398.88 49398.89 49417.52 
Forecast errors:
MPE: 0.8%; Bias: 33.8%; MAPE: 1.7%; SMAPE: 1.8%
MASE: 0.786; sMAE: 1.7%; RelMAE: 0.076; sMSE: 0.1%</pre>
<p><p>Estimation in this case takes approximately four seconds (on my PC), while for
<pre>initial=&quot;o&quot;</pre>
<p> it takes around 13 seconds and for
<pre>initial=&quot;b&quot;</pre>
<p> – near seven seconds. The resulting models in these cases look very similar, with the first model producing slightly more accurate forecasts.</p>
<p>This method of initialisation may be used when the other two methods for some reason do not work as expected (for example, take too much computational time) and/ or when we know the values from some reliable sources (for example, from previously fitted model to the same or similar data). It can also be used for fun and experiments with ETS. In all the other cases I would not recommend using it.</p>
<p>There are other exciting parameters in
<pre>es()</pre>
<p> that can be controlled. They allow to switch between optimised and predefined values. For example, parameter persistence accepts vector of smoothing parameters, the length of which should correspond to the number of components in the model, while parameter phi defines damping parameter. So, for example, ETS(A,Ad,N) fitted to a time series N1234 from M3 can be constructed using:</p>
<pre>es(M3$N1234$x,&quot;AAdN&quot;,h=8,persistence=c(0.2,0.1),phi=0.95)</pre>
<p>Compare the resulting graph:</p>
<div class="wp-caption"><a target="_blank" rel="nofollow" href="https://i1.wp.com/forecasting.svetunkov.ru/wp-content/uploads/2017/03/N1234-AAdN-predefined-parameters.png"><img sizes="(max-width: 300px) 100vw, 300px" alt="" class="wp-image-1127" src="https://i1.wp.com/forecasting.svetunkov.ru/wp-content/uploads/2017/03/N1234-AAdN-predefined-parameters-300x175.png?resize=300%2C175" data-recalc-dims="1"></a>
<p class="wp-caption-text">Series N1234 and ETS(A,Ad,N) fit to it with predefined parameters</p>
</div>
<p>with the one from optimised model:</p>
<pre>es(M3$N1234$x,&quot;AAdN&quot;,h=8)</pre>
<p><div class="wp-caption"><a target="_blank" rel="nofollow" href="https://i1.wp.com/forecasting.svetunkov.ru/wp-content/uploads/2017/03/N1234-AAdN-optimal.png"><img sizes="(max-width: 300px) 100vw, 300px" alt="" class="wp-image-1128" src="https://i0.wp.com/forecasting.svetunkov.ru/wp-content/uploads/2017/03/N1234-AAdN-optimal-300x175.png?resize=300%2C175" data-recalc-dims="1"></a>
<p class="wp-caption-text">Series N1234 and ETS(A,Ad,N) fit to it with optimised parameters</p>
</div>
<p>I cannot say which of them is better in means of accuracy, but if you really need to define those parameters manually (for example, when applying one and the same model to a large set of time series), then you can easily do it, and now you know how.</p>
<p>One other cool thing about
<pre>es()</pre>
<p> is that it saves all the discussed above values and returns them as a list. So you can save a model and then reuse the parameters. For example, let’s select the best model and save it:</p>
<pre>ourModel &lt;- es(M3$N1234$x,&quot;ZZZ&quot;,h=8,holdout=TRUE)</pre>
<p><p>Which gives us:</p>
<div class="wp-caption"><a target="_blank" rel="nofollow" href="https://i0.wp.com/forecasting.svetunkov.ru/wp-content/uploads/2017/03/N1234-MAN-optimal.png"><img sizes="(max-width: 300px) 100vw, 300px" alt="" class="wp-image-1130" src="https://i2.wp.com/forecasting.svetunkov.ru/wp-content/uploads/2017/03/N1234-MAN-optimal-300x175.png?resize=300%2C175" data-recalc-dims="1"></a>
<p class="wp-caption-text">Series N1234 and ETS(M,A,N) fit to it with optimised parameters</p>
</div>
<p>Now we will use a small but neat function called
<pre>model.type()</pre>
<p>, which extracts type of model, and use exactly the same model with exactly the same parameters but with a larger sample:</p>
<pre>es(M3$N1234$x,model.type(ourModel),h=8,holdout=FALSE,initial=ourModel$initial,persistence=ourModel$persistence,phi=ourModel$phi)</pre>
<p><p>This now results in the same model but with the updated states, taking into account the last ушпре observations:</p>
<div class="wp-caption"><a target="_blank" rel="nofollow" href="https://i2.wp.com/forecasting.svetunkov.ru/wp-content/uploads/2017/03/N1234-MAN-predefined.png"><img sizes="(max-width: 300px) 100vw, 300px" alt="" class="wp-image-1131" src="https://i0.wp.com/forecasting.svetunkov.ru/wp-content/uploads/2017/03/N1234-MAN-predefined-300x175.png?resize=300%2C175" data-recalc-dims="1"></a>
<p class="wp-caption-text">Series N1234 and the same ETS(M,A,N) fit to a larger sample</p>
</div>
<p>The other way to do exactly the same is just to pass
<pre>ourModel</pre>
<p> to
<pre>es()</pre>
<p> function this way:</p>
<pre>es(M3$N1234$x,model=ourModel,h=8,holdout=FALSE)</pre>
<p><p>This way we can, for example, do rolling origin with fixed parameters.</p>
<p>The function
<pre>model.type()</pre>
<p> also works with models estimated using
<pre>ets()</pre>
<p> function from
<pre>forecast</pre>
<p> package, so you can easily use
<pre>ets()</pre>
<p> and then construct the model of the same type using
<pre>es()</pre>
<p>:</p>
<pre>etsModel &lt;- ets(M3$N1234$x)
es(M3$N1234$x,model=model.type(etsModel),h=8,holdout=TRUE)</pre>
<p><p>Not sure why you would need it, but here it is. Enjoy!</p>
<p>That’s it for today. I hope that this post was helpful and now you know what you can do when you don’t have anything to do. See you next time!</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://forecasting.svetunkov.ru/en/2017/03/05/smooth-package-for-r-es-function-part-v-essential-parameters/"> R – Modern Forecasting</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/bebBgo3d5Sk"></a></div></ol>