<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/ggmail-forecast-how-many-emails-i-will-get-tomorrow/"></div></div><div><a data-url="https://www.r-bloggers.com/ggmail-forecast-how-many-emails-i-will-get-tomorrow/" data-counturl="https://www.r-bloggers.com/ggmail-forecast-how-many-emails-i-will-get-tomorrow/" data-count="horizontal" data-text="ggmail + forecast = how many emails I will get tomorrow?" 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://smarterpoland.pl/index.php/2016/10/ggmail-ggplot2-data-from-gmail-forecast/"> SmarterPoland.pl » English</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img align="right" src="https://i0.wp.com/smarterpoland.pl/wp-content/uploads/2016/10/ggmail_mont2-1024x528.png?w=300" data-recalc-dims="1"><br>
During the <a target="_blank" rel="nofollow" href="http://erum.ue.poznan.pl/">eRum 2016</a>, <a target="_blank" rel="nofollow" href="http://prac.im.pwr.wroc.pl/~zagdan/english_ver/index.html">Adam Zagdański</a> gave a very good tutorial about time series modeling. Among other things I’ve learned that the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/forecast/index.html">forecast</a> package (created by <a target="_blank" rel="nofollow" href="http://robjhyndman.com/hyndsight/">Rob Hyndman</a>) got cool new plots based on the ggplot2 package.</p>
<p>Let’s use it to play with mailbox statistics for my gmail account!</p>
<p><strong>1. Get the data</strong></p>
<p>Follow <a target="_blank" rel="nofollow" href="https://support.google.com/accounts/answer/3024190?hl=en">this link</a> to download the data from your gmail account as a single mbox file.<br>
It may be large (15GB in my case), but for further steps it’s enough to keep only headers. <code>grep + cat</code> will do the job.</p>
<p><span></span></p>
<p><strong>2. Read headers</strong></p>
<p>The <code>readLines()</code> function can handle headers. Then the <code>lubridate</code> package is useful to extract and convert dates to the R format.</p>
<p><strong>3. Basic gg-exploration</strong></p>
<p>I’ve started with daily aggregates – number of emails per day.<br>
The <code>ts()</code> function converts vector of aggregates to a time series object.<br>
Then I’ve used the <code>autoplot()</code> function to plot the time series. Since it’s the <code>ggplot2</code> plot, you can easily add a smooth trend to the plot with the <code>geom_smooth()</code> function. </p>
<p><img alt="ggmail_trend" class="wp-image-7418" src="https://i2.wp.com/smarterpoland.pl/wp-content/uploads/2016/10/ggmail_trend-1024x503.png?w=456" data-recalc-dims="1"></p>
<p>There is some trend, but what about seasonality?<br>
The <code>geom_boxplot()</code> is useful to check if there are differences among days of week or months.<br>
It turns out that the number of emails per day is very different for week-days and weekends.<br>
Also the August is the email-lightest month. Only, on average, 60 per day <img alt=";-)" class="wp-smiley" src="https://i1.wp.com/smarterpoland.pl/wp-includes/images/smilies/icon_wink.gif?w=456" data-recalc-dims="1"></p>
<p><img alt="ggmail_day" class="wp-image-7420" src="https://i1.wp.com/smarterpoland.pl/wp-content/uploads/2016/10/ggmail_day-1024x518.png?w=456" data-recalc-dims="1"></p>
<p><img alt="ggmail_month" class="wp-image-7419" src="https://i0.wp.com/smarterpoland.pl/wp-content/uploads/2016/10/ggmail_mont2-1024x528.png?w=456" data-recalc-dims="1"></p>
<p><strong>4. Time Series</strong></p>
<p>The <code>decompose()</code> + <code>autoplot()</code> functions extract trend and seasonal components from the time series. The multiplicative seasonal component is probably more appropriate here, but below the additive component is presented since it’s easier to read values on the oy axis.</p>
<p><img alt="ggmail_decompose" class="wp-image-7417" src="https://i1.wp.com/smarterpoland.pl/wp-content/uploads/2016/10/ggmail_decompose-1024x534.png?w=456" data-recalc-dims="1"></p>
<p>A lot of models that can be fitted with the <code>forecast</code> package. From different choices the most scary one is for the forecast with the Holt method. Scary because of the trend.</p>
<p><img alt="ggmail_forecast" class="wp-image-7416" src="https://i1.wp.com/smarterpoland.pl/wp-content/uploads/2016/10/ggmail_forecast-1024x518.png?w=456" data-recalc-dims="1"></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/ggmail-forecast-how-many-emails-i-will-get-tomorrow/"></div></div><div><a data-url="https://www.r-bloggers.com/ggmail-forecast-how-many-emails-i-will-get-tomorrow/" data-counturl="https://www.r-bloggers.com/ggmail-forecast-how-many-emails-i-will-get-tomorrow/" data-count="vertical" data-text="ggmail + forecast = how many emails I will get tomorrow?" 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://smarterpoland.pl/index.php/2016/10/ggmail-ggplot2-data-from-gmail-forecast/"> SmarterPoland.pl » English</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/jNOfelg9onE"></a></div>