<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/tidy-time-series-analysis-part-4-lags-and-autocorrelation/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.business-science.io/timeseries-analysis/2017/08/30/tidy-timeseries-analysis-pt-4.html"> business-science.io - Articles</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In the fourth part in a series on <strong>Tidy Time Series Analysis</strong>, we’ll investigate <strong>lags</strong> and <strong>autocorrelation</strong>, which are useful in understanding seasonality and form the basis for autoregressive forecast models such as AR, ARMA, ARIMA, SARIMA (basically any forecast model with “AR” in the acronym). We’ll use the <code>tidyquant</code> package along with our <em>tidyverse</em> downloads data obtained from <code>cranlogs</code>. The focus of this post is using <code>lag.xts()</code>, a function capable of returning multiple lags from a <em>xts</em> object, to investigate autocorrelation in lags among the daily <em>tidyverse</em> package downloads. When using <code>lag.xts()</code> with <code>tq_mutate()</code> we can scale to multiple groups (different <em>tidyverse</em> packages in our case). If you like what you read, please <a rel="nofollow" href="http://www.business-science.io/timeseries-analysis/2017/08/30/tidy-timeseries-analysis-pt-4.html#social" target="_blank">follow us on social media</a> to stay up on the latest <a rel="nofollow" href="http://www.business-science.io/timeseries-analysis/2017/08/30/tidy-timeseries-analysis-pt-4.html#contact" target="_blank">Business Science</a> news, events and information! As always, we are interested in both expanding our <em>network of data scientists</em> and seeking <em>new clients interested in applying data science to business and finance</em>. If interested, <a rel="nofollow" href="http://www.business-science.io/contact.html" target="_blank">contact us</a>.</p>
<p>If you haven’t checked out the previous tidy time series posts, you may want to review them to get up to speed.</p>
<ul>
<li><a rel="nofollow" href="http://www.business-science.io/timeseries-analysis/2017/07/02/tidy-timeseries-analysis.html" target="_blank">Part 1: Tidy Period Apply</a></li>
<li><a rel="nofollow" href="http://www.business-science.io/timeseries-analysis/2017/07/23/tidy-timeseries-analysis-pt-2.html" target="_blank">Part 2: Tidy Rolling Functions</a></li>
<li><a rel="nofollow" href="http://www.business-science.io/timeseries-analysis/2017/07/30/tidy-timeseries-analysis-pt-3.html" target="_blank">Part 3: Tidy Rolling Correlations</a></li>
</ul>
<p>Here’s an example of the autocorrelation plot we investigate as part of this post:</p>
<p><img alt="tidyquant correlation over time" src="https://i2.wp.com/www.business-science.io/assets/tidy-timeseries-part4.png?w=456" data-recalc-dims="1"></p>
<h1>Libraries Needed</h1>
<p>We’ll need to load several libraries today.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidyquant</span><span>)</span><span> </span><span># Loads tidyverse, tidyquant, financial pkgs, xts/zoo
</span><span>library</span><span>(</span><span>cranlogs</span><span>)</span><span> </span><span># For inspecting package downloads over time
</span><span>library</span><span>(</span><span>timetk</span><span>)</span><span> </span><span># For consistent time series coercion functions
</span><span>library</span><span>(</span><span>stringr</span><span>)</span><span> </span><span># Working with strings
</span><span>library</span><span>(</span><span>forcats</span><span>)</span><span> </span><span>#</span><span> </span><span>Working</span><span> </span><span>with</span><span> </span><span>factors</span><span>/</span><span>categorical</span><span> </span><span>data</span></code></pre>
</figure>
<h1>CRAN tidyverse Downloads</h1>
<p>We’ll be using the same “tidyverse” dataset as the last several posts. The script below gets the package downloads for the first half of 2017.</p>
<figure>
<pre><code data-lang="r"><span># tidyverse packages (see my laptop stickers from first post) ;)
</span><span>pkgs</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>
</span><span>&quot;tidyr&quot;</span><span>,</span><span> </span><span>&quot;lubridate&quot;</span><span>,</span><span> </span><span>&quot;dplyr&quot;</span><span>,</span><span> </span><span>&quot;broom&quot;</span><span>,</span><span> </span><span>&quot;tidyquant&quot;</span><span>,</span><span> </span><span>&quot;ggplot2&quot;</span><span>,</span><span> </span><span>&quot;purrr&quot;</span><span>,</span><span> </span><span>&quot;stringr&quot;</span><span>,</span><span> </span><span>&quot;knitr&quot;</span><span>
</span><span>)</span><span>
</span><span># Get the downloads for the individual packages
</span><span>tidyverse_downloads</span><span> </span><span>&lt;-</span><span> </span><span>cran_downloads</span><span>(</span><span>
</span><span>packages</span><span> </span><span>=</span><span> </span><span>pkgs</span><span>,</span><span> </span><span>from</span><span> </span><span>=</span><span> </span><span>&quot;2017-01-01&quot;</span><span>,</span><span> </span><span>to</span><span> </span><span>=</span><span> </span><span>&quot;2017-06-30&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>tibble</span><span>::</span><span>as_tibble</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>package</span><span>)</span><span>
</span><span>tidyverse_downloads</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 1,629 x 3
## # Groups:   package [9]
##          date count package
##  *     </code></pre></figure>