<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/smooth-package-for-r-common-ground-part-iii-exogenous-variables-basic-stuff/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://forecasting.svetunkov.ru/en/2018/01/15/smooth-package-for-r-common-ground-part-iii-exogenous-variables-basic-stuff/"> R – Modern Forecasting</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<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 </p>
<pre>arima()</pre>
<p> function from </p>
<pre>stats</pre>
<p> package allows inserting exogenous variables, </p>
<pre>ets()</pre>
<p> function from </p>
<pre>forecast</pre>
<p> 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 </p>
<pre>smooth</pre>
<p> package (except for </p>
<pre>sma()</pre>
<p>) allow using exogenous variables, so this feature is not restricted with </p>
<pre>es()</pre>
<p> only.</p>
<p>There are two types of models with exogenous variables implemented in </p>
<pre>smooth</pre>
<p> 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’ 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 rel="nofollow" href="https://forecasting.svetunkov.ru/en/2016/11/02/smooth-package-for-r-es-function-part-ii-pure-additive-models/" target="_blank">the previous posts</a>.</p>
<p>The second model is formulated differently, because it is driven by <a rel="nofollow" href="https://forecasting.svetunkov.ru/en/2016/11/18/smooth-package-for-r-es-function-part-iii-multiplicative-models/" target="_blank">the multiplication of the ETS components</a> by error term:<br>
\begin{equation} \label{eq:multiplicative}<br>
\log y_t = w’ \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’ \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 rel="nofollow" href="https://forecasting.svetunkov.ru/en/2016/11/02/smooth-package-for-r-es-function-part-ii-pure-additive-models/" target="_blank">pure additive</a> or <a rel="nofollow" href="https://forecasting.svetunkov.ru/en/2016/11/18/smooth-package-for-r-es-function-part-iii-multiplicative-models/" target="_blank">pure multiplicative</a> models, ignoring their combinations (see <a rel="nofollow" href="https://forecasting.svetunkov.ru/en/2017/01/24/smooth-package-for-r-es-function-part-iv-model-selection-and-combination-of-forecasts/" target="_blank">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>ourModel &lt;- es(BJsales, &quot;XXN&quot;, 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 class="wp-caption"><a rel="nofollow" href="https://i0.wp.com/forecasting.svetunkov.ru/wp-content/uploads/2018/01/BJsalesETSXSimple.png?ssl=1" target="_blank"><img sizes="(max-width: 300px) 100vw, 300px" src="https://i1.wp.com/forecasting.svetunkov.ru/wp-content/uploads/2018/01/BJsalesETSXSimple-300x175.png?resize=300%2C175&ssl=1" alt="" width="300" class="wp-image-1623" data-recalc-dims="1" height="175"></a>
<p class="wp-caption-text">BJsales series and ETSX with a leading indicator</p>
</div>
<p>In this example we use sales data from Box &amp; 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 </p>
<pre>BJsales.lead</pre>
<p> 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 </p>
<pre>formula()</pre>
<p> function, which in case of </p>
<pre>smooth</pre>
<p> 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>formula(ourModel)</pre>
<pre>&quot;y[t] = l[t-1] + b[t-1] + a1 * x[t] + e[t]&quot;</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 </p>
<pre>lm()</pre>
<p> function).</p>
<p>Just for fun, let’s specify a weird mixed model and see its formula:</p>
<pre>ourModel &lt;- es(BJsales, &quot;MAN&quot;, xreg=BJsales.lead, h=10, holdout=TRUE)
formula(ourModel)</pre>

<pre>&quot;y[t] = (l[t-1] + b[t-1]) * exp(a1 * x[t]) * e[t]&quot;</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 </p>
<pre>smooth</pre>
<p> functions will automatically produce forecasts using </p>
<pre>es()</pre>
<p> 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 </p>
<pre>iss()</pre>
<p> 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 </p>
<pre>holdout=FALSE</pre>
<p>. Here’s how it works:</p>
<pre>es(BJsales, &quot;XXN&quot;, 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 </p>
<pre>smooth</pre>
<p> functions will cut off the redundant end of data. For example:</p>
<pre>ourModel &lt;- es(BJsales[1:140], &quot;XXN&quot;, 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><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 </p>
<pre>forecast()</pre>
<p> function together with </p>
<pre>smooth</pre>
<p> functions (which is not necessary at all), you can do it the following way:</p>
<pre>forecast(ourModel, h=10, xreg=BJsales.lead)</pre>
<p><p>Due to the implementation of exogenous variables in </p>
<pre>smooth</pre>
<p>, you need to provide the whole </p>
<pre>xreg</pre>
<p> (as much as you have) to the </p>
<pre>forecast()</pre>
<p> function. If you provide the values for the holdout only, the function will think that your </p>
<pre>xreg</pre>
<p> 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 </p>
<pre>es()</pre>
<p> (as discussed above).</p>
<p>Personally, I would advise using </p>
<pre>es()</pre>
<p>, </p>
<pre>ssarima()</pre>
<p> and other </p>
<pre>smooth</pre>
<p> functions directly, ignoring </p>
<pre>forecast()</pre>
<p>. This way you can prepare your </p>
<pre>xreg</pre>
<p>, and then use it directly without additional lines of code.</p>
<p>Similarly to how it was discussed in <a rel="nofollow" href="https://forecasting.svetunkov.ru/en/2017/06/11/smooth-package-for-r-prediction-intervals/" target="_blank">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 </p>
<pre>xreg</pre>
<p>.</p>
<p>Finally, you can always pre-specify the values of </p>
<pre>xreg</pre>
<p> parameters if you don’t want them to be estimated. This is controlled using </p>
<pre>initialX</pre>
<p> parameter:</p>
<pre>ourModel &lt;- es(BJsales, &quot;XXN&quot;, 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>es(BJsales, &quot;XXN&quot;, 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>es(BJsales, &quot;XXN&quot;, 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>es(BJsales, &quot;XXN&quot;, 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><p>That’s it for the basic exogenous variables functionality in </p>
<pre>smooth</pre>
<p> functions. Next time we will continue with more advanced, more fascinating stuff.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://forecasting.svetunkov.ru/en/2018/01/15/smooth-package-for-r-common-ground-part-iii-exogenous-variables-basic-stuff/"> R – Modern Forecasting</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/wr33F6VnPXk" height="1"></a></div>