<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Archives Common parameters - Open Forecasting</title>
	<atom:link href="https://openforecast.org/category/r-en/smooth/general/feed/" rel="self" type="application/rss+xml" />
	<link>https://openforecast.org/category/r-en/smooth/general/</link>
	<description>How to look into the future</description>
	<lastBuildDate>Tue, 31 Mar 2020 15:49:30 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2015/08/cropped-usd-05-32x32.png&amp;nocache=1</url>
	<title>Archives Common parameters - Open Forecasting</title>
	<link>https://openforecast.org/category/r-en/smooth/general/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>&#8220;smooth&#8221; package for R. Common ground. Part IV. Exogenous variables. Advanced stuff</title>
		<link>https://openforecast.org/2018/02/10/smooth-package-for-r-common-ground-part-iv-exogenous-variables-advanced-stuff/</link>
					<comments>https://openforecast.org/2018/02/10/smooth-package-for-r-common-ground-part-iv-exogenous-variables-advanced-stuff/#respond</comments>
		
		<dc:creator><![CDATA[Ivan Svetunkov]]></dc:creator>
		<pubDate>Sat, 10 Feb 2018 15:51:33 +0000</pubDate>
				<category><![CDATA[Common parameters]]></category>
		<category><![CDATA[Package smooth for R]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Univariate models]]></category>
		<category><![CDATA[smooth]]></category>
		<guid isPermaLink="false">https://openforecast.org/?p=1645</guid>

					<description><![CDATA[<p>Previously we’ve covered the basics of exogenous variables in smooth functions. Today we will go slightly crazy and discuss automatic variables selection. But before we do that, we need to look at a Santa’s little helper function implemented in smooth. It is called xregExpander(). It is useful in cases when you think that your exogenous [&#8230;]</p>
<p>Message <a href="https://openforecast.org/2018/02/10/smooth-package-for-r-common-ground-part-iv-exogenous-variables-advanced-stuff/">&#8220;smooth&#8221; package for R. Common ground. Part IV. Exogenous variables. Advanced stuff</a> first appeared on <a href="https://openforecast.org">Open Forecasting</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Previously we’ve covered the basics of exogenous variables in smooth functions. Today we will go slightly crazy and discuss automatic variables selection. But before we do that, we need to look at a Santa’s little helper function implemented in <span class="lang:r decode:true crayon-inline">smooth</span>. It is called <span class="lang:r decode:true crayon-inline">xregExpander()</span>. It is useful in cases when you think that your exogenous variable may influence the variable of interest either via some lag or lead. Let’s say that we think that BJsales.lead discussed in the <a href="/en/2018/01/15/smooth-package-for-r-common-ground-part-iii-exogenous-variables-basic-stuff/">previous post</a> influences sales in a non-standard way: for example, we believe that today’s value of sales is driven by the several values of that variable: the value today, five and 10 days ago. This means that we need to include <span class="lang:r decode:true crayon-inline">BJsales.lead</span> with lags. And <span class="lang:r decode:true crayon-inline">xregExpander()</span> from the <span class="lang:r decode:true crayon-inline">greybox</span> package allows automating this for us:</p>
<pre class="decode">library("greybox")
newXreg <- xregExpander(BJsales.lead, lags=c(-5,-10))</pre>
<p>The <span class="lang:r decode:true crayon-inline">newXreg</span> is a matrix, which contains the original data, the data with lag 5 and the data with lag 10. However, if we just move the original data several observations ahead or backwards, we will have missing values, so <span class="lang:r decode:true crayon-inline">xregExpander()</span> fills in those values with the forecasts using <span class="lang:r decode:true crayon-inline">es()</span> and <span class="lang:r decode:true crayon-inline">iss()</span> functions (depending on the type of variable we are dealing with). This also means that in cases of binary variables you may have weird averaged values as forecasts (e.g. 0.7812), so beware and look at the produced matrix. Maybe in your case it makes sense to just substitute these weird numbers with zeroes...</p>
<p>You may also need leads instead of lags. This is regulated with the same "lags" parameter but with positive values:</p>
<pre class="decode">newXreg <- xregExpander(BJsales.lead, lags=c(7,-5,-10))</pre>
<p>Once again, the values are shifted, and now the first 7 values are backcasted.</p>
<p>After expanding the exogenous variables, we can use them in <span class="lang:r decode:true crayon-inline">smooth</span> functions for forecasting purposes. All the rules discussed in <a href="/en/2018/01/15/smooth-package-for-r-common-ground-part-iii-exogenous-variables-basic-stuff/">the previous post</a> apply:</p>
<pre class="decode">es(BJsales, "XXN", xreg=newXreg, h=10, holdout=TRUE)</pre>
<p>But what should we do if we have several variables and we are not sure what lags and leads to select? This may become a complicated task which can have several possible solutions. <span class="lang:r decode:true crayon-inline">smooth</span> functions provide one. I should warn you that this is not necessarily the best solution, but <strong>a</strong> solution. There is a function called <span class="lang:r decode:true crayon-inline">stepwise()</span> in <span class="lang:r decode:true crayon-inline">greybox</span> that does the selection based on an information criterion and partial correlations. In order to run this function the response variable needs to be in the first column. The idea of the function is simple, it works iteratively the following way:</p>
<ol>
<li>The basic model of the first variable and the constant is constructed (this corresponds to simple mean). An information criterion is calculated;</li>
<li>The correlations of the residuals of the model with all the original exogenous variables are calculated;</li>
<li>The regression model of the response variable and all the variables in the previous model plus the new most correlated variable from (2) is constructed using <span class="lang:r decode:true crayon-inline">lm()</span> function;</li>
<li>An information criterion is calculated and is compared with the one from the previous model. If it is greater or equal to the previous one, then we stop and use the previous model. Otherwise we go to step 2.</li>
</ol>
<p>This way we do not do a blind search, but we follow some sort of "trace" of a good model: if some significant part that can be explained by one of the exogenous variables is left in the residuals, then that variable is included in the model. Following correlations makes sure that we include only meaningful (from technical point of view) things in the model. Using information criteria allows overcoming the problem with the uncertainty of statistical tests. In general the function guarantees that you will have the model with the lowest information criterion. However this does not guarantee that you will end up with a meaningful model or with a model that produces the most accurate forecasts. And this is why evolution has granted human beings the almighty brain – it helps in selecting the most appropriate model in those cases, when statistics fails.</p>
<p>Let’s see how the function works with the same example and 1 to 10 leads and 1 to 10 lags (so we have 21 variables now). First we expand the data and form the matrix with all the variables:</p>
<pre class="decode">newXreg <- as.data.frame(xregExpander(BJsales.lead,lags=c(-10:10)))
newXreg <- cbind(as.matrix(BJsales),newXreg)
colnames(newXreg)[1] <- "y"</pre>
<p>This way we have a nice data frame with nice names, not something weird with strange long names. It is important to note that the response variable should be in the first column of the resulting matrix. After that we use our magical stepwise function:</p>
<pre class="decode">ourModel <- stepwise(newXreg)</pre>
<p>And here’s what it returns (the object of class “lm”):</p>
<pre>Call:
lm(formula = y ~ xLag4 + xLag9 + xLag3 + xLag10 + xLag5 + xLag6 + 
    xLead9 + xLag7 + xLag8, data = newXreg)

Coefficients:
(Intercept)        xLag4        xLag9        xLag3       xLag10        xLag5        xLag6  
    17.6448       3.3712       1.3724       4.6781       1.5412       2.3213       1.7075  
     xLead9        xLag7        xLag8  
     0.3767       1.4025       1.3370</pre>
<p>The values in the function are listed in the order of most correlated with the response variable to the least correlated ones. The function works very fast because it does not need to go through all the variables and their combinations in the dataset.</p>
<p>Okay. So, that’s the second nice function in <span class="lang:r decode:true crayon-inline">smooth</span> that you can use for exogenous variables. But what does it have to do with the main forecasting functions?</p>
<p>The thing is <span class="lang:r decode:true crayon-inline">es()</span>, <span class="lang:r decode:true crayon-inline">ssarima()</span>, <span class="lang:r decode:true crayon-inline">ces()</span>, <span class="lang:r decode:true crayon-inline">ges()</span> - all the forecasting functions in <span class="lang:r decode:true crayon-inline">smooth</span> have a parameter <span class="lang:r decode:true crayon-inline">xregDo</span>, which defines what to do with the provided exogenous variables, and by default it is set to <span class="lang:r decode:true crayon-inline">"use"</span>. However, there is also an option of <span class="lang:r decode:true crayon-inline">"select"</span>, which uses the aforementioned <span class="lang:r decode:true crayon-inline">stepwise()</span> function. The function is applied to the residuals of the specified model, and when the appropriate exogenous variables are found, the model with these variables is re-estimated. This may cause some inaccuracies in the selection mechanisms (because, for example, the optimisation of ETS and X parts does not happen simultaneously), but at least it is done in the finite time frame.</p>
<p>Let’s see how this works. First we use all the expanded variables for up to 10 lags and leads without the variables selection mechanism (I will remove the response variable from the first row of the data we previously used):</p>
<pre class="decode">newXreg <- newXreg[,-1]
ourModelUse <- es(BJsales, "XXN", xreg=newXreg, h=10, holdout=TRUE, silent=FALSE, xregDo="use", intervals="sp")</pre>
<pre>Time elapsed: 1.13 seconds
Model estimated: ETSX(ANN)
Persistence vector g:
alpha 
0.922 
Initial values were optimised.
24 parameters were estimated in the process
Residuals standard deviation: 0.287
Xreg coefficients were estimated in a normal style
Cost function type: MSE; Cost function value: 0.068

Information criteria:
      AIC      AICc       BIC 
 69.23731  79.67209 139.83673 
95% semiparametric prediction intervals were constructed
100% of values are in the prediction interval
Forecast errors:
MPE: 0%; Bias: 55.7%; MAPE: 0.1%; SMAPE: 0.1%
MASE: 0.166; sMAE: 0.1%; RelMAE: 0.055; sMSE: 0%</pre>
<div id="attachment_1621" style="width: 310px" class="wp-caption alignnone"><a href="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2018/01/BJsalesETSXUse.png&amp;nocache=1"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-1621" src="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2018/01/BJsalesETSXUse-300x175.png&amp;nocache=1" alt="" width="300" height="175" class="size-medium wp-image-1621" srcset="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2018/01/BJsalesETSXUse-300x175.png&amp;nocache=1 300w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2018/01/BJsalesETSXUse-768x448.png&amp;nocache=1 768w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2018/01/BJsalesETSXUse-1024x597.png&amp;nocache=1 1024w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2018/01/BJsalesETSXUse.png&amp;nocache=1 1200w" sizes="(max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-1621" class="wp-caption-text">BJsales series and ETSX with all the variables</p></div>
<p>As we see, the forecast became more accurate in this case than <a href="/en/2018/01/15/smooth-package-for-r-common-ground-part-iii-exogenous-variables-basic-stuff/">in the case of using pure BJsales.lead</a>, which may mean that there can be a lagged relationship between the sales and the indicator. However, we have included too many exogenous variable, which may lead to the overfitting. So there is a potential for the accuracy increase if we remove the redundant variables. And that’s where the selection procedure kicks in:</p>
<pre class="decode">ourModelSelect <- es(BJsales, "XXN", xreg=newXreg, h=10, holdout=TRUE, silent=FALSE, xregDo="select", intervals="sp")</pre>
<pre>Time elapsed: 0.98 seconds
Model estimated: ETSX(ANN)
Persistence vector g:
alpha 
    1 
Initial values were optimised.
11 parameters were estimated in the process
Residuals standard deviation: 0.283
Xreg coefficients were estimated in a normal style
Cost function type: MSE; Cost function value: 0.074

Information criteria:
     AIC     AICc      BIC 
54.55463 56.61713 86.91270 
95% semiparametric prediction intervals were constructed
100% of values are in the prediction interval
Forecast errors:
MPE: 0%; Bias: 61.4%; MAPE: 0.1%; SMAPE: 0.1%
MASE: 0.159; sMAE: 0.1%; RelMAE: 0.052; sMSE: 0%</pre>
<div id="attachment_1622" style="width: 310px" class="wp-caption alignnone"><a href="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2018/01/BJsalesETSXSelect.png&amp;nocache=1"><img decoding="async" aria-describedby="caption-attachment-1622" src="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2018/01/BJsalesETSXSelect-300x175.png&amp;nocache=1" alt="" width="300" height="175" class="size-medium wp-image-1622" srcset="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2018/01/BJsalesETSXSelect-300x175.png&amp;nocache=1 300w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2018/01/BJsalesETSXSelect-768x448.png&amp;nocache=1 768w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2018/01/BJsalesETSXSelect-1024x597.png&amp;nocache=1 1024w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2018/01/BJsalesETSXSelect.png&amp;nocache=1 1200w" sizes="(max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-1622" class="wp-caption-text">BJsales series and ETSX with the selected variables</p></div>
<p>Although it is hard to see from graph, there is an increase in forecasting accuracy: MASE was reduced from 0.166 to 0.159. AICc has also reduced from 79.67209 to 56.61713. This is because only 8 exogenous variables are used in the second model (instead of 21):</p>
<pre class="decode">ncol(ourModelUse$xreg)
ncol(ourModelSelect$xreg)</pre>
<p>The xreg selection works even when you have combination of forecasts. The exogenous variables are selected for each model separately in this case, and then the forecasts are combined based on IC weights. For example, this way we can combine different non-seasonal additive ETS models:</p>
<pre class="decode">ourModelCombine <- es(BJsales, c("ANN","AAN","AAdN","CCN"), xreg=newXreg, h=10, holdout=TRUE, silent=FALSE, xregDo="s", intervals="sp")</pre>
<pre>Time elapsed: 1.46 seconds
Model estimated: ETSX(CCN)
Initial values were optimised.
Residuals standard deviation: 0.272
Xreg coefficients were estimated in a normal style
Cost function type: MSE

Information criteria:
(combined values)
     AIC     AICc      BIC 
54.55463 56.61713 86.91270 
95% semiparametric prediction intervals were constructed
100% of values are in the prediction interval
Forecast errors:
MPE: 0%; Bias: 61.4%; MAPE: 0.1%; SMAPE: 0.1%
MASE: 0.159; sMAE: 0.1%; RelMAE: 0.052; sMSE: 0%</pre>
<p>Taking that ETSX(A,N,N) is significantly better in terms of AICc than the other models, we end up with a maximum weight for that model and infinitesimal weights for the others. That’s why the forecasts of the ourModelSelect and ourModelCombine are roughly the same. Starting from v2.3.2, es() function returns the matrix of information criteria for different models estimated in the process, so we can see what was used and what were the values for the models. This information is available through the element ICs:</p>
<pre class="decode">ourModelCombine$ICs</pre>
<pre>               AIC      AICc      BIC
ANN       54.55463  56.61713  86.9127
AAN      120.85273 122.91523 153.2108
AAdN     107.76905 110.22575 143.0688
Combined  54.55463  56.61713  86.9127</pre>
<p>So, as we see, ETS(A,N,N) had indeed the lowest information criterion of all the models in the combination (substantially lower than the others), which led to its domination in the combination.</p>
<p>Keep in mind that this combination of forecasts does not mean the combination of regression models with different exogenous variables – this functionality is currently unavailable, and I am not sure how to implement it (and whether it is needed at all). The combination of forecasts is done for the whole ETS models, not just for the X parts of it.</p>
<p>Finally, it is worth noting that the variables selection approach we use here puts more importance on the dynamic model (in our examples it is ETS) rather than on the exogenous variables. We use exogenous variables as an additional instrument which allows increasing the forecasting accuracy. The conventional econometrics approach would be the other way around: construct regression, then add dynamic components to that in case you cannot explain the response variable with the available data. This approach has different aims and leads to different results.</p>
<p>Message <a href="https://openforecast.org/2018/02/10/smooth-package-for-r-common-ground-part-iv-exogenous-variables-advanced-stuff/">&#8220;smooth&#8221; package for R. Common ground. Part IV. Exogenous variables. Advanced stuff</a> first appeared on <a href="https://openforecast.org">Open Forecasting</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://openforecast.org/2018/02/10/smooth-package-for-r-common-ground-part-iv-exogenous-variables-advanced-stuff/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>&#8220;smooth&#8221; package for R. Common ground. Part III. Exogenous variables. Basic stuff</title>
		<link>https://openforecast.org/2018/01/15/smooth-package-for-r-common-ground-part-iii-exogenous-variables-basic-stuff/</link>
					<comments>https://openforecast.org/2018/01/15/smooth-package-for-r-common-ground-part-iii-exogenous-variables-basic-stuff/#respond</comments>
		
		<dc:creator><![CDATA[Ivan Svetunkov]]></dc:creator>
		<pubDate>Mon, 15 Jan 2018 14:42:01 +0000</pubDate>
				<category><![CDATA[Common parameters]]></category>
		<category><![CDATA[Package smooth for R]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Univariate models]]></category>
		<category><![CDATA[ARIMA]]></category>
		<category><![CDATA[ETS]]></category>
		<category><![CDATA[smooth]]></category>
		<guid isPermaLink="false">https://openforecast.org/?p=1613</guid>

					<description><![CDATA[<p>One of the features of the functions in smooth package is the ability to use exogenous (aka “external”) variables. This potentially leads to the increase in the forecasting accuracy (given that you have a good estimate of the future exogenous variable). For example, in retail this can be a binary variable for promotions and we [&#8230;]</p>
<p>Message <a href="https://openforecast.org/2018/01/15/smooth-package-for-r-common-ground-part-iii-exogenous-variables-basic-stuff/">&#8220;smooth&#8221; package for R. Common ground. Part III. Exogenous variables. Basic stuff</a> first appeared on <a href="https://openforecast.org">Open Forecasting</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>One of the features of the functions in smooth package is the ability to use exogenous (aka “external”) variables. This potentially leads to the increase in the forecasting accuracy (given that you have a good estimate of the future exogenous variable). For example, in retail this can be a binary variable for promotions and we may know when the next promotion will happen. Or we may have an idea about the temperature for the next day and include it as an exogenous variable in the model.</p>
<p>While <span class="lang:r decode:true crayon-inline">arima()</span> function from <span class="lang:r decode:true crayon-inline">stats</span> package allows inserting exogenous variables, <span class="lang:r decode:true crayon-inline">ets()</span> function from <span class="lang:r decode:true crayon-inline">forecast</span> package does not. That was one of the original motivations of developing an alternative function for ETS. It is worth noting that all the forecasting functions in <span class="lang:r decode:true crayon-inline">smooth</span> package (except for <span class="lang:r decode:true crayon-inline">sma()</span>) allow using exogenous variables, so this feature is not restricted with <span class="lang:r decode:true crayon-inline">es()</span> only.</p>
<p>There are two types of models with exogenous variables implemented in <span class="lang:r decode:true crayon-inline">smooth</span> functions: additive error model and multiplicative error model. They are slightly different. The former one is formulated as:<br />
\begin{equation} \label{eq:additive}<br />
	y_t = w&#8217; v_{t-l} + a_1 x_{1,t} + a_2 x_{2,t} + … + a_k x_{k,t} + \epsilon_t ,<br />
\end{equation}<br />
where \(a_1, a_2, …, a_k\) are parameters for the respective exogenous variables \(x_{1,t}, x_{2,t}, …, x_{t,k}\). All the other variables have been discussed earlier in <a href="/en/2016/11/02/smooth-package-for-r-es-function-part-ii-pure-additive-models/">the previous posts</a>.</p>
<p>The second model is formulated differently, because it is driven by <a href="/en/2016/11/18/smooth-package-for-r-es-function-part-iii-multiplicative-models/">the multiplication of the ETS components</a> by error term:<br />
\begin{equation} \label{eq:multiplicative}<br />
	\log y_t = w&#8217; \log(v_{t-1}) + a_1 x_{1,t} + a_2 x_{2,t} + … + a_k x_{k,t} + \log(1 + \epsilon_t) ,<br />
\end{equation}<br />
so this model can be reformlated as:<br />
\begin{equation} \label{eq:multiplicativeAlternative}<br />
	y_t =\exp \left({w&#8217; \log(v_{t-1})} \right) \exp(a_1 x_{1,t}) \exp(a_2 x_{2,t}) \dots \exp(a_k x_{k,t}) (1 + \epsilon_t).<br />
\end{equation}</p>
<p>This corresponds to log-linear model. This formulation is adopted because the exponents of exogenous variables allow using dummy variables, which would not be possible in log-log model. This also means that if you want to have a log-log model, you need to take logarithms of the exogenous variables before using them in the functions.</p>
<p>The important thing to note is that the mixed ETS models may cause problems, because some components are added, while the others are multiplied and you may end up with weird cocktail leading to meaningless or unstable forecasts. So in cases of ETSX I would advice sticking with either <a href="/en/2016/11/02/smooth-package-for-r-es-function-part-ii-pure-additive-models/">pure additive</a> or <a href="/en/2016/11/18/smooth-package-for-r-es-function-part-iii-multiplicative-models/">pure multiplicative</a> models, ignoring their combinations (see <a href="/en/2017/01/24/smooth-package-for-r-es-function-part-iv-model-selection-and-combination-of-forecasts/">the model selection post</a> on how to select between the pure models).</p>
<p>In order to construct the model with a set of preselected exogenous variables, all you need to do is to specify the vector, matrix or data.frame with these variables in columns the following way:</p>
<pre class="decode">ourModel <- es(BJsales, "XXN", xreg=BJsales.lead, h=10, holdout=TRUE, silent=FALSE)</pre>
<pre>Estimation progress: 100%... Done! 
Time elapsed: 0.27 seconds
Model estimated: ETSX(AAdN)
Persistence vector g:
alpha  beta 
0.939 0.301 
Damping parameter: 0.877
Initial values were optimised.
7 parameters were estimated in the process
Residuals standard deviation: 1.381
Xreg coefficients were estimated in a normal style
Cost function type: MSE; Cost function value: 1.811

Information criteria:
     AIC     AICc      BIC 
494.4490 495.2975 515.0405 
Forecast errors:
MPE: 1.2%; Bias: 91.3%; MAPE: 1.3%; SMAPE: 1.3%
MASE: 2.794; sMAE: 1.5%; RelMAE: 0.917; sMSE: 0%</pre>
<div id="attachment_1623" style="width: 310px" class="wp-caption alignnone"><a href="/wp-content/uploads/2018/01/BJsalesETSXSimple.png"><img decoding="async" aria-describedby="caption-attachment-1623" src="/wp-content/uploads/2018/01/BJsalesETSXSimple-300x175.png" alt="" width="300" height="175" class="size-medium wp-image-1623" srcset="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2018/01/BJsalesETSXSimple-300x175.png&amp;nocache=1 300w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2018/01/BJsalesETSXSimple-768x448.png&amp;nocache=1 768w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2018/01/BJsalesETSXSimple-1024x597.png&amp;nocache=1 1024w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2018/01/BJsalesETSXSimple.png&amp;nocache=1 1200w" sizes="(max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-1623" class="wp-caption-text">BJsales series and ETSX with a leading indicator</p></div>
<p>In this example we use sales data from Box & Jenkins (1976) book with a leading indicator. I’ve asked for 10-steps ahead forecast and for the holdout. This means that we don’t need to do anything with the leading indicator, the last 10 actual observations of <span class="lang:r decode:true crayon-inline">BJsales.lead</span> will be taken for the forecasting purposes. The function tells us that ETSX model was estimated and points out that the parameters for exogenous variables were "estimated in a normal style", meaning that they are assumed to be constant for all the observations. The option with dynamic parameters will be discussed later.</p>
<p>As we see the selected model is ETS(A,Ad,N), but the produced forecast is not really accurate and seems to be biased.</p>
<p>If you ever get lost and stop understanding what model you have created, you can use <span class="lang:r decode:true crayon-inline">formula()</span> function, which in case of <span class="lang:r decode:true crayon-inline">smooth</span> functions gives purely descriptive information – the output of that function cannot be used directly in any model. Let’s see what is the formula of our model:</p>
<pre class="decode">formula(ourModel)</pre>
<pre>"y[t] = l[t-1] + b[t-1] + a1 * x[t] + e[t]"</pre>
<p>This thing tells us that we have level l[t-1], trend b[t-1], one exogenous variable that is called "x[t]" in our case (because we provided a vector) and the error term. If we had a matrix with exogenous variables or a model with dynamic parameters for exogenous variables, this would be reflected in the formula. Just remember, that this is purely descriptive thing. You cannot use this information directly in any other model (as you may usually do with <span class="lang:r decode:true crayon-inline">lm()</span> function).</p>
<p>Just for fun, let’s specify a weird mixed model and see its formula:</p>
<pre class="decode">ourModel <- es(BJsales, "MAN", xreg=BJsales.lead, h=10, holdout=TRUE)
formula(ourModel)</pre>
<pre>"y[t] = (l[t-1] + b[t-1]) * exp(a1 * x[t]) * e[t]"</pre>
<p>As it can be seen, we firstly add the trend component to the level and then multiply it by the exponent of our exogenous variable. If for some reason trend is negative and level is low, we will end up with a very weird thing, because the exogenous variable will be multiplied by a negative number. That’s why I say that the mixed models are not safe.</p>
<p>Now, if we do not have the values for the holdout, then the <span class="lang:r decode:true crayon-inline">smooth</span> functions will automatically produce forecasts using <span class="lang:r decode:true crayon-inline">es()</span> for each of the variables and then use those values in the final forecast of the variable of interest. Beware, that if you use dummy variables, then the forecast will correspond to some sort of conditional mean value (this is then produced by <span class="lang:r decode:true crayon-inline">iss()</span> function). This means that you will end up having something like 0.784 as a forecast. So be careful when blindly using the function for the cases of <span class="lang:r decode:true crayon-inline">holdout=FALSE</span>. Here’s how it works:</p>
<pre class="decode">es(BJsales, "XXN", xreg=BJsales.lead, h=10, holdout=FALSE, silent=FALSE)</pre>
<p>We should get the following warning:</p>
<pre>Warning message:
xreg did not contain values for the holdout, so we had to predict missing values.</pre>
<p>If your exogenous variable is longer than the variable of interest, then <span class="lang:r decode:true crayon-inline">smooth</span> functions will cut off the redundant end of data. For example:</p>
<pre class="decode">ourModel <- es(BJsales[1:140], "XXN", xreg=BJsales.lead, h=10, holdout=TRUE)</pre>
<p>This produces a warning:</p>
<pre>Warning message:
xreg contained too many observations, so we had to cut off some of them.</pre>
<p>This is because xreg contained too many observations, and the function used only the first 140 of them, removing the last ten.</p>
<p>As you see, the function works on its quite well, but if you are kin in using <span class="lang:r decode:true crayon-inline">forecast()</span> function together with <span class="lang:r decode:true crayon-inline">smooth</span> functions (which is not necessary at all), you can do it the following way:</p>
<pre class="decode">forecast(ourModel, h=10, xreg=BJsales.lead)</pre>
<p>Due to the implementation of exogenous variables in <span class="lang:r decode:true crayon-inline">smooth</span>, you need to provide the whole <span class="lang:r decode:true crayon-inline">xreg</span> (as much as you have) to the <span class="lang:r decode:true crayon-inline">forecast()</span> function. If you provide the values for the holdout only, the function will think that your <span class="lang:r decode:true crayon-inline">xreg</span> series is too short and produce forecasts for it. If you provide the values for the in-sample only, the function will once again produce forecasts for each variable in xreg using <span class="lang:r decode:true crayon-inline">es()</span> (as discussed above).</p>
<p>Personally, I would advise using <span class="lang:r decode:true crayon-inline">es()</span>, <span class="lang:r decode:true crayon-inline">ssarima()</span> and other <span class="lang:r decode:true crayon-inline">smooth</span> functions directly, ignoring <span class="lang:r decode:true crayon-inline">forecast()</span>. This way you can prepare your <span class="lang:r decode:true crayon-inline">xreg</span>, and then use it directly without additional lines of code.</p>
<p>Similarly to how it was discussed in <a href="/en/2017/06/11/smooth-package-for-r-prediction-intervals/">the previous post</a>, you can always ask for the prediction intervals and you will have them. But keep in mind that parametric intervals are pretty complicated in case of dynamic models with exogenous variables, because the covariances between the parameters and ETS components are hard to derive. Although the function will produce you anything you ask for it, the parametric intervals may be inaccurate. So I would advise using semiparametric or nonparametric intervals in case of <span class="lang:r decode:true crayon-inline">xreg</span>.</p>
<p>Finally, you can always pre-specify the values of <span class="lang:r decode:true crayon-inline">xreg</span> parameters if you don’t want them to be estimated. This is controlled using <span class="lang:r decode:true crayon-inline">initialX</span> parameter:</p>
<pre class="decode">ourModel <- es(BJsales, "XXN", xreg=BJsales.lead, h=10, holdout=T, initialX=c(-1))</pre>
<p>The function is also smart enough to detect if a provided variable does not have any variability or if some of the variables are highly correlated (correlation or multiple correlations are higher than 0.999). In both of these cases, it will drop some variables and tell us about that:</p>
<pre class="decode">es(BJsales, "XXN", xreg=cbind(BJsales.lead,BJsales.lead), h=10, holdout=TRUE)</pre>
<pre>Warning message:
Some exogenous variables were perfectly correlated. We've dropped them out.</pre>
<pre class="decode">es(BJsales, "XXN", xreg=cbind(BJsales.lead,rep(100,150)), h=10, holdout=TRUE)</pre>
<pre>Warning message:
Some exogenous variables do not have any variability. Dropping them out.</pre>
<p>If you accidentally provide the response variable in xreg, the function will also drop it:</p>
<pre class="decode">es(BJsales, "XXN", xreg=cbind(BJsales,BJsales.lead), h=10, holdout=TRUE)</pre>
<pre>Warning message:
One of exogenous variables and the forecasted data are exactly the same. We have dropped it.</pre>
<p>That’s it for the basic exogenous variables functionality in <span class="lang:r decode:true crayon-inline">smooth</span> functions. Next time we will continue with more advanced, more fascinating stuff.</p>
<p>Message <a href="https://openforecast.org/2018/01/15/smooth-package-for-r-common-ground-part-iii-exogenous-variables-basic-stuff/">&#8220;smooth&#8221; package for R. Common ground. Part III. Exogenous variables. Basic stuff</a> first appeared on <a href="https://openforecast.org">Open Forecasting</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://openforecast.org/2018/01/15/smooth-package-for-r-common-ground-part-iii-exogenous-variables-basic-stuff/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>&#8220;smooth&#8221; package for R. Common ground. Part II. Estimators</title>
		<link>https://openforecast.org/2017/11/20/smooth-package-for-r-common-ground-part-ii-estimators/</link>
					<comments>https://openforecast.org/2017/11/20/smooth-package-for-r-common-ground-part-ii-estimators/#respond</comments>
		
		<dc:creator><![CDATA[Ivan Svetunkov]]></dc:creator>
		<pubDate>Mon, 20 Nov 2017 17:21:15 +0000</pubDate>
				<category><![CDATA[Common parameters]]></category>
		<category><![CDATA[Package smooth for R]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Univariate models]]></category>
		<category><![CDATA[ARIMA]]></category>
		<category><![CDATA[ETS]]></category>
		<category><![CDATA[smooth]]></category>
		<category><![CDATA[statistics]]></category>
		<guid isPermaLink="false">https://openforecast.org/?p=1359</guid>

					<description><![CDATA[<p>UPDATE: Starting from the v2.5.1 the cfType parameter has been renamed into loss. This post has been updated since then in order to include the more recent name. A bit about estimates of parameters Hi everyone! Today I want to tell you about parameters estimation of smooth functions. But before going into details, there are [&#8230;]</p>
<p>Message <a href="https://openforecast.org/2017/11/20/smooth-package-for-r-common-ground-part-ii-estimators/">&#8220;smooth&#8221; package for R. Common ground. Part II. Estimators</a> first appeared on <a href="https://openforecast.org">Open Forecasting</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><strong>UPDATE</strong>: Starting from the v2.5.1 the <span class="lang:r decode:true crayon-inline">cfType</span> parameter has been renamed into <span class="lang:r decode:true crayon-inline">loss</span>. This post has been updated since then in order to include the more recent name.</p>
<h3>A bit about estimates of parameters</h3>
<p>Hi everyone! Today I want to tell you about parameters estimation of smooth functions. But before going into details, there are several things that I want to note. In this post we will discuss <strong>bias</strong>, <strong>efficiency</strong> and <strong>consistency</strong> of estimates of parameters, so I will use phrases like “efficient estimator”, implying that we are talking about some optimisation mechanism that gives efficient estimates of parameters. It is probably not obvious for people without statistical background to understand what the hell is going on and why we should care, so I decided to give a brief explanation. Although there are strict statistical definitions of the aforementioned terms (you can easily find them in Wikipedia or anywhere else), I do not want to copy-paste them here, because there are only a couple of important points worth mentioning in our context. So, let’s get started.</p>
<p><strong>Bias</strong> refers to the expected difference between the estimated value of parameter (on a specific sample) and the &#8220;true&#8221; one. Having unbiased estimates of parameters is important because they should lead to more accurate forecasts (at least in theory). For example, if the estimated parameter is equal to zero, while in fact it should be 0.5, then the model would not take the provided information into account correctly and as a result will produce less accurate point forecasts and incorrect prediction intervals. In inventory this may mean that we constantly order 100 units less than needed only because the parameter is lower than it should be.</p>
<p><strong>Efficiency</strong> means that if the sample size increases, then the estimated parameters will not change substantially, they will vary in a narrow range (variance of estimates will be small). In the case with inefficient estimates the increase of sample size from 50 to 51 observations may lead to the change of a parameter from 0.1 to, let’s say, 10. This is bad because the values of parameters usually influence both point forecasts and prediction intervals. As a result the inventory decision may differ radically from day to day. For example, we may decide that we urgently need 1000 units of product on Monday, and order it just to realise on Tuesday that we only need 100. Obviously this is an exaggeration, but no one wants to deal with such an erratic stocking policy, so we need to have efficient estimates of parameters.</p>
<p><strong>Consistency</strong> means that our estimates of parameters will get closer to the stable values (what statisticians would refer to as &#8220;<a href="/en/2016/06/25/true-model/">true</a>&#8221; values) with the increase of the sample size. This is important because in the opposite case estimates of parameters will diverge and become less and less realistic. This once again influences both point forecasts and prediction intervals, which will be less meaningful than they should have been. In a way consistency means that with the increase of the sample size the parameters will become more efficient and less biased. This in turn means that the more observations we have, the better. There is a prejudice in the world of practitioners that the situation in the market changes so fast that the old observations become useless very fast. As a result many companies just through away the old data. Although, in general the statement about the market changes is true, the forecasters tend to work with the models that take this into account (e.g. Exponential smoothing, ARIMA). These models adapt to the potential changes. So, we may benefit from the old data because it allows us getting more consistent estimates of parameters. Just keep in mind, that you can always remove the annoying bits of data but you can never un-throw away the data.</p>
<p>Having clarified these points, we can proceed to the topic of today’s post.</p>
<h3>One-step-ahead estimators of smooth functions</h3>
<p><a href="/en/2017/01/24/smooth-package-for-r-es-function-part-iv-model-selection-and-combination-of-forecasts/">We already know</a> that the default estimator used for <span class="lang:r decode:true crayon-inline">smooth</span> functions is Mean Squared Error (for one step ahead forecast). If the residuals are distributed <a href="/en/2016/11/02/smooth-package-for-r-es-function-part-ii-pure-additive-models/">normally</a> / <a href="/en/2016/11/18/smooth-package-for-r-es-function-part-iii-multiplicative-models/">log-normally</a>, then the minimum of MSE gives estimates that also maximise the respective likelihood function. As a result the estimates of parameters become nice: consistent and efficient. It is also known in statistics that minimum of MSE gives mean estimates of the parameters, which means that MSE also produces unbiased estimates of parameters (if the model is specified correctly and <a href="/en/2016/06/25/true-model/">bla-bla-bla</a>). This works very well, when we deal with symmetric distributions of random variables. But it may perform poorly otherwise.</p>
<p>In this post we will use the series N1823 for our examples:</p>
<pre class="decode">library(Mcomp)
x <- ts(c(M3$N1823$x,M3$N1823$xx),frequency=frequency(M3$N1823$x))</pre>
<p>Plot the data in order to see what we have:</p>
<pre class="decode">plot(x)</pre>
<div id="attachment_1360" style="width: 310px" class="wp-caption alignnone"><a href="/wp-content/uploads/2017/10/SmoothEstimators01.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-1360" src="/wp-content/uploads/2017/10/SmoothEstimators01-300x175.png" alt="" width="300" height="175" class="size-medium wp-image-1360" srcset="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators01-300x175.png&amp;nocache=1 300w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators01-768x448.png&amp;nocache=1 768w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators01-1024x597.png&amp;nocache=1 1024w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators01.png&amp;nocache=1 1200w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-1360" class="wp-caption-text">N1823 series</p></div>
<p>The data seems to have slight multiplicative seasonality, which changes over the time, but it is hard to say for sure. Anyway, in order to simplify things, we will apply an ETS(A,A,N) model to this data, so we can see how the different estimators behave. We will withhold 18 observations as it is usually done for monthly data in M3.</p>
<pre class="decode">ourModel <- es(x,"AAN",silent=F,interval="p",h=18,holdout=T)</pre>
<div id="attachment_1361" style="width: 310px" class="wp-caption alignnone"><a href="/wp-content/uploads/2017/10/SmoothEstimators02.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-1361" src="/wp-content/uploads/2017/10/SmoothEstimators02-300x175.png" alt="" width="300" height="175" class="size-medium wp-image-1361" srcset="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators02-300x175.png&amp;nocache=1 300w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators02-768x448.png&amp;nocache=1 768w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators02-1024x597.png&amp;nocache=1 1024w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators02.png&amp;nocache=1 1200w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-1361" class="wp-caption-text">N1823 and ETS(A,A,N) with MSE</p></div>
<p>Here’s the output:</p>
<pre>Time elapsed: 0.08 seconds
Model estimated: ETS(AAN)
Persistence vector g:
alpha  beta 
0.147 0.000 
Initial values were optimised.
5 parameters were estimated in the process
Residuals standard deviation: 629.249
Cost function type: MSE; Cost function value: 377623.069

Information criteria:
     AIC     AICc      BIC 
1703.389 1703.977 1716.800 
95% parametric prediction intervals were constructed
100% of values are in the prediction interval
Forecast errors:
MPE: -14%; Bias: -74.1%; MAPE: 16.8%; SMAPE: 15.1%
MASE: 0.855; sMAE: 13.4%; RelMAE: 1.047; sMSE: 2.4%</pre>
<p>It is hard to make any reasonable conclusions from the graph and the output, but it seems that we slightly overforecast the data. At least the prediction interval covers all the values in the holdout. Relative MAE is equal to 1.047, which means that the model produced forecasts less accurate than Naive. Let’s have a look at the residuals:</p>
<pre class="decode">qqnorm(resid(ourModel))
qqline(resid(ourModel))</pre>
<div id="attachment_1363" style="width: 310px" class="wp-caption alignnone"><a href="/wp-content/uploads/2017/10/SmoothEstimators02QQ.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-1363" src="/wp-content/uploads/2017/10/SmoothEstimators02QQ-300x175.png" alt="" width="300" height="175" class="size-medium wp-image-1363" srcset="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators02QQ-300x175.png&amp;nocache=1 300w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators02QQ-768x448.png&amp;nocache=1 768w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators02QQ-1024x597.png&amp;nocache=1 1024w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators02QQ.png&amp;nocache=1 1200w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-1363" class="wp-caption-text">QQ-plot of the residuals from ETS(A,A,N) with MSE</p></div>
<p>The residuals of this model do not look normal, a lot of empirical quantiles a far from the theoretical ones. If we conduct Shapiro-Wilk test, then we will have to reject the hypothesis of normality for the residuals on 5%:</p>
<pre class="decode">shapiro.test(resid(ourModel))
> p-value = 0.001223</pre>
<p>This may indicate that other estimators may do a better job. And there is a magical parameter <span class="lang:r decode:true crayon-inline">loss</span> in the <span class="lang:r decode:true crayon-inline">smooth</span> functions which allows to estimate models differently. It controls what to use and how to use it. You can select the following estimators instead of MSE:</p>
<ul>
<li>MAE – Mean Absolute Error:</li>
</ul>
<p>\begin{equation} \label{eq:MAE}<br />
	\text{MAE} = \frac{1}{T} \sum_{t=1}^T |e_{t+1}|<br />
\end{equation}</p>
<p>The minimum of MAE gives median estimates of the parameters. MAE is considered to be a more robust estimator than MSE. If you have asymmetric distribution, give MAE a try. It gives consistent, but not efficient estimates of parameters. Asymptotically, if the distribution of the residuals is normal, the estimators of MAE converge to the estimators of MSE (which follows from the connection between mean and median of normal distribution). Also, the minimum of MAE corresponds to the maximum of the likelihood of Laplace distribution (and the functions of the package use this property for the inference).</p>
<p>Let’s see what happens with the same model, on the same data when we use MAE:</p>
<pre class="decode">ourModel <- es(x,"AAN",silent=F,interval="p",h=18,holdout=T,loss="MAE")</pre>
<div id="attachment_1362" style="width: 310px" class="wp-caption alignnone"><a href="/wp-content/uploads/2017/10/SmoothEstimators03.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-1362" src="/wp-content/uploads/2017/10/SmoothEstimators03-300x175.png" alt="" width="300" height="175" class="size-medium wp-image-1362" srcset="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators03-300x175.png&amp;nocache=1 300w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators03-768x448.png&amp;nocache=1 768w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators03-1024x597.png&amp;nocache=1 1024w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators03.png&amp;nocache=1 1200w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-1362" class="wp-caption-text">N1823 and ETS(A,A,N) with MAE</p></div>
<pre>Time elapsed: 0.09 seconds
Model estimated: ETS(AAN)
Persistence vector g:
alpha  beta 
0.101 0.000 
Initial values were optimised.
5 parameters were estimated in the process
Residuals standard deviation: 636.546
Cost function type: MAE; Cost function value: 462.675

Information criteria:
     AIC     AICc      BIC 
1705.879 1706.468 1719.290 
95% parametric prediction intervals were constructed
100% of values are in the prediction interval
Forecast errors:
MPE: -5.1%; Bias: -32.1%; MAPE: 12.9%; SMAPE: 12.4%
MASE: 0.688; sMAE: 10.7%; RelMAE: 0.842; sMSE: 1.5%</pre>
<p>There are several things to note from the graph and the output. First, the smoothing parameter alpha is smaller than in the previous case. Second, Relative MAE is smaller than one, which means that model in this case outperformed Naive. Comparing this value with the one from the previous model, we can conclude that MAE worked well as an estimator of parameters for this data. Finally, the graph shows that point forecasts go through the middle of the holdout sample, which is reflected with lower values of error measures. The residuals are still not normally distributed, but this is expected, because they won't become normal just because we used a different estimator:</p>
<div id="attachment_1364" style="width: 310px" class="wp-caption alignnone"><a href="/wp-content/uploads/2017/10/SmoothEstimators03QQ.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-1364" src="/wp-content/uploads/2017/10/SmoothEstimators03QQ-300x175.png" alt="" width="300" height="175" class="size-medium wp-image-1364" srcset="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators03QQ-300x175.png&amp;nocache=1 300w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators03QQ-768x448.png&amp;nocache=1 768w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators03QQ-1024x597.png&amp;nocache=1 1024w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators03QQ.png&amp;nocache=1 1200w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-1364" class="wp-caption-text">QQ-plot of the residuals from ETS(A,A,N) with MAE</p></div>
<ul>
<li>HAM – Half Absolute Moment:</li>
</ul>
<p>\begin{equation} \label{eq:HAM}<br />
	\text{HAM} = \frac{1}{T} \sum_{t=1}^T \sqrt{|e_{t+1}|}<br />
\end{equation}<br />
This is even more robust estimator than MAE. On count data its minimum corresponds to the mode of data. In case of continuous data the minimum of this estimator corresponds to something not yet well studied, between mode and median. The paper about this thing is currently in a draft stage, but you can already give it a try, if you want. This is also consistent, but not efficient estimator.</p>
<p>The same example, the same model, but HAM as an estimator:</p>
<pre class="decode">ourModel <- es(x,"AAN",silent=F,interval="p",h=18,holdout=T,loss="HAM")</pre>
<div id="attachment_1365" style="width: 310px" class="wp-caption alignnone"><a href="/wp-content/uploads/2017/10/SmoothEstimators04.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-1365" src="/wp-content/uploads/2017/10/SmoothEstimators04-300x175.png" alt="" width="300" height="175" class="size-medium wp-image-1365" srcset="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators04-300x175.png&amp;nocache=1 300w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators04-768x448.png&amp;nocache=1 768w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators04-1024x597.png&amp;nocache=1 1024w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators04.png&amp;nocache=1 1200w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-1365" class="wp-caption-text">N1823 and ETS(A,A,N) with HAM</p></div>
<pre>Time elapsed: 0.06 seconds
Model estimated: ETS(AAN)
Persistence vector g:
alpha  beta 
0.001 0.001 
Initial values were optimised.
5 parameters were estimated in the process
Residuals standard deviation: 666.439
Cost function type: HAM; Cost function value: 19.67

Information criteria:
     AIC     AICc      BIC 
1715.792 1716.381 1729.203 
95% parametric prediction intervals were constructed
100% of values are in the prediction interval
Forecast errors:
MPE: -1.7%; Bias: -14.1%; MAPE: 11.4%; SMAPE: 11.4%
MASE: 0.63; sMAE: 9.8%; RelMAE: 0.772; sMSE: 1.3%</pre>
<p>This estimator produced even more accurate forecasts in this example, forcing smoothing parameters to become close to zero. Note that the residuals standard deviation in case of HAM is larger than in case of MAE which in its turn is larger than in case of MSE. This means that one-step-ahead parametric and semiparametric prediction intervals will be wider in case of HAM than in case of MAE, than in case of MSE. However, taking that the smoothing parameters in the last model are close to zero, the multiple steps ahead prediction intervals of HAM may be narrower than the ones of MSE.</p>
<p>Finally, it is worth noting that the optimisation of models using different estimators takes different time. MSE is the slowest, while HAM is the fastest estimator. I don't have any detailed explanation of this, but this obviously happens because of the form of the cost functions surfaces. So if you are in a hurry and need to estimate something somehow, you can give HAM a try. Just remember that information criteria may become inapplicable in this case.</p>
<h3>Multiple-steps-ahead estimators of smooth functions</h3>
<p>While these three estimators above are calculated based on the one-step-ahead forecast error, the next three are based on multiple steps ahead estimators. They can be useful if you want to have a more stable and “conservative” model (a paper on this topic is currently in the final stage). Prior to v2.2.1 these estimators had different names, be aware!</p>
<ul>
<li>MSE\(_h\) - Mean Squared Error for h-steps ahead forecast:</li>
</ul>
<p>\begin{equation} \label{eq:MSEh}<br />
	\text{MSE}_h = \frac{1}{T} \sum_{t=1}^T e_{t+h}^2<br />
\end{equation}<br />
The idea of this estimator is very simple: if you are interested in 5 steps ahead forecasts, then optimise over this horizon, not one-step-ahead. However, by using this estimator, we shrink the smoothing parameters towards zero, forcing the model to become closer to the deterministic and robust to outliers. This applies both to ETS and ARIMA, but the models behave slightly differently. The effect of shrinkage increases with the increase of \(h\). The forecasts accuracy may increase for that specific horizon, but it almost surely will decrease for all the other horizons. Keep in mind that this is in general not efficient and biased estimator with a much slower convergence to the true value than the one-step-ahead estimators. This estimator is eventually consistent, but it may need a very large sample to become one. This means that this estimator may result in values of parameters very close to zero even if they are not really needed for the data. I personally would advise using this thing on large samples (for instance, on high frequency data). By the way, Nikos Kourentzes, Rebecca Killick and I are working on a paper on that topic, so stay tuned. </p>
<p>Here’s what happens when we use this estimator:</p>
<pre class="decode">ourModel <- es(x,"AAN",silent=F,interval="p",h=18,holdout=T,loss="MSEh")</pre>
<div id="attachment_1366" style="width: 310px" class="wp-caption alignnone"><a href="/wp-content/uploads/2017/10/SmoothEstimators05.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-1366" src="/wp-content/uploads/2017/10/SmoothEstimators05-300x175.png" alt="" width="300" height="175" class="size-medium wp-image-1366" srcset="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators05-300x175.png&amp;nocache=1 300w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators05-768x448.png&amp;nocache=1 768w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators05-1024x597.png&amp;nocache=1 1024w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators05.png&amp;nocache=1 1200w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-1366" class="wp-caption-text">N1823 and ETS(A,A,N) with MSEh</p></div>
<pre>Time elapsed: 0.24 seconds
Model estimated: ETS(AAN)
Persistence vector g:
alpha  beta 
    0     0 
Initial values were optimised.
5 parameters were estimated in the process
Residuals standard deviation: 657.781
Cost function type: MSEh; Cost function value: 550179.34

Information criteria:
     AIC     AICc      BIC 
30393.86 30404.45 30635.25 
95% parametric prediction intervals were constructed
100% of values are in the prediction interval
Forecast errors:
MPE: -10.4%; Bias: -62%; MAPE: 14.9%; SMAPE: 13.8%
MASE: 0.772; sMAE: 12.1%; RelMAE: 0.945; sMSE: 1.8%</pre>
<p>As you can see, the smoothing parameters are now equal to zero, which gives us the straight line going through all the data. If we had 1008 observations instead of 108, the parameters would not be shrunk to zero, because the model would need to adapt to changes in order to minimise the respective cost function.</p>
<ul>
<li>TMSE - Trace Mean Squared Error:</li>
</ul>
<p>The need for having a specific 5 steps ahead forecast is not common, so it makes sense to work with something that deals with one to h steps ahead:<br />
\begin{equation} \label{TMSE}<br />
	\text{TMSE} = \sum_{j=1}^h \frac{1}{T} \sum_{t=1}^T e_{t+j}^2<br />
\end{equation}<br />
This estimator is more reasonable than MSE\(_h\) because it takes into account all the errors from one to h-steps-ahead forecasts. This is a desired behaviour in inventory management, because we are not so much interested in how much we will sell tomorrow or next Monday, but rather how much we will sell starting from tomorrow till the next Monday. However, the variance of forecast errors h-steps-ahead is usually larger than the variance of one-step-ahead errors (because of the increasing uncertainty), which leads to the effect of “masking”: the latter is hidden behind the former. As a result if we use TMSE as the estimator, the final values are seriously influenced by the long term errors than the short term ones (see <a href="https://doi.org/10.1109/TNNLS.2015.2411629" rel="noopener noreferrer" target="_blank">Taieb and Atiya, 2015</a> paper). This estimator is not recommended if short-term forecasts are more important than long term ones. Plus, this is still less efficient and more biased estimator than one-step-ahead estimators, with slow convergence to the true values, similar to MSE\(_h\), but slightly better.</p>
<p>This is what happens in our example:</p>
<pre class="decode">ourModel <- es(x,"AAN",silent=F,interval="p",h=18,holdout=T,loss="TMSE")</pre>
<div id="attachment_1666" style="width: 310px" class="wp-caption alignnone"><a href="/wp-content/uploads/2017/11/SmoothEstimators06.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-1666" src="/wp-content/uploads/2017/11/SmoothEstimators06-300x175.png" alt="" width="300" height="175" class="size-medium wp-image-1666" srcset="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/11/SmoothEstimators06-300x175.png&amp;nocache=1 300w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/11/SmoothEstimators06-768x448.png&amp;nocache=1 768w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/11/SmoothEstimators06-1024x597.png&amp;nocache=1 1024w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/11/SmoothEstimators06.png&amp;nocache=1 1200w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-1666" class="wp-caption-text">N1823 and ETS(A,N,N) with TMSE</p></div>
<pre>Time elapsed: 0.2 seconds
Model estimated: ETS(AAN)
Persistence vector g:
alpha  beta 
0.075 0.000 
Initial values were optimised.
5 parameters were estimated in the process
Residuals standard deviation: 633.48
Cost function type: TMSE; Cost function value: 7477097.717

Information criteria:
     AIC     AICc      BIC 
30394.36 30404.94 30635.75 
95% parametric prediction intervals were constructed
100% of values are in the prediction interval
Forecast errors:
MPE: -7.5%; Bias: -48.9%; MAPE: 13.4%; SMAPE: 12.6%
MASE: 0.704; sMAE: 11%; RelMAE: 0.862; sMSE: 1.5%</pre>
<p>Comparing the model estimated using TMSE with the same one estimated using MSE and MSE\(_h\), it is worth noting that the smoothing parameters in this model are greater than in case of MSE\(_h\), but less than in case of MSE. This demonstrates that there is a shrinkage effect in TMSE, forcing the parameters towards zero, but the inclusion of one-step-ahead errors makes model slightly more flexible than in case of MSE\(_h\). Still, it is advised to use this estimator on large samples, where the estimates of parameters become more efficient and less biased.</p>
<ul>
<li>GTMSE - Geometric Trace Mean Squared Error:</li>
</ul>
<p>This is similar to TMSE, but derived from the so called Trace Forecast Likelihood (which I may discuss at some point in one of the future posts). The idea here is to take logarithms of each MSE\(_j\) and then sum them up:<br />
\begin{equation} \label{eq:GTMSE}<br />
	\text{GTMSE} = \sum_{j=1}^h \log \left( \frac{1}{T} \sum_{t=1}^T e_{t+j}^2 \right)<br />
\end{equation}<br />
Logarithms make variances of errors on several steps ahead closer to each other. For example, if the variance of one-step-ahead error is equal to 100 and the variance of 10 steps ahead error is equal to 1000, then their logarithms will be 4.6 and 6.9 respectively. As a result when GTMSE is used as an estimator, the model will take into account both short and long term errors. So this is a more balanced estimator of parameters than MSE\(_h\) and TMSE. This estimator is more efficient than both TMSE and MSE\(_j\) because of the log-scale and converges to true values faster than the previous two, but still can be biased on small samples.</p>
<pre class="decode">ourModel <- es(x,"AAN",silent=F,interval="p",h=18,holdout=T,loss="GTMSE")</pre>
<div id="attachment_1368" style="width: 310px" class="wp-caption alignnone"><a href="/wp-content/uploads/2017/10/SmoothEstimators07.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-1368" src="/wp-content/uploads/2017/10/SmoothEstimators07-300x175.png" alt="" width="300" height="175" class="size-medium wp-image-1368" /></a><p id="caption-attachment-1368" class="wp-caption-text">N1823 and ETS(A,A,N) with GTMSE</p></div>
<pre>Time elapsed: 0.18 seconds
Model estimated: ETS(AAN)
Persistence vector g:
alpha  beta 
    0     0 
Initial values were optimised.
5 parameters were estimated in the process
Residuals standard deviation: 649.253
Cost function type: GTMSE; Cost function value: 232.419

Information criteria:
     AIC     AICc      BIC 
30402.77 30413.36 30644.16 
95% parametric prediction intervals were constructed
100% of values are in the prediction interval
Forecast errors:
MPE: -8.2%; Bias: -53.8%; MAPE: 13.8%; SMAPE: 12.9%
MASE: 0.72; sMAE: 11.3%; RelMAE: 0.882; sMSE: 1.6%</pre>
<p>In our example GTMSE shrinks both smoothing parameters towards zero and makes the model deterministic, which corresponds to MSE\(_h\). However the initial values are slightly different, which leads to slightly different forecasts. Once again, it is advised to use this estimator on large samples.</p>
<p>Keep in mind that all those multiple steps ahead estimators take more time for the calculation, because the model needs to produce h-steps-ahead forecasts from each observation in sample.</p>
<ul>
<li>Analytical multiple steps ahead estimators.</li>
</ul>
<p>There is also a non-documented feature in <span class="lang:r decode:true crayon-inline">smooth</span> functions (currently available only for pure additive models) – analytical versions of multiple steps ahead estimators. In order to use it, we need to add “a” in front of the desired estimator: aMSE\(_h\), aTMSE, aGTMSE. In this case only one-step-ahead forecast error is produced and after that the structure of the applied state-space model is used in order to reconstruct multiple steps ahead estimators. This feature is useful if you want to use the multiple steps ahead estimator on small samples, where the multi-steps errors cannot be calculated appropriately. It is also useful in cases of large samples, when the time of estimation is important.</p>
<p>These estimators have similar properties to their empirical counterparts, but work faster and are based on asymptotic properties. Here is an example of analytical MSE\(_h\) estimator:</p>
<pre class="decode">ourModel <- es(x,"AAN",silent=F,interval="p",h=18,holdout=T,loss="aMSEh")</pre>
<div id="attachment_1377" style="width: 310px" class="wp-caption alignnone"><a href="/wp-content/uploads/2017/10/SmoothEstimators09.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-1377" src="/wp-content/uploads/2017/10/SmoothEstimators09-300x175.png" alt="" width="300" height="175" class="size-medium wp-image-1377" srcset="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators09-300x175.png&amp;nocache=1 300w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators09-768x448.png&amp;nocache=1 768w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators09-1024x597.png&amp;nocache=1 1024w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/10/SmoothEstimators09.png&amp;nocache=1 1200w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-1377" class="wp-caption-text">N1823 and ETS(A,A,N) with aMSEh</p></div>
<pre>Time elapsed: 0.11 seconds
Model estimated: ETS(AAN)
Persistence vector g:
alpha  beta 
    0     0 
Initial values were optimised.
5 parameters were estimated in the process
Residuals standard deviation: 627.818
Cost function type: aMSEh; Cost function value: 375907.976

Information criteria:
     AIC     AICc      BIC 
30652.15 30662.74 30893.55 
95% parametric prediction intervals were constructed
100% of values are in the prediction interval
Forecast errors:
MPE: -1.9%; Bias: -14.6%; MAPE: 11.7%; SMAPE: 11.6%
MASE: 0.643; sMAE: 10%; RelMAE: 0.787; sMSE: 1.3%</pre>
<p>The resulting smoothing parameters are shrunk towards zero, similar to MSE\(_h\), but the initial values are slightly different, which leads to different forecasts. Note that the time elapsed in this case is 0.11 seconds instead of 0.24 as in MSE\(_h\). The difference in time may increase with the increase of sample size and forecasting horizon.</p>
<ul>
<li>Similar to MSE, there are empirical multi-step MAE and HAM in <span class="lang:r decode:true crayon-inline">smooth</span> functions (e.g. MAE\(_h\) and THAM). However, they are currently implemented mainly “because I can” and for fun, so I cannot give you any recommendations about them.</li>
</ul>
<ul>
<li>Starting from v2.4.0, a new estimator was introduced, "Mean Squared Cumulative Error" - MSCE, which may be useful in cases, when the cumulative demand is of the interest rather than point or trace ones.</li>
</ul>
<h3>Conclusions</h3>
<p>Now that we discussed all the possible estimators that you can use with <span class="lang:r decode:true crayon-inline">smooth</span>, you are most probably confused and completely lost. The question that may naturally appear after you have read this post is “What should I do?” Frankly speaking, I cannot give you appropriate answer and a set of universal recommendations, because this is still under-researched problem. However, I have some advice.</p>
<p>First, <a href="http://kourentzes.com/forecasting/" rel="noopener noreferrer" target="_blank">Nikos Kourentzes</a> and <a href="http://blog.uclm.es/juanramontrapero/" rel="noopener noreferrer" target="_blank">Juan Ramon Trapero</a> <a href="http://kourentzes.com/forecasting/2015/08/20/forecasting-solar-irradiance-true-models-trace-optimisation-and-shrinkage/" rel="noopener noreferrer" target="_blank">found that</a> in case of high frequency data (they used solar irradiation data) using MSE\(_h\) and TMSE leads to the increase in forecasting accuracy in comparison with the conventional MSE. However in order to achieve good accuracy in case of MSE\(_h\), you need to estimate \(h\) separate models, while with TMSE you need to estimate only one. So, TMSE is faster than MSE\(_h\), but at the same time leads to at least as accurate forecasts as in case of MSE\(_h\) for all the steps from 1 to h.</p>
<p>Second, if you have asymmetrically distributed residuals in the model after using MSE, give MAE or HAM a try – they may improve your model and its accuracy.</p>
<p>Third, analytical counterparts of multi-step estimators can be useful in one of the two situations: 1. When you deal with very large samples (e.g. high frequency data), want to use advanced estimation methods, but want them to work fast. 2. When you work with small sample, but want to use the properties of these estimators anyway.</p>
<p>Finally, don’t use MSE\(_h\), TMSE and GTMSE if you are interested in the values of parameters of models – they will almost surely be inefficient and biased. This applies to both ETS and ARIMA models, which will become close to their deterministic counterparts in this case. Use conventional MSE instead.</p>
<p>Message <a href="https://openforecast.org/2017/11/20/smooth-package-for-r-common-ground-part-ii-estimators/">&#8220;smooth&#8221; package for R. Common ground. Part II. Estimators</a> first appeared on <a href="https://openforecast.org">Open Forecasting</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://openforecast.org/2017/11/20/smooth-package-for-r-common-ground-part-ii-estimators/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>&#8220;smooth&#8221; package for R. Common ground. Part I. Prediction intervals</title>
		<link>https://openforecast.org/2017/06/11/smooth-package-for-r-prediction-intervals/</link>
					<comments>https://openforecast.org/2017/06/11/smooth-package-for-r-prediction-intervals/#respond</comments>
		
		<dc:creator><![CDATA[Ivan Svetunkov]]></dc:creator>
		<pubDate>Sun, 11 Jun 2017 13:23:40 +0000</pubDate>
				<category><![CDATA[Applied forecasting]]></category>
		<category><![CDATA[Common parameters]]></category>
		<category><![CDATA[Package smooth for R]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Univariate models]]></category>
		<category><![CDATA[ARIMA]]></category>
		<category><![CDATA[CES]]></category>
		<category><![CDATA[ETS]]></category>
		<category><![CDATA[smooth]]></category>
		<guid isPermaLink="false">https://openforecast.org/?p=984</guid>

					<description><![CDATA[<p>UPDATE: Starting from v2.5.1 the parameter intervals has been renamed into interval for the consistency purposes with the other R functions. We have spent previous six posts discussing basics of es() function (underlying models and their implementation). Now it is time to move forward. Starting from this post we will discuss common parameters, shared by [&#8230;]</p>
<p>Message <a href="https://openforecast.org/2017/06/11/smooth-package-for-r-prediction-intervals/">&#8220;smooth&#8221; package for R. Common ground. Part I. Prediction intervals</a> first appeared on <a href="https://openforecast.org">Open Forecasting</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><strong>UPDATE</strong>: Starting from v2.5.1 the parameter <code>intervals</code> has been renamed into <code>interval</code> for the consistency purposes with the other R functions.</p>
<p>We have spent previous six posts discussing basics of <code>es()</code> function (underlying models and their implementation). Now it is time to move forward. Starting from this post we will discuss common parameters, shared by all the forecasting functions implemented in smooth. This means that the topics that we discuss are not only applicable to <code>es()</code>, but also to <code>ssarima()</code>, <code>ces()</code>, <code>gum()</code> and <code>sma()</code>. However, taking that we have only discussed ETS so far, we will use <code>es()</code> in our examples for now.</p>
<p>And I would like to start this series of general posts from the topic of prediction intervals.</p>
<h3>Prediction intervals for smooth functions</h3>
<p>One of the features of <code>smooth</code> functions is their ability to produce different types of prediction intervals. Parametric prediction intervals (triggered by <code>interval="p"</code>, <code>interval="parametric"</code> or <code>interval=TRUE</code>) are derived analytically only for <a href="/en/2016/11/02/smooth-package-for-r-es-function-part-ii-pure-additive-models/">pure additive</a> and <a href="/en/2016/11/18/smooth-package-for-r-es-function-part-iii-multiplicative-models/">pure multiplicative</a> models and are based on the state-space model discussed in previous posts. In the current <code>smooth</code> version (v2.0.0) only <code>es()</code> function has multiplicative components, all the other functions are based on additive models. This makes <code>es()</code> &#8220;special&#8221;. While constructing intervals for pure models (either additive or multiplicative) is relatively easy to do, the mixed models cause pain in the arse (one of the reasons why I don&#8217;t like them). So in case of <a href="/en/2017/01/24/smooth-package-for-r-es-function-part-iv-model-selection-and-combination-of-forecasts/">mixed ETS models</a>, we have to use several tricks.</p>
<p>If the model has multiplicative error, non-multiplicative other components (trend, seasonality) and low variance of the error (smaller than 0.1), then the intervals can be approximated by similar models with additive error term. For example, the intervals for ETS(M,A,N) can be approximated with intervals of ETS(A,A,N), when the variance is low, because the distribution of errors in both models will be similar. In all the other cases we use simulations for prediction intervals construction (via <code>sim.es()</code> function). In this case the data is generated with preset parameters (including variance) and contains \(h\) observations. This process is repeated 10,000 times, resulting in 10,000 possible trajectories. After that the necessary quantiles of these trajectories for each step ahead are taken using <code>quantile()</code> function from <code>stats</code> package and returned as prediction intervals. This cannot be considered as a pure parametric approach, but it is the closest we have.</p>
<p><code>smooth</code> functions also introduce semiparametric and nonparametric prediction intervals. Both of them are based on multiple steps ahead (also sometimes called as &#8220;trace&#8221;) forecast errors. These are obtained via producing forecasts for horizon 1 to \(h\) from each observation of time series. As a result a matrix with \(h\) columns and \(T-h\) rows is produced. In case of semi-parametric intervals (called using <code>interval="sp"</code> or <code>interval="semiparametric"</code>), variances of forecast errors for each horizon are calculated and then used in order to extract quantiles of either normal or log-normal distribution (depending on error type). This way we cover possible violation of assumptions of homoscedasticity and no autocorrelation in residuals, but we still assume that each separate observation has some parametric distribution.</p>
<p>In case of non-parametric prediction intervals (defined in R via <code>interval="np"</code> or <code>interval="nonparametric"</code>), we loosen assumptions further, dropping part about distribution of residuals. In this case quantile regressions are used as proposed by <a href="https://www.jstor.org/stable/2634872?seq=1#page_scan_tab_contents" target="_blank" rel="noopener noreferrer">Taylor and Bunn, 1999</a>. However we use a different form of regression model than the authors do:<br />
\begin{equation} \label{eq:ssTaylorPIs}<br />
	\hat{e}_{j} = a_0 j ^ {a_{1}},<br />
\end{equation}<br />
where \(j = 1, .., h\) is forecast horizon. This function has an important advantage over the proposed by the authors second order polynomial: it does not have extremum (turning point) for \(j>0\), which means that the intervals won&#8217;t behave strangely after several observations ahead. Using polynomials for intervals sometimes leads to weird bounds (for example, expanding and then shrinking). On the other hand, power function allows producing wide variety of forecast trajectories, which correspond to differently increasing or decreasing bounds of prediction intervals (depending on values of \(a_0\) and \(a_1\)), without producing any ridiculous trajectories.</p>
<p>The main problem with nonparametric intervals produced by <code>smooth</code> is caused by quantile regressions, which do not behave well on small samples. In order to produce correct 0.95 quantile, we need to have at least 20 observations, and if we want 0.99 quantile, then the sample must contain at least 100. In the cases, when there is not enough observations, the produced intervals can be inaccurate and may not correspond to the nominal level values.</p>
<p>As a small note, if a user produces only one-step-ahead forecast, then semiparametric interval will correspond to parametric one (because only the variance of the one-step-ahead error is used), and the nonparametric interval is constructed using <code>quantile()</code> function from <code>stats</code> package.</p>
<p>Finally, the width of prediction intervals is regulated by parameter <code>level</code>, which can be written either as a fraction number (<code>level=0.95</code>) or as an integer number, less than 100 (<code>level=95</code>). I personally prefer former, but the latter is needed for the consistency with <code>forecast</code> package functions. By default all the <code>smooth</code> functions produce 95% prediction intervals.</p>
<p>There are some other features of prediction interval construction for specific intermittent models and cumulative forecasts, but they will be covered in upcoming posts.</p>
<h3>Examples in R</h3>
<p>We will use a time series N1241 as an example and we will estimate model ETS(A,Ad,N). Here&#8217;s how we do that:</p>
<pre class="decode" title="Examples of usage">
ourModel1 <- es(M3$N1241$x, "AAdN", h=8, holdout=TRUE, interval="p")
ourModel2 <- es(M3$N1241$x, "AAdN", h=8, holdout=TRUE, interval="sp")
ourModel3 <- es(M3$N1241$x, "AAdN", h=8, holdout=TRUE, interval="np")</pre>
<p>The resulting graphs demonstrate some differences in prediction intervals widths and shapes:</p>
<div id="attachment_960" style="width: 310px" class="wp-caption alignnone"><a href="/wp-content/uploads/2016/10/N1241-AAdN-PI-parametric.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-960" src="/wp-content/uploads/2016/10/N1241-AAdN-PI-parametric-300x175.png" alt="Series N1241 from M3, es() forecast, parametric prediction intervals" width="300" height="175" class="size-medium wp-image-960" srcset="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2016/10/N1241-AAdN-PI-parametric-300x175.png&amp;nocache=1 300w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2016/10/N1241-AAdN-PI-parametric-768x448.png&amp;nocache=1 768w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2016/10/N1241-AAdN-PI-parametric-1024x597.png&amp;nocache=1 1024w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2016/10/N1241-AAdN-PI-parametric.png&amp;nocache=1 1200w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-960" class="wp-caption-text">Series N1241 from M3, es() forecast, parametric prediction intervals</p></div>
<div id="attachment_959" style="width: 310px" class="wp-caption alignnone"><a href="/wp-content/uploads/2016/10/N1241-AAdN-PI-semiparametric.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-959" src="/wp-content/uploads/2016/10/N1241-AAdN-PI-semiparametric-300x175.png" alt="Series N1241 from M3, es() forecast, semi-parametric prediction intervals" width="300" height="175" class="size-medium wp-image-959" srcset="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2016/10/N1241-AAdN-PI-semiparametric-300x175.png&amp;nocache=1 300w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2016/10/N1241-AAdN-PI-semiparametric-768x448.png&amp;nocache=1 768w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2016/10/N1241-AAdN-PI-semiparametric-1024x597.png&amp;nocache=1 1024w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2016/10/N1241-AAdN-PI-semiparametric.png&amp;nocache=1 1200w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-959" class="wp-caption-text">Series N1241 from M3, es() forecast, semiparametric prediction intervals</p></div>
<div id="attachment_958" style="width: 310px" class="wp-caption alignnone"><a href="/wp-content/uploads/2016/10/N1241-AAdN-PI-nonparametric.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-958" src="/wp-content/uploads/2016/10/N1241-AAdN-PI-nonparametric-300x175.png" alt="Series N1241 from M3, es() forecast, non-parametric prediction intervals" width="300" height="175" class="size-medium wp-image-958" srcset="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2016/10/N1241-AAdN-PI-nonparametric-300x175.png&amp;nocache=1 300w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2016/10/N1241-AAdN-PI-nonparametric-768x448.png&amp;nocache=1 768w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2016/10/N1241-AAdN-PI-nonparametric-1024x597.png&amp;nocache=1 1024w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2016/10/N1241-AAdN-PI-nonparametric.png&amp;nocache=1 1200w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-958" class="wp-caption-text">Series N1241 from M3, es() forecast, nonparametric prediction intervals</p></div>
<p>All of them cover actual values in the holdout, because the intervals are very wide. It is not obvious, which of them is the most appropriate for this task. So we can calculate the spread of intervals and see, which of them is on average wider:</p>
<pre class="decode" title="Spread of intervals">
mean(ourModel1$upper-ourModel1$lower)
mean(ourModel2$upper-ourModel2$lower)
mean(ourModel3$upper-ourModel3$lower)
</pre>
<p>Which results in:</p>
<pre>950.4171
955.0831
850.614</pre>
<p>In this specific example, the non-parametric interval appeared to be the narrowest, which is good, taking that it adequately covered values in the holdout sample. However, this doesn't mean that it is in general superior to the other methods. Selection of the appropriate intervals should be done based on the general understanding of the violated assumptions. If we didn't know the actual values in the holdout sample, then we could make a decision based on the analysis of the in-sample residuals in order to get a clue about the violation of any assumptions. This can be done, for example, this way:</p>
<pre class="decode" title="Graphs of residuals">
forecast::tsdisplay(ourModel1$residuals)

hist(ourModel1$residuals)

qqnorm(ourModel3$residuals)
qqline(ourModel3$residuals)</pre>
<div id="attachment_1254" style="width: 310px" class="wp-caption alignnone"><a href="/wp-content/uploads/2017/05/N1241-residuals-plot.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-1254" src="/wp-content/uploads/2017/05/N1241-residuals-plot-300x175.png" alt="" width="300" height="175" class="size-medium wp-image-1254" srcset="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/05/N1241-residuals-plot-300x175.png&amp;nocache=1 300w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/05/N1241-residuals-plot-768x448.png&amp;nocache=1 768w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/05/N1241-residuals-plot-1024x597.png&amp;nocache=1 1024w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/05/N1241-residuals-plot.png&amp;nocache=1 1200w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-1254" class="wp-caption-text">Linear plot and correlation functions of the residuals of the ETS(A,Ad,N) model</p></div>
<div id="attachment_1248" style="width: 310px" class="wp-caption alignnone"><a href="/wp-content/uploads/2017/05/N1241-residuals-histogram.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-1248" src="/wp-content/uploads/2017/05/N1241-residuals-histogram-300x175.png" alt="" width="300" height="175" class="size-medium wp-image-1248" srcset="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/05/N1241-residuals-histogram-300x175.png&amp;nocache=1 300w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/05/N1241-residuals-histogram-768x448.png&amp;nocache=1 768w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/05/N1241-residuals-histogram-1024x597.png&amp;nocache=1 1024w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/05/N1241-residuals-histogram.png&amp;nocache=1 1200w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-1248" class="wp-caption-text">Histogram of the residuals of the ETS(A,Ad,N) model</p></div>
<div id="attachment_1250" style="width: 310px" class="wp-caption alignnone"><a href="/wp-content/uploads/2017/05/N1241-residuals-qqplot.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-1250" src="/wp-content/uploads/2017/05/N1241-residuals-qqplot-300x175.png" alt="" width="300" height="175" class="size-medium wp-image-1250" srcset="https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/05/N1241-residuals-qqplot-300x175.png&amp;nocache=1 300w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/05/N1241-residuals-qqplot-768x448.png&amp;nocache=1 768w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/05/N1241-residuals-qqplot-1024x597.png&amp;nocache=1 1024w, https://openforecast.org/wp-content/webpc-passthru.php?src=https://openforecast.org/wp-content/uploads/2017/05/N1241-residuals-qqplot.png&amp;nocache=1 1200w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-1250" class="wp-caption-text">Q-Q plot of the residuals of the ETS(A,Ad,N) model</p></div>
<p>The first plot shows how residuals change over time and how the autocorrelation and partial autocorrelation functions look for this time series. There is no obvious autocorrelation and no obvious heteroscedasticity in the residuals. This means that we can assume that these conditions are not violated in the model, so there is no need to use semiparametric prediction intervals. However, the second and the third graphs demonstrate that the residuals are not normally distributed (as assumed by the model ETS(A,Ad,N)). This means that parametric prediction intervals may be wrong for this time series. All of this motivates the usage of nonparametric prediction intervals for the series N1241.</p>
<p>That's it for today.</p>
<p>Message <a href="https://openforecast.org/2017/06/11/smooth-package-for-r-prediction-intervals/">&#8220;smooth&#8221; package for R. Common ground. Part I. Prediction intervals</a> first appeared on <a href="https://openforecast.org">Open Forecasting</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://openforecast.org/2017/06/11/smooth-package-for-r-prediction-intervals/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
