<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/tsstudio-0-1-3/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://ramikrispin.github.io/2018/12/tsstudio-0.1.3/"> Rami Krispin</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><br>
<br>
<br>




</p>
<p>I used the Thanksgiving break to push a new update of the <a rel="nofollow" href="https://github.com/RamiKrispin/TSstudio" target="_blank">TSstudio</a> package to CRAN (version 0.1.3). The new version includes an update for the <code>ts_backtesting</code> function along with two new function – <code>ts_to_prophet</code> for converting time series objects to a <a rel="nofollow" href="https://facebook.github.io/prophet/docs/quick_start.html#r-api" target="_blank">prophet</a> input format (i.e., <code>ds</code> and <code>y</code> columns), and <code>ccf_plot</code> for lags plot between two time series. The package can be installed from either CRAN or Github:</p>
<pre><code># CRAN
install.packages(&quot;TSstudio&quot;)

# Github
# install.packages(&quot;devtools&quot;)
devtools::install_github(&quot;RamiKrispin/TSstudio&quot;)</code></pre>
<pre><code>library(TSstudio)
packageVersion(&quot;TSstudio&quot;)</code></pre>
<pre><code>## [1] '0.1.3'</code></pre>
<div>
<h4>Converting time series object to a prophet format</h4>
<p>The <code>ts_to_prophet</code> function converting <code>ts</code>, <code>xts</code> and <code>zoo</code> objects into prophet input format (i.e., data frame with two columns – ds for date and y for the series values). For instance, convertig the <code>USgas</code> series to a prophet object:</p>
<pre><code>data(&quot;USgas&quot;)

ts_info(USgas)</code></pre>
<pre><code>##  The USgas series is a ts object with 1 variable and 225 observations
##  Frequency: 12 
##  Start time: 2000 1 
##  End time: 2018 9</code></pre>
<pre><code>USgas_prophet &lt;- ts_to_prophet(USgas)

head(USgas)</code></pre>
<pre><code>## [1] 2510.5 2330.7 2050.6 1783.3 1632.9 1513.1</code></pre>
<pre><code>head(USgas_prophet)</code></pre>
<pre><code>##           ds      y
## 1 2000-01-01 2510.5
## 2 2000-02-01 2330.7
## 3 2000-03-01 2050.6
## 4 2000-04-01 1783.3
## 5 2000-05-01 1632.9
## 6 2000-06-01 1513.1</code></pre>
<p>In the case of a <code>ts</code> object, where the index is not a date object, the function extracts the time component from the first observation and use it along with the frequency of the series to estimate the date column of the prophet data frame. For instance, in the case of a monthly series, where the time object provides only the year and the month, by default the day component of the date object will be set to 1. Alternatively, if known, you can set the date of the first observation with the <code>start</code> argument. For example, if the <code>USgas</code> series is being captured during the mid of the month (or every 15th of the month):</p>
<pre><code>USgas_prophet &lt;- ts_to_prophet(USgas, start = as.Date(&quot;2000-01-15&quot;))

head(USgas_prophet)</code></pre>
<pre><code>##           ds      y
## 1 2000-01-15 2510.5
## 2 2000-02-15 2330.7
## 3 2000-03-15 2050.6
## 4 2000-04-15 1783.3
## 5 2000-05-15 1632.9
## 6 2000-06-15 1513.1</code></pre>
<p>Similarly, the function can handle <code>xts</code> and <code>zoo</code> objects:</p>
<pre><code>data(&quot;EURO_Brent&quot;)
ts_info(EURO_Brent)</code></pre>
<pre><code>##  The EURO_Brent series is a zoo object with 1 variable and 378 observations
##  Frequency: monthly 
##  Start time: May 1987 
##  End time: Oct 2018</code></pre>
<pre><code>head(EURO_Brent)</code></pre>
<pre><code>## May 1987 Jun 1987 Jul 1987 Aug 1987 Sep 1987 Oct 1987 
##    18.58    18.86    19.86    18.98    18.31    18.76</code></pre>
<pre><code>ts_to_prophet(EURO_Brent) %>% head()</code></pre>
<pre><code>##           ds     y
## 1 1987-05-01 18.58
## 2 1987-06-01 18.86
## 3 1987-07-01 19.86
## 4 1987-08-01 18.98
## 5 1987-09-01 18.31
## 6 1987-10-01 18.76</code></pre>
</div>
<div>
<h4>Lags plots of two series</h4>
<p>The second function, <code>ccf_plot</code>, provides an interactive and intuitive visualization of the cross-correlation between two time series, by plotting a series against another series (and its lags) and calculating the correlation between the two with the <code>ccf</code> function. For instance, let’s use the function to plot the relationship between the unemployment rate and the total vehicle sales in the US:</p>
<pre><code>data(&quot;USUnRate&quot;)

ts_info(USUnRate)</code></pre>
<pre><code>##  The USUnRate series is a ts object with 1 variable and 850 observations
##  Frequency: 12 
##  Start time: 1948 1 
##  End time: 2018 10</code></pre>
<pre><code>data(&quot;USVSales&quot;)

