<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/forecast-8-0/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://robjhyndman.com/hyndsight/forecast8/"> R – Hyndsight</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In what is now a roughly annual event, the forecast package has been updated on CRAN with a new version, this time 8.0.</p>
<p>A few of the more important new features are described below.</p>
<h3>Check residuals</h3>
<p>A common task when building forecasting models is to check that the residuals satisfy some assumptions (that they are uncorrelated, normally distributed, etc.). The new function <code>checkresiduals</code> makes this very easy: it produces a time plot, an ACF, a histogram with super-imposed normal curve, and does a Ljung-Box test on the residuals with appropriate number of lags and degrees of freedom.</p>
<div>
<table>
<tr>
<td>
<pre>fit <span>&lt;-</span> auto.<span>arima</span><span>(</span><span>WWWusage</span><span>)</span>
checkresiduals<span>(</span>fit<span>)</span></pre>
</td>
</tr>
</table>
</div>
<p><img src="https://i1.wp.com/robjhyndman.com/hyndsight/files/2017/02/unnamed-chunk-1-1.png?w=456" data-recalc-dims="1"></p>
<pre><code>## 
##  Ljung-Box test
## 
## data:  residuals
## Q* = 7.8338, df = 8, p-value = 0.4499
## 
## Model df: 2.   Total lags used: 10</code></pre>
<p>This should work for all the modelling functions in the package, as well as some of the time series modelling functions in the <code>stats</code> package.</p>
<h3>Different types of residuals</h3>
<p>Usually, residuals are computed as the difference between observations and the corresponding one-step forecasts. But for some models, residuals are computed differently; for example, a multiplicative ETS model or a model with a Box-Cox transformation. So the <code>residuals()</code> function now has an additional argument to deal with this situation.</p>
<p>“Innovation residuals”” correspond to the white noise process that drives the evolution of the time series model. “Response residuals” are the difference between the observations and the fitted values (as with GLMs). For homoscedastic models, the innovation residuals and the one-step response residuals are identical. “Regression residuals” are also available for regression models with ARIMA errors, and are equal to the original data minus the effect of the regression variables. If there are no regression variables, the errors will be identical to the original series (possibly adjusted to have zero mean).</p>
<div>
<table>
<tr>
<td>
<pre><span>library</span><span>(</span>ggplot2<span>)</span>
fit <span>&lt;-</span> ets<span>(</span>woolyrnq<span>)</span>
res <span>&lt;-</span> <span>cbind</span><span>(</span>Residuals <span>=</span> <span>residuals</span><span>(</span>fit<span>)</span>, 
             Response.<span>residuals</span> <span>=</span> <span>residuals</span><span>(</span>fit, type<span>=</span><span>'response'</span><span>)</span><span>)</span>
autoplot<span>(</span>res, facets<span>=</span>TRUE<span>)</span></pre>
</td>
</tr>
</table>
</div>
<p><img src="https://i1.wp.com/robjhyndman.com/hyndsight/files/2017/02/unnamed-chunk-2-1.png?w=456" data-recalc-dims="1"></p>
<h3>Some new graphs</h3>
<p>The <code>geom_histogram()</code> function in the <code>ggplot2</code> package is nice, but it does not have a good default bandwidth. So I added the <code>gghistogram</code> function which provides a quick histogram with good defaults. You can also overlay a normal density curve or a kernel density estimate.</p>
<div>
<table>
<tr>
<td>
<pre>gghistogram<span>(</span><span>lynx</span><span>)</span></pre>
</td>
</tr>
</table>
</div>
<p><img src="https://i0.wp.com/robjhyndman.com/hyndsight/files/2017/02/unnamed-chunk-3-1.png?w=456" data-recalc-dims="1"></p>
<p>The <code>ggseasonplot</code> function is useful for studying seasonal patterns and how they change over time. It now has a <code>polar</code> argument to create graphs like this.</p>
<div>
<table>
<tr>
<td>
<pre>ggseasonplot<span>(</span><span>USAccDeaths</span>, polar<span>=</span>TRUE<span>)</span></pre>
</td>
</tr>
</table>
</div>
<p><img src="https://i0.wp.com/robjhyndman.com/hyndsight/files/2017/02/unnamed-chunk-4-1.png?w=456" data-recalc-dims="1"></p>
<p>I often want to add a time series line to an existing plot. Base graphics has <code>line()</code> which works well when a time series is passed as an argument. So I added <code>autolayer</code> which is similar (but more general). It is an S3 method like <code>autoplot</code>, and adds a layer to an existing <code>ggplot</code> object. <code>autolayer</code> will eventually form part of the next release of <code>ggplot2</code>, but for now it is available in the <code>forecast</code> package. There are methods provided for <code>ts</code> and <code>forecast</code> objects:</p>
<div>
<table>
<tr>
<td>
<pre><span>WWWusage</span> <span>%>%</span> ets <span>%>%</span> forecast<span>(</span>h<span>=</span><span>20</span><span>)</span> <span>-&amp;</span>gt<span>;</span> fc
autoplot<span>(</span><span>WWWusage</span>, series<span>=</span><span>&quot;Data&quot;</span><span>)</span> <span>+</span> 
  autolayer<span>(</span>fc, series<span>=</span><span>&quot;Forecast&quot;</span><span>)</span> <span>+</span> 
  autolayer<span>(</span><span>fitted</span><span>(</span>fc<span>)</span>, series<span>=</span><span>&quot;Fitted&quot;</span><span>)</span></pre>
</td>
</tr>
</table>
</div>
<p><img src="https://i1.wp.com/robjhyndman.com/hyndsight/files/2017/02/unnamed-chunk-5-1.png?w=456" data-recalc-dims="1"></p>
<h3>Cross-validation</h3>
<p>The <code>tsCV</code> and <code>CVar</code> functions have been added. These were discussed in a <a target="_blank" rel="nofollow" href="http://robjhyndman.com/hyndsight/tscv/">previous post</a>.</p>
<h3>Bagged ETS</h3>
<p>The <code>baggedETS</code> function has been added, which implements the procedure discussed in <a target="_blank" rel="nofollow" href="http://robjhyndman.com/papers/bagging-ets/">Bergmeir et al (2016)</a> for bagging ETS forecasts.</p>
<h3>head and tail of time series</h3>
<p>I’ve long found it annoying that <code>head</code> and <code>tail</code> do not work on multiple time series. So I added some functions to the package so they now work.</p>
<h3>Imports and Dependencies</h3>
<p>The pipe operator from the <code>magrittr</code> package is now imported. So you don’t need to load the <code>magrittr</code> package to use it.</p>
<p>There are now no packages that are loaded with <code>forecast</code> – everything required is imported. This makes the start up much cleaner (no more annoying messages from all those packages being loaded). Instead, some random tips are occasionally printed when you load the forecast package (much like <code>ggplot2</code> does).</p>
<p>There is quite a bit more — see the <a target="_blank" rel="nofollow" href="https://github.com/robjhyndman/forecast/blob/master/ChangeLog">Changelog</a> for a list.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://robjhyndman.com/hyndsight/forecast8/"> R – Hyndsight</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/b21D_Z2P3_A"></a></div>