<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/update-of-ggseas-for-seasonal-decomposition-on-the-fly/"></div></div><div><a data-url="https://www.r-bloggers.com/update-of-ggseas-for-seasonal-decomposition-on-the-fly/" data-counturl="https://www.r-bloggers.com/update-of-ggseas-for-seasonal-decomposition-on-the-fly/" data-count="horizontal" data-text="Update of `ggseas` for seasonal decomposition on the fly" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ellisp.github.io/blog/2016/10/12/ggsdc-rents"> Peter's stats stuff - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>What’s new</h2>
<p>A new version (0.5.1) of my <code>ggseas</code> R package is now <a target="_blank" href="https://cran.r-project.org/package=ggseas">available on CRAN</a>. <code>ggseas</code> is a small package that provides several tools to make it easier to do seasonal adjustment or decomposition of time series on the fly in a <code>ggplot2</code> pipeline. New in this version:</p>
<ul>
<li>A <code>facet.titles</code> argument to the decomposition graphic function <code>ggsdc</code></li>
<li>Addition of a vignette and other small documentation improvements</li>
<li><code>ggsdc</code> now supports tibbles</li>
<li>Fixed some minor bugs, such as how the ordering of factor levels is preserved</li>
</ul>
<h2>Installation</h2>
<p>Installation is the usual way:</p>
<figure>
<pre><code data-lang="r"><span>install.packages</span><span>(</span><span>&quot;ggseas&quot;</span><span>)</span></code></pre>
</figure>
<h2>Example usage</h2>
<p>One of the two main areas of functionality is the ability to easily do seasonal decomposition into seasonal, trend and random components in an exploratory workflow. Here’s an example, applied to the <a target="_blank" href="http://www.mbie.govt.nz/info-services/housing-property/sector-information-and-statistics/rental-bond-data">mean rent by territorial authority data</a> published by New Zealand Ministry of Business, Innovation and Employment (disclaimer – I work there, but not on rental data).</p>
<p>The aim is to produce this graphic, comparing mean rents in two selected cities:</p>
<p><img alt="seas" src="http://ellisp.github.io/img/0060-seas-rents.svg"></p>
<p>Auckland is New Zealand’s largest city, on the north island. Dunedin is on the south island and has a high proportion of students which, even before we look at the data, we might expect to impact on the rental market. In this graphic we see that the seasonal effect in Dunedin is in fact stronger than in Auckland; actually, there is virtually no seasonality in Auckland rents at all. In Dunedin, rents for leases that start in the most expensive time are around $60 or more a week more than in the cheapest. The decomposition in this graphic is done with X13-SEATS-ARIMA, which <code>ggseas::ggsdc</code> calls via the excellent <code>seasonal</code> R package.</p>
<p>Here’s the code</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidyr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span># We use the dev version of ggplot2 for subtitle and captions, # so uncomment the following line if you don't already have it
# devtools::install_github(&quot;hadley/ggplot2&quot;)
</span><span>library</span><span>(</span><span>ggseas</span><span>)</span><span>
</span><span>www</span><span> </span><span>&lt;-</span><span> </span><span>&quot;http://www.mbie.govt.nz/info-services/housing-property/sector-information-and-statistics/rental-bond-data/documents-images/by-ta/ta-mean-rents.csv&quot;</span><span>
</span><span>rents_orig</span><span> </span><span>&lt;-</span><span> </span><span>read.csv</span><span>(</span><span>www</span><span>,</span><span> </span><span>check.names</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>rents</span><span> </span><span>&lt;-</span><span> </span><span>rents_orig</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>TA</span><span>,</span><span> </span><span>MeanRent</span><span>,</span><span> </span><span>-</span><span>Month</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>Month</span><span> </span><span>=</span><span> </span><span>as.Date</span><span>(</span><span>Month</span><span>))</span><span>
</span><span>rents</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>TA</span><span> </span><span>%in%</span><span> </span><span>c</span><span>(</span><span>&quot;Auckland&quot;</span><span>,</span><span> </span><span>&quot;Dunedin&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ggsdc</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Month</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>MeanRent</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>TA</span><span>),</span><span> </span><span>frequency</span><span> </span><span>=</span><span> </span><span>12</span><span>,</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;seas&quot;</span><span>,</span><span> </span><span>start</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1993</span><span>,</span><span> </span><span>1</span><span>),</span><span>
</span><span>facet.titles</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Original series&quot;</span><span>,</span><span> </span><span>&quot;Underlying trend&quot;</span><span>,</span><span>
</span><span>&quot;Regular seasonal impacts&quot;</span><span>,</span><span> </span><span>&quot;Residual randomness&quot;</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Month lodged&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>&quot;Decomposition of mean rent&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>dollar</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Rent in Dunedin has a strong seasonal pattern&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Seasonal decomposition with X13-SEATS-ARIMA via ggsdc&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: Ministry of Business, Innovation and Employment&quot;</span><span>)</span></code></pre>
</figure>
<p>As another view of the data, I use the same approach to show classical multiplicative decomposition. This isn’t the best way to treat seasonal data, particularly economic data where the seasonality can be expected to change over time, but it is the simplest and can form a useful reality check on the more sophisticated algorithms used in X13-SEATS-ARIMA.</p>
<p><img alt="decomp" src="http://ellisp.github.io/img/0060-decomp-rents.svg"></p>
<p>This approach forces the seasonality to be the same over the whole life of the series. Note that the “seasonality” facet is now showing multipliers, not additions.</p>
<figure>
<pre><code data-lang="r"><span>rents</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>TA</span><span> </span><span>%in%</span><span> </span><span>c</span><span>(</span><span>&quot;Auckland&quot;</span><span>,</span><span> </span><span>&quot;Dunedin&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ggsdc</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Month</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>MeanRent</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>TA</span><span>),</span><span> </span><span>frequency</span><span> </span><span>=</span><span> </span><span>12</span><span>,</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;decompose&quot;</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;multiplicative&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span></code></pre>
</figure>
<h1>Which months are cheaper?</h1>
<p>That last graphic makes it clearer that Dunedin’s rents vary in a regular pattern, but it’s difficult to see exaclty read the axis in enough detail to see which months are low and high. To understand this we need a monthplot:</p>
<p><img alt="monthplot1" src="http://ellisp.github.io/img/0060-monthplot.svg"></p>
<p>…or a monthplot of the detrended series:</p>
<p><img alt="monthplot2" src="http://ellisp.github.io/img/0060-monthplot-detrended.svg"></p>
<p>These graphics show that the months that have the lowest rents lodged are actually in the middle of the year – June and July.</p>
<p>The last two graphics were made with the benefit of <code>stl</code> and <code>monthplot</code> from the standard <code>stats</code> package, not related to <code>ggseas</code> at all:</p>
<figure>
<pre><code data-lang="r"><span>dun_ts</span><span> </span><span>&lt;-</span><span> </span><span>rents</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>TA</span><span> </span><span>%in%</span><span> </span><span>c</span><span>(</span><span>&quot;Dunedin&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>LogRent</span><span> </span><span>=</span><span> </span><span>log</span><span>(</span><span>MeanRent</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>LogRent</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ts</span><span>(</span><span>start</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1993</span><span>,</span><span> </span><span>1</span><span>),</span><span> </span><span>frequency</span><span> </span><span>=</span><span> </span><span>12</span><span>)</span><span> </span><span># plot of months, including trend
</span><span>monthplot</span><span>(</span><span>dun_ts</span><span>)</span><span>
</span><span>grid</span><span>()</span><span>
</span><span># de-trended version
</span><span>dun_stl</span><span> </span><span>&lt;-</span><span> </span><span>stl</span><span>(</span><span>dun_ts</span><span>[</span><span> </span><span>,</span><span>1</span><span>],</span><span> </span><span>s.window</span><span> </span><span>=</span><span> </span><span>7</span><span>)</span><span>
</span><span>monthplot</span><span>(</span><span>with</span><span>(</span><span>as.data.frame</span><span>(</span><span>dun_stl</span><span>$</span><span>time.series</span><span>),</span><span> </span><span>seasonal</span><span> </span><span>+</span><span> </span><span>remainder</span><span>))</span><span>
</span><span>grid</span><span>()</span></code></pre>
</figure>
<h2>More</h2>
<p>There’s more to <code>ggseas</code> than these decomposition plots. It also offers indexing, rolling average, and seasonal adjustment “stats” that integrate into the standard <code>ggplot</code> graphic production line. Check out the <a target="_blank" href="https://cran.r-project.org/web/packages/ggseas/vignettes/ggsdc.html">new vignette</a>. Any bugs or feature requests please <a target="_blank" href="https://github.com/ellisp/ggseas/issues">post as issues on GitHub</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/update-of-ggseas-for-seasonal-decomposition-on-the-fly/"></div></div><div><a data-url="https://www.r-bloggers.com/update-of-ggseas-for-seasonal-decomposition-on-the-fly/" data-counturl="https://www.r-bloggers.com/update-of-ggseas-for-seasonal-decomposition-on-the-fly/" data-count="vertical" data-text="Update of `ggseas` for seasonal decomposition on the fly" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://ellisp.github.io/blog/2016/10/12/ggsdc-rents"> Peter's stats stuff - 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 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/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=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/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/-C-FLNCfa-Q"></a></div>