<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/minor-updates-for-ggseas-and-tcomp-r-packages-by-ellis2013nz/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://freerangestats.info/blog/2018/06/15/ggseas-and-tcomp-update"> free range statistics - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Updates</h2>
<p>I’ve made small updates to two of my R packages on CRAN: <code>ggseas</code> (seasonal adjustment on the fly for <code>ggplot2</code> graphics) and <code>Tcomp</code> (tourism forecasting competition data). Neither of the packages changes in a noticeable way for most users.</p>
<ul>
<li>The <code>ggseas</code> update is to get it ready for the <a rel="nofollow" href="https://www.tidyverse.org/articles/2018/05/ggplot2-2-3-0/" target="_blank">coming release of <code>ggplot2</code> 2.3.0</a> scheduled for the end of June 2018. It makes changes under the hood to the <a rel="nofollow" href="https://community.rstudio.com/t/my-package-will-be-broken-on-cran-when-should-i-submit-the-fix/8708/7" target="_blank">mapping of variables to support “tidy evaluation”</a> but does not change anything for users. It will still work with older versions of <code>ggplot2</code>.</li>
<li>The <code>Tcomp</code> update fixes a <a rel="nofollow" href="https://github.com/ellisp/Tcomp-r-package/issues/12" target="_blank">problem</a> where the reported length of time series was incorrect.</li>
</ul>
<p>I’ve written more about <a rel="nofollow" href="http://freerangestats.info/blog/2016/10/12/ggsdc-rents" target="_blank"><code>ggseas</code></a> and <a rel="nofollow" href="http://freerangestats.info/blog/2016/10/19/Tcomp" target="_blank"><code>Tcomp</code></a> elsewhere. Given they’re both being updated at once, here’s a brief demo of them in action together.</p>
<p>There are 1,311 series in the list <code>tourism</code> in <code>Tcomp</code>. Each element of <code>tourism</code> contains some limited metadata about the series (for example its length, and how long the forecast is to be for), the original training data, and the “answer” in the form of the actual observations over the forecast period. These objects are of class <code>Mdata</code>, introduced in Rob Hyndmans’ <code>Mcomp</code> package, which comes with a convenient plotting method.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>scales</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggseas</span><span>)</span><span>
</span><span>library</span><span>(</span><span>Tcomp</span><span>)</span><span>
</span><span># default plot method for forecasting competition datasets of class Mdata
</span><span>par</span><span>(</span><span>bty</span><span> </span><span>=</span><span> </span><span>&quot;l&quot;</span><span>,</span><span> </span><span>font.main</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span>plot</span><span>(</span><span>tourism</span><span>[[</span><span>&quot;M4&quot;</span><span>]],</span><span> </span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Series M4 from the tourism forecasting competition&quot;</span><span>)</span></code></pre>
</figure>
<p><img width="100%" src="http://freerangestats.io/img/0124-plot.svg"></p>
<p><code>ggseas</code> makes it easy to take a seasonal time series object (ie an object of class <code>ts</code> with frequency > 1), convert it into a data frame, and produce exploratory graphics with it. For example, here’s code to take the training data from that same forecasting competition object and compare the original with a seasonally adjusted version (using X13-SEATS-ARIMA for the seasonal adjustment), and a 12 month rolling average:</p>
<figure>
<pre><code data-lang="r"><span># convert a time series to a data frame
</span><span>the_data</span><span> </span><span>&lt;-</span><span> </span><span>ggseas</span><span>::</span><span>tsdf</span><span>(</span><span>tourism</span><span>[[</span><span>&quot;M4&quot;</span><span>]]</span><span>$</span><span>x</span><span>)</span><span>
</span><span># draw a graphic
</span><span>ggplot</span><span>(</span><span>the_data</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>x</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>y</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>1</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>aes</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;Original&quot;</span><span>))</span><span> </span><span>+</span><span>
</span><span>stat_seas</span><span>(</span><span>aes</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;Seasonally adjusted&quot;</span><span>))</span><span> </span><span>+</span><span>
</span><span>stat_rollapplyr</span><span>(</span><span>aes</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;12 month rolling average&quot;</span><span>),</span><span> </span><span>width</span><span> </span><span>=</span><span> </span><span>12</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_colour_manual</span><span>(</span><span>values</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Original&quot;</span><span> </span><span>=</span><span> </span><span>&quot;grey50&quot;</span><span>,</span><span> </span><span>&quot;Seasonally adjusted&quot;</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>,</span><span> </span><span>&quot;12 month rolling average&quot;</span><span> </span><span>=</span><span> </span><span>&quot;blue&quot;</span><span>))</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.2</span><span>,</span><span> </span><span>0.8</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>&quot;Unknown units\n&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>comma</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Comparison of statistical summary methods in plotting a time series&quot;</span><span>,</span><span>
</span><span>&quot;Monthly series 4 from the Tourism forecasting competition&quot;</span><span>)</span></code></pre>
</figure>
<p><img width="100%" src="http://freerangestats.io/img/0124-stat_seas.svg"></p>
<p>Then the <code>ggsdc</code> function makes it easy to look at a decomposition of a time series. This really comes into its own when we want to look at two or more time series, mapped to colour (there’s an example in the helpfile), but we can use it with a univariate time series too:</p>
<figure>
<pre><code data-lang="r"><span>ggsdc</span><span>(</span><span>the_data</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>x</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>y</span><span>),</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;seas&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Seasonal decomposition with `X13-SEATS-ARIMA`, `seasonal` and `ggseas`&quot;</span><span>,</span><span>
</span><span>&quot;Monthly series 4 from the Tourism forecasting competition&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>&quot;Unknown units\n&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>comma</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>)</span></code></pre>
</figure>
<p><img width="100%" src="http://freerangestats.io/img/0124-ggsdc.svg"></p>
<p>I wrote the first version of the functions that later became <code>ggseas</code> in 2012 when working for New Zealand’s Ministry of Business, Innovation and Employment. We had new access to large amounts of monthly electronic transactions data and needed an efficient way to explore it on our way to developing what eventually became the <a rel="nofollow" href="http://www.mbie.govt.nz/info-services/sectors-industries/tourism/tourism-research-data/monthly-regional-tourism-estimates" target="_blank">Monthly Regional Tourism Estimates</a>.</p>
<h2>Reflections on CRAN</h2>
<p>This experience and recent twittering on Twitter have led me to reflect on the CRAN package update process. Both these updates went through really smoothly; I’m always impressed at the professionalism of the volunteers behind CRAN.</p>
<p>I think CRAN is hugely important and an amazing asset for the R community. It’s really important that packages be published on CRAN. I think the mapping of dependencies, and the process for checking that they all keep working together, is the key aspect here.</p>
<p><code>ggplot2</code> apparently has more than 2,000 reverse dependencies (ie packages that import some functionality from <code>ggplot2</code>), all of them maintained more or less voluntarily. When major changes are made I can’t think of any way other than CRAN (or something like it) for helping the system react and keep everything working. For instance, I found out that the new <code>ggplot2</code> v2.3.0 would break <code>ggseas</code> when I got an automated email advising me of this, from the tests Hadley Wickham and others working on <code>ggplot2</code> ran to identify problems for their reverse dependencies. The RStudio community site was useful for pointing me (and others with similar issues) in the direction of a fix, and of course one uses GitHub or similar to manage issues and code version control, but in the end we need the centralised discipline of something like CRAN as the publication point and the definitive map of the dependencies. So, a big shout out to the volunteers who maintain CRAN and do an awesome job.</p>
<h2>Reflections on testing</h2>
<p>When a user pointed out a bug in the <code>Tcomp</code> package by raising an issue on GitHub, I was pleased with myself for using genuine test-driven development. That is, I first wrote a test that failed because of the bug:</p>
<figure>
<pre><code data-lang="r"><span>test_that</span><span>(</span><span>&quot;series lengths are correct&quot;</span><span>,</span><span> </span><span>{</span><span>
</span><span>expect_equal</span><span>(</span><span>sum</span><span>(</span><span>sapply</span><span>(</span><span>tourism</span><span>,</span><span> </span><span>function</span><span>(</span><span>s</span><span>){</span><span>length</span><span>(</span><span>s</span><span>$</span><span>x</span><span>)</span><span> </span><span>-</span><span> </span><span>s</span><span>$</span><span>n</span><span>})),</span><span> </span><span>0</span><span>)</span><span>
</span><span>})</span></code></pre>
</figure>
<p>…and <em>then</em> worked on finding the problem and fixing it upstream. It’s such an effective way to work, and it means that you have a growing body of tests. Packages like <code>Tcomp</code> and <code>ggseas</code> go for years without me doing anything to them, so when I do have to go back and make a change it’s very reassuring to have a bunch of tests (plus running all the examples, and performing the checks for CRAN) to be sure that everything still works the way it’s meant to.</p>
<p>There’s not enough tests in the build process of those packages at the moment; the more experienced I get, the more I think “build more tests, and build them earlier” is one of the keys to success. This is just in code-driven projects either; I think there’s broad applicability in any professional situation, although the development and automation of tests is harder when you’re not just dealing with code.</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://freerangestats.info/blog/2018/06/15/ggseas-and-tcomp-update"> free range statistics - R</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/lORwQMZ8k0c" height="1"></a></div>