<p>Two months ago <code>padr</code> was introduced, followed by an improved version that allowed for applying <code>pad</code> on group level. See the <a target="_blank" rel="nofollow" href="https://edwinth.github.io/blog/padr-intro/">introduction blogs</a> or the <code>vignette(&quot;padr&quot;)</code> for more package information. In this blog I give four more elaborate examples on how to go from raw data to insight with <code>padr</code>, <code>dplyr</code> and <code>ggplot2</code>.<br>
They might serve as recipes for time series problems you want to solve. The dataset <code>emergency</code> is available in <code>padr</code>, and contains about a year of emergency data in Montgomery County, PA. For this blog I only use the twelve most prevalent emergencies.</p>
<div>
<pre><code><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>padr</span><span>)</span><span>
</span><span>top12</span><span> </span><span>&lt;-</span><span> </span><span>emergency</span><span> </span><span>%>%</span><span> </span><span>count</span><span>(</span><span>title</span><span>)</span><span> </span><span>%>%</span><span> </span><span>arrange</span><span>(</span><span>desc</span><span>(</span><span>n</span><span>))</span><span> </span><span>%>%</span><span> </span><span>slice</span><span>(</span><span>1</span><span>:</span><span>12</span><span>)</span><span> </span><span>%>%</span><span> </span><span>select</span><span>(</span><span>title</span><span>)</span><span> </span><span>%>%</span><span> </span><span>inner_join</span><span>(</span><span>emergency</span><span>)</span><span>
</span></code></pre>
</div>
<h1>1) A proportion plot over the months</h1>
<p>Lets start with a fairly simple problem, we are calculating the relative distribution of events within each month. We normalize the counts within each month, and then plot through time. <code>geom_bar</code> does most of the heavy lifiting here, we just need the months counts for each event. (I checked, all events have calls in each month, there is no need for padding.)</p>
<div>
<pre><code><span>top12</span><span> </span><span>%>%</span><span> </span><span>thicken</span><span>(</span><span>'month'</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>'month'</span><span>)</span><span> </span><span>%>%</span><span> </span><span>count</span><span>(</span><span>title</span><span>,</span><span> </span><span>month</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>month</span><span>,</span><span> </span><span>n</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_bar</span><span>(</span><span>aes</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>title</span><span>),</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;black&quot;</span><span>,</span><span>
</span><span>stat</span><span> </span><span>=</span><span> </span><span>'identity'</span><span>,</span><span> </span><span>position</span><span> </span><span>=</span><span> </span><span>&quot;fill&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk unnamed-chunk-2" src="https://i0.wp.com/edwinth.github.io/figure/source/2017-03-19-padr-examples/unnamed-chunk-2-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>2) Get a distribution, per event, per time unit</h2>
<p>For each clock hour, for each event, we want to compute the average of the number of occurences. The first step is to make the hourly distribution out of the raw data. For every hour we need to count the number of calls per event. This would give no records if there were no calls within a givin hour. If we did not account for this, the calculated averages would be too high. (Recently called “the zero-bug” in <a target="_blank" rel="nofollow" href="http://www.win-vector.com/blog/2017/02/the-zero-bug/">this blog post</a>). Therefore we use <code>pad</code> and <code>fill_by_value</code> to insert records and give them the value 0.</p>
<div>
<pre><code><span>hourly_distribution</span><span> </span><span>&lt;-</span><span> </span><span>top12</span><span> </span><span>%>%</span><span> </span><span>thicken</span><span>(</span><span>'hour'</span><span>)</span><span> </span><span>%>%</span><span> </span><span>count</span><span>(</span><span>title</span><span>,</span><span> </span><span>time_stamp_hour</span><span>)</span><span> </span><span>%>%</span><span> </span><span>pad</span><span>(</span><span>group</span><span> </span><span>=</span><span> </span><span>'title'</span><span>)</span><span> </span><span>%>%</span><span> </span><span>fill_by_value</span><span>(</span><span>n</span><span>)</span><span> </span></code></pre>
</div>
<p>Next step is extracting the clock hour from each timestamp, and calculate the mean per event, per clock hour.</p>
<div>
<pre><code><span>hourly_distribution</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>clock_hour</span><span> </span><span>=</span><span> </span><span>lubridate</span><span>::</span><span>hour</span><span>(</span><span>time_stamp_hour</span><span>)</span><span> </span><span>)</span><span> </span><span>%>%</span><span> </span><span>group_by</span><span>(</span><span>clock_hour</span><span>,</span><span> </span><span>title</span><span>)</span><span> </span><span>%>%</span><span> </span><span>summarise</span><span>(</span><span>mn</span><span> </span><span>=</span><span> </span><span>mean</span><span>(</span><span>n</span><span>))</span><span> </span><span>%>%</span><span> </span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>clock_hour</span><span>,</span><span> </span><span>mn</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span> </span><span>facet_wrap</span><span>(</span><span>~</span><span>title</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>'free'</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk unnamed-chunk-4" src="https://i1.wp.com/edwinth.github.io/figure/source/2017-03-19-padr-examples/unnamed-chunk-4-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>3) Get the 30th observation per time unit</h2>
<p>For this dataset it might not be terribly interesting, but there are many situations were it is very useful to extract the first, last, or nth observation per time unit. Lets find the 30th observation per week, where weeks are starting on Mondays. We need an offset in <code>thicken</code> here, to make the weeks start on Mondays.</p>
<div>
<pre><code><span>first_day</span><span> </span><span>&lt;-</span><span> </span><span>top12</span><span>$</span><span>time_stamp</span><span> </span><span>%>%</span><span> </span><span>as.Date</span><span> </span><span>%>%</span><span> </span><span>min</span><span>
</span><span>first_day</span><span> </span><span>%>%</span><span> </span><span>weekdays</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] &quot;Thursday&quot;
</code></pre>
</div>
<div>
<pre><code><span>top12</span><span> </span><span>%>%</span><span> </span><span>thicken</span><span>(</span><span>'week'</span><span>,</span><span> </span><span>start_val</span><span> </span><span>=</span><span> </span><span>first_day</span><span> </span><span>-</span><span> </span><span>3</span><span>)</span><span> </span><span>%>%</span><span> </span><span>group_by</span><span>(</span><span>title</span><span>,</span><span> </span><span>time_stamp_week</span><span>)</span><span> </span><span>%>%</span><span> </span><span>filter</span><span>(</span><span>min_rank</span><span>(</span><span>time_stamp</span><span>)</span><span> </span><span>==</span><span> </span><span>30</span><span>)</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>weekday</span><span> </span><span>=</span><span> </span><span>weekdays</span><span>(</span><span>time_stamp</span><span>)</span><span> </span><span>%>%</span><span> </span><span>factor</span><span>(</span><span>levels</span><span> </span><span>=</span><span> </span><span>(</span><span>c</span><span>(</span><span>'Monday'</span><span>,</span><span> </span><span>'Tuesday'</span><span>,</span><span> </span><span>'Wednesday'</span><span>,</span><span> </span><span>'Thursday'</span><span>,</span><span> </span><span>'Friday'</span><span>,</span><span>
</span><span>'Saturday'</span><span>,</span><span> </span><span>'Sunday'</span><span>)</span><span> </span><span>%>%</span><span> </span><span>rev</span><span>)</span><span> </span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>weekday</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_bar</span><span>(</span><span>aes</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>title</span><span>))</span><span> </span><span>+</span><span>
</span><span>coord_flip</span><span>()</span><span> </span><span>+</span><span>
</span><span>guides</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>title</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk unnamed-chunk-5" src="https://i0.wp.com/edwinth.github.io/figure/source/2017-03-19-padr-examples/unnamed-chunk-5-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>So here we have a distribution on which day the 30th call came in, for each of the twelve events.</p>
<h2>4) Make a moving average</h2>
<p>Moving averages are very helpful for smoothing time series. It is often a better indication of the underlying trend than the raw data. I recently learned about the <code>RcppRoll</code> package, when I was browsing through <em>R for Data Science</em>. This is a nice package by Kevin Ushey, that makes it terribly easy to calculate rolling stats on a vector. Here we want the moving average of the daily count of each of the events.</p>
<div>
<pre><code><span>top12</span><span> </span><span>%>%</span><span> </span><span>thicken</span><span>(</span><span>'day'</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>'day'</span><span>)</span><span> </span><span>%>%</span><span> </span><span>count</span><span>(</span><span>day</span><span>,</span><span> </span><span>title</span><span>)</span><span> </span><span>%>%</span><span> </span><span>pad</span><span>(</span><span>group</span><span> </span><span>=</span><span> </span><span>'title'</span><span>)</span><span> </span><span>%>%</span><span> </span><span>fill_by_value</span><span>(</span><span>n</span><span>)</span><span> </span><span>%>%</span><span> </span><span>group_by</span><span>(</span><span>title</span><span>)</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>moving_average</span><span> </span><span>=</span><span> </span><span>RcppRoll</span><span>::</span><span>roll_meanr</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>n</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>30</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>NA</span><span>))</span><span> </span><span>%>%</span><span> </span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>day</span><span>,</span><span> </span><span>n</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>aes</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>moving_average</span><span>),</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>'red'</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>title</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>'free'</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk unnamed-chunk-6" src="https://i1.wp.com/edwinth.github.io/figure/source/2017-03-19-padr-examples/unnamed-chunk-6-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>There we have some recipes for preparing data containing timestamp for analysis. Do you have a time series-related challenge not adressed here? I would love to hear from you, and try to figure out an effective way for solving it! Just send an email or post a comment.</p>
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/CDt8Wx9Ya8U">