ts_info(USVSales)</code></pre>
<pre><code>##  The USVSales series is a ts object with 1 variable and 514 observations
##  Frequency: 12 
##  Start time: 1976 1 
##  End time: 2018 10</code></pre>
<pre><code>ccf_plot(x = USVSales, y = USUnRate)</code></pre>
<div></div>
<p></p>
<p>The function automatically aligned and used only the overlapping observations of the two series before calculating the cross-correlation values between the series and the lags of the second series (where the 0 lag represents the series itself, and negative lags represent the leading lags). The title of each plot specifies the lag number and the cross-correlation value. The <code>lags</code> argument of the function defines the number of lags in the plot, where the use of negative lags defines the leading indicators. For example, setting the <code>lags</code> argument to -6:6 will plot the first 6 lags, the series itself and the first 6 leading lags of the series:</p>
<pre><code>ccf_plot(x = USVSales, y = USUnRate, lags = -6:6)</code></pre>
<div></div>
<p>
</div>
<div>
<h4>Forecasting with backtesting and xreg</h4>
<p>The <code>ts_backtesting</code> function for training and testing multiple models (e.g., <code>auto.arima</code>, <code>HoltWinters</code>, <code>nnetar</code>, etc.) with backtesting approach, is now supporting the <code>xreg</code> component of the <code>auto.arima</code>, <code>nnetar</code> ( <a rel="nofollow" href="https://github.com/robjhyndman/forecast" target="_blank">forecast</a> package)and their embedment in the <code>hybridModel</code> model ( <a rel="nofollow" href="https://github.com/ellisp/forecastHybrid" target="_blank">forecastHybrid</a> package). The use of the <code>xreg</code> component is straightforward and required two components:</p>
<ul>
<li>The predictors – or the regressors component in a vector or matric format will be used as an input to the model <code>xreg</code> argument. The length of this input must be aligned with the length of the input series</li>
<li>The future values of the predictors – a vector or matrix must correspond to the inputs which used as predictors, where the length of this component must be aligned to the forecast horizon (or the <code>h</code> argument of the function). This setting of this component is done with the <code>xreg.h</code> argument</li>
</ul>
<p>For instance, let’s forecast the monthly consumption of natural gas in US in the next 5 years (or 60 months) by regressing the <code>USgas</code> series with its Fourier terms, using <code>auto.arima</code>, <code>nnetar</code> and <code>hybridModel</code> models. We will use the <code>fourier</code> function from the forecast package to generate both the inputs for the regression model (<code>x_reg</code>) and future values for the forecast itself (<code>x_reg.forecast</code>):</p>
<pre><code># Setting the forecast horizon
h &lt;- 60

library(forecast)
# Creating the xreg component for the regression
x_reg &lt;- fourier(USgas, K = 5)

# Creating the xreg component for the forecast 
x_reg.forecast &lt;- forecast::fourier(USgas, K = 5, h = h)</code></pre>
<p>Note that the <code>ts_backtesting</code> function automatically split and aligned the <code>xreg</code> component according to the expanding window movement of the function. We will set the function to run backtesting using 6 periods/splits to train <code>auto.arima</code>, <code>nnetar</code> and <code>hybridModel</code> models, in order to examine the performance of the models over time:</p>
<pre><code>md &lt;- ts_backtesting(ts.obj = USgas,
                     error = &quot;MAPE&quot;,
                     models = &quot;anh&quot;,
                     periods = 6,
                     h = h,
                     xreg.h = x_reg.forecast,
                     a.arg = list(xreg = x_reg),
                     h.arg = list(models = &quot;aetsfz&quot;, 
                                  a.args = list(xreg = x_reg), 
                                  verbose = FALSE),
                     n.arg = list(xreg = x_reg),
                     plot = FALSE)</code></pre>
<pre><code>##   Model_Name  avgMAPE   sdMAPE  avgRMSE   sdRMSE
## 1 auto.arima 4.713333 1.448857 123.5483 33.67451
## 2     hybrid 4.786667 1.545039 122.6517 34.40697
## 3     nnetar 9.158333 2.652051 224.1967 60.38386</code></pre>
<p>We can now review the performance of each model using the summary plot:</p>
<pre><code>md$summary_plot</code></pre>
<div></div>
<p></p>
<p>The summary plot provides the error distribution of each model and the plot forecasting model which performed best on the backtesting. The output contains the models’ performance on the backtesting (i.e., summary plot and leaderboard). In this case, since we set the <code>error</code> argument to <code>MAPE</code>, the function selected the <code>auto.arima</code> final forecast. Yet, you can see in the plot that the error rate of the <code>hybrid</code> model is more stable compared to the <code>auto.arima</code> and it might be a better choice (the <code>hybrid</code> contains both the <code>auto.arima</code> and other models, which potentially helps to hedge the error). All the models’ information available on the <code>Forecast_Final</code> folder. For example, you can pull the <code>auto.arima</code> model and check its residuals:</p>
<pre><code>check_res(md$Forecast_Final$auto.arima)</code></pre>
<div></div>
<p></p>
<p>The plan for future releases is to expend the functionality of the <code>ts_backtesting</code> function, by adding additional models (e.g., <code>tslm</code>, <code>prophet</code>, etc.) and expend the window setting of the backtesting (adding sliding window option).</p>
</div>
	<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://ramikrispin.github.io/2018/12/tsstudio-0.1.3/"> Rami Krispin</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/f-RvOB-p07s" height="1"></a></div>