<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/recreating-rviews-reproducible-finance-with-r-sector-correlations/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.mattdancho.com/financial-analysis/2017/02/02/Recreating-RViews-Reproducible-Finance-Post.html"> mattdancho.com - Articles</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The folks at <em>RStudio</em> have a segment on their <em>RViews</em> blog, <a target="_blank" rel="nofollow" href="https://www.rstudio.com/rviews/category/finance/">“Reproducible Finance with R”</a>, one that we at <a target="_blank" rel="nofollow" href="http://www.mattdancho.com/index.html">Business Science</a> are very fond of! In the spirit of reproducibility, we thought that it would be appropriate to recreate the <a target="_blank" rel="nofollow" href="https://www.rstudio.com/rviews/2017/01/18/reproducible-finance-with-r-sector-correlations/"><em>RViews</em> post, “Reproducible Finance with R: Sector Correlations”</a>. This time, however, the <code>tidyquant</code> package will be used to <strong>streamline much of the code that is currently used</strong>. The main advantage of <code>tidyquant</code> is to bridge the gap between the best quantitative resources for collecting and manipulating quantitative data: <code>xts</code>, <code>zoo</code>, <code>quantmod</code> and <code>TTR</code>, and the data modeling workflow and infrastructure of the <code>tidyverse</code>. When implemented, <code>tidyquant</code> <strong>cuts the code down by about half and simplifies the workflow</strong>.</p>
<h1>Table of Contents</h1>
<ul>
<li><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/financial-analysis/2017/02/02/Recreating-RViews-Reproducible-Finance-Post.html#overview">Correlating Sector ETF Returns to the SP500</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/financial-analysis/2017/02/02/Recreating-RViews-Reproducible-Finance-Post.html#prerequisites">Prerequisites</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/financial-analysis/2017/02/02/Recreating-RViews-Reproducible-Finance-Post.html#data-import">Data Import</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/financial-analysis/2017/02/02/Recreating-RViews-Reproducible-Finance-Post.html#rolling-corr">Rolling Correlations</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/financial-analysis/2017/02/02/Recreating-RViews-Reproducible-Finance-Post.html#dygraphs">Dygraphs</a></li>
</ul>
<h1>Correlating Sector ETF Returns to the SP500 <a></a></h1>
<p>The folks at <em>RStudio</em> have a new segment in <em>RViews</em> (The <em>RStudio</em> blog) called <a target="_blank" rel="nofollow" href="https://www.rstudio.com/rviews/category/finance/">“Reproducible Finance with R”</a>. For their first installation of 2017 they looked at how Sector Exchange Traded Fund (ETF) returns correlate to the broader market using the Spider SP500 ETF (“SPY”) as a proxy for the SP500 index. The <em>RViews</em> post can be found <a target="_blank" rel="nofollow" href="https://www.rstudio.com/rviews/2017/01/18/reproducible-finance-with-r-sector-correlations/">here</a>, and here’s a snapshot of the final chart comparing the correlation between the SP500 (overall market) and the Technology ETF over time.</p>
<p><img alt="Correlation Between SP500 and Tech ETF" src="https://i2.wp.com/www.mattdancho.com/assets/rviews-sector-corr-post.PNG?w=456" data-recalc-dims="1"></p>
<p>Source: <a target="_blank" rel="nofollow" href="https://www.rstudio.com/rviews/2017/01/18/reproducible-finance-with-r-sector-correlations/">Reproducible Finance with R: Sector Correlations</a></p>
<p>Today, the newest member of the Business Science (BizSci) team, <a target="_blank" rel="nofollow" href="http://www.mattdancho.com/team.html">Davis Vaughan</a>, shows how you can implement our R financial package, <code>tidyquant</code>, to streamline the <em>RViews</em> Sector Correlations analysis. I hope you’ll join me in welcoming Davis to our team! You can follow him on <a target="_blank" rel="nofollow" href="https://twitter.com/dvaughan32">twitter</a>, <a target="_blank" rel="nofollow" href="https://www.linkedin.com/in/davis-vaughan-9786455b">LinkedIn</a> and <a target="_blank" rel="nofollow" href="https://github.com/DavisVaughan">GitHub</a>.</p>
<p><em>Matt Dancho, Director of Product Development @ BizSci</em></p>
<h1>Prerequisites <a></a></h1>
<p>Let’s start by loading some packages.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidyquant</span><span>)</span><span>
</span><span>library</span><span>(</span><span>dygraphs</span><span>)</span></code></pre>
</figure>
<p>We’ll use the same ETF tickers as <em>RViews</em>.</p>
<figure>
<pre><code data-lang="r"><span># List of tickers for sector etfs. </span><span>ticker</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;XLY&quot;</span><span>,</span><span> </span><span>&quot;XLP&quot;</span><span>,</span><span> </span><span>&quot;XLE&quot;</span><span>,</span><span> </span><span>&quot;XLF&quot;</span><span>,</span><span> </span><span>&quot;XLV&quot;</span><span>,</span><span> </span><span>&quot;XLI&quot;</span><span>,</span><span> </span><span>&quot;XLB&quot;</span><span>,</span><span> </span><span>&quot;XLK&quot;</span><span>,</span><span> </span><span>&quot;XLU&quot;</span><span>,</span><span> </span><span>&quot;SPY&quot;</span><span>)</span><span> </span><span># And the accompanying sector names for those ETFs.
</span><span>sector</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;Consumer Discretionary&quot;</span><span>,</span><span> </span><span>&quot;Consumer Staples&quot;</span><span>,</span><span> </span><span>&quot;Energy&quot;</span><span>,</span><span> </span><span>&quot;Financials&quot;</span><span>,</span><span> </span><span>&quot;Health Care&quot;</span><span>,</span><span> </span><span>&quot;Industrials&quot;</span><span>,</span><span> </span><span>&quot;Materials&quot;</span><span>,</span><span> </span><span>&quot;Information Technology&quot;</span><span>,</span><span> </span><span>&quot;Utilities&quot;</span><span>,</span><span> </span><span>&quot;Index&quot;</span><span>)</span><span>
</span><span>etf_ticker_sector</span><span> </span><span>&lt;-</span><span> </span><span>tibble</span><span>(</span><span>ticker</span><span>,</span><span> </span><span>sector</span><span>)</span><span>
</span><span>etf_ticker_sector</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 10 × 2
##    ticker                 sector
##     &lt;chr>                  &lt;chr>
## 1     XLY Consumer Discretionary
## 2     XLP       Consumer Staples
## 3     XLE                 Energy
## 4     XLF             Financials
## 5     XLV            Health Care
## 6     XLI            Industrials
## 7     XLB              Materials
## 8     XLK Information Technology
## 9     XLU              Utilities
## 10    SPY                  Index</code></pre>
</figure>
<h1>Data Import <a></a></h1>
<p>Alright, now is where things get interesting! Let’s take a peek at the differences in how you might get data to solve this problem.</p>
<h2>RViews</h2>
<p>Here’s the <em>RViews</em> code snippet. <em>RViews</em> created a special function to import closing prices using <code>getSymbols()</code> directly from the <code>quantmod</code> package. They then used <code>periodReturn()</code> to convert these prices to weekly log returns. Internal to the function, there are calls to <code>getSymbols</code> and <code>do.call</code> with <code>merge</code> and <code>lapply</code> twice, once to get closing prices and once to get log weekly returns. Pretty complicated. Kudos to the author, Jonathan Regenstein, at <em>RStudio</em> for figuring this out.</p>
<figure>
<pre><code data-lang="r"><span># A function to build an xts object of etf returns.
</span><span>etf_weekly_returns</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>ticker</span><span>)</span><span> </span><span>{</span><span>
</span><span># Download prices using getSybmols </span><span> </span><span>symbols</span><span> </span><span>&lt;-</span><span> </span><span>getSymbols</span><span>(</span><span>ticker</span><span>,</span><span> </span><span>auto.assign</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>warnings</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span># Take just the Closing prices.
</span><span> </span><span>etf_prices</span><span> </span><span>&lt;-</span><span> </span><span>do.call</span><span>(</span><span>merge</span><span>,</span><span> </span><span>lapply</span><span>(</span><span>symbols</span><span>,</span><span> </span><span>function</span><span>(</span><span>x</span><span>)</span><span> </span><span>Cl</span><span>(</span><span>get</span><span>(</span><span>x</span><span>))))</span><span>
</span><span># Use the periodReturn() function to get log weekly returns. </span><span> </span><span>etf_returns</span><span> </span><span>&lt;-</span><span> </span><span>do.call</span><span>(</span><span>merge</span><span>,</span><span> </span><span>lapply</span><span>(</span><span>etf_prices</span><span>,</span><span> </span><span>function</span><span>(</span><span>x</span><span>)</span><span> </span><span>periodReturn</span><span>(</span><span>x</span><span>,</span><span> </span><span>period</span><span> </span><span>=</span><span> </span><span>'weekly'</span><span>,</span><span>
</span><span>type</span><span> </span><span>=</span><span> </span><span>'log'</span><span>)))</span><span>
</span><span>#Change the column names to the sector names from our dataframe above.
</span><span> </span><span>colnames</span><span>(</span><span>etf_returns</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>etf_ticker_sector</span><span>$</span><span>sector</span><span>
</span><span>etf_returns</span><span>
</span><span>}</span><span>
</span><span># Let's pass in our ticker symbols and build an xts object of etf returns
</span><span>etf_returns_rviews</span><span> </span><span>&lt;-</span><span> </span><span>etf_weekly_returns</span><span>(</span><span>etf_ticker_sector</span><span>$</span><span>ticker</span><span>)</span><span>
</span><span>head</span><span>(</span><span>etf_returns_rviews</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">##            Consumer Discretionary Consumer Staples       Energy
## 2007-01-05           -0.007034027     -0.005722007 -0.011733539
## 2007-01-12            0.026062578      0.014055350 -0.008620725
## 2007-01-19            0.008875499      0.003765102  0.020354264
## 2007-01-26           -0.019888442     -0.004519819  0.004760584
## 2007-02-02            0.022409825      0.011634547  0.030830343
## 2007-02-09           -0.006315545     -0.010502685 -0.004101903
##              Financials  Health Care  Industrials     Materials
## 2007-01-05 -0.011167160  0.005655589 -0.007693470 -0.0113323889
## 2007-01-12  0.013331718  0.017361037  0.017579216  0.0290876947
## 2007-01-19 -0.001081666  0.011312637  0.001123511  0.0152115609
## 2007-01-26 -0.003795098 -0.012772249 -0.011577145  0.0086291974
## 2007-02-02  0.015362168  0.014501414  0.027452705  0.0151288535
## 2007-02-09 -0.005901386 -0.005485765 -0.009439299 -0.0008194183
##            Information Technology    Utilities        Index
## 2007-01-05            0.007260335 -0.019471313 -0.005888436
## 2007-01-12            0.021886396 -0.006668599  0.019029484
## 2007-01-19           -0.027005903  0.005005572 -0.002936435
## 2007-01-26           -0.001283929  0.005532545 -0.004842978
## 2007-02-02            0.016985546  0.023177534  0.018680358
## 2007-02-09           -0.008881386  0.025021348 -0.006025965</code></pre>
</figure>
<h2>BizSci</h2>
<p>And, the BizSci version using <code>tidyquant</code>. We first get the ETF prices using <code>tq_get()</code> and group the prices by ticker and sector. Then we use <code>tq_transform()</code> to get the period returns from the stock prices.</p>
<p>If you are new to <code>tidyquant</code>, <code>tq_transform()</code> is used when the return is in a different periodicity than the input. It accepts <code>ohlc_fun = Cl</code> and <code>transform_fun = periodReturn</code> , along with any additional <code>periodReturn</code> args passed by way of <code>...</code>. This tells the function to use the closing price to calculate period returns and return the result as a new tibble. Note that typically you would used the <code>ohlc_fun = Ad</code> for period returns since stock splits are present in closing prices, but for an ETF we should not have splits.</p>
<figure>
<pre><code data-lang="r"><span># Get stock prices
</span><span>prices</span><span> </span><span>&lt;-</span><span> </span><span>etf_ticker_sector</span><span> </span><span>%>%</span><span>
</span><span>tq_get</span><span>(</span><span>get</span><span> </span><span>=</span><span> </span><span>&quot;stock.prices&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>group_by</span><span>(</span><span>ticker</span><span>,</span><span> </span><span>sector</span><span>)</span><span>
</span><span># Transform to period returns
</span><span>etf_returns_bizsci</span><span> </span><span>&lt;-</span><span> </span><span>prices</span><span> </span><span>%>%</span><span> </span><span>tq_transform</span><span>(</span><span>ohlc_fun</span><span> </span><span>=</span><span> </span><span>Cl</span><span>,</span><span> </span><span>transform_fun</span><span> </span><span>=</span><span> </span><span>periodReturn</span><span>,</span><span> </span><span>period</span><span> </span><span>=</span><span> </span><span>'weekly'</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>'log'</span><span>)</span><span>
</span><span>head</span><span>(</span><span>etf_returns_bizsci</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## Source: local data frame [6 x 4]
## Groups: ticker, sector [1]
## 
##   ticker                 sector       date weekly.returns
##    &lt;chr>                  &lt;chr>     &lt;date>          &lt;dbl>
## 1    XLY Consumer Discretionary 2007-01-05   -0.007034027
## 2    XLY Consumer Discretionary 2007-01-12    0.026062578
## 3    XLY Consumer Discretionary 2007-01-19    0.008875499
## 4    XLY Consumer Discretionary 2007-01-26   -0.019888442
## 5    XLY Consumer Discretionary 2007-02-02    0.022409825
## 6    XLY Consumer Discretionary 2007-02-09   -0.006315545</code></pre>
</figure>
<p>It’s that easy! No need for <code>do.call()</code>, <code>lapply()</code>, or any special functions! It’s all been taken care of for you. Grouping by ticker (and sector to keep the column) allows us to perform the transform on <strong>each group</strong> separately, but with one line of code. Also, notice that the data is preserved in a <em>tidy</em> format, as opposed to the xts format that <em>RViews</em> uses.</p>
<p>Additionally, <em>RViews</em> intends to create a flexdashboard from their notebook:</p>
<blockquote>
<p>…this Notebook will be the first step toward an flexdashboard that lets us do more interactive exploration – choosing different sector ETFs and rolling windows</p>
<p>-Jonathan Regenstein, RStudio</p>
</blockquote>
<p>It would be easy to now create a function wrapping the process like <em>RViews</em> did, allowing the user to just enter the tibble of tickers. This could be useful in the flexdashboard that they will create, but for this post, we chose not do to that.</p>
<h1>Rolling Correlations <a></a></h1>
<p>The next step is to calculate <em>rolling correlations</em> between the SP500 index (“SPY”) ETF returns and the sector-specific ETF returns.</p>
<h2>RViews</h2>
<p>Here’s how <em>RViews</em> solved this problem in two steps.</p>
<h5>Step 1: Create a Sector Index Correlation Function</h5>
<p>A function is a nice approach, but the downside is it only works for one component unless you use the <code>purrr</code> package to map the function. A special function was again created to merge the sector and SPY returns and then apply the rolling correlation using <code>rollapply()</code> with another special function. Very well done, but complicated.</p>
<figure>
<pre><code data-lang="r"><span># A function that calculates the rolling correlation between a sector ETF and the SPY SP500 ETF. </span><span>sector_index_correlation</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>x</span><span>,</span><span> </span><span>window</span><span>)</span><span> </span><span>{</span><span>
</span><span># Make one xts object to hold the sector returns and the SPY returns </span><span> </span><span>merged_xts</span><span> </span><span>&lt;-</span><span> </span><span>merge</span><span>(</span><span>x</span><span>,</span><span> </span><span>etf_returns_rviews</span><span>$</span><span>'Index'</span><span>)</span><span>
</span><span># Use rollapply() to calculate the rolling correlations. </span><span> </span><span># See what happens if you remove the 'pairwise.complete.obs' argument - the NAs will cause problems.
</span><span> </span><span>merged_xts</span><span>$</span><span>rolling_cor</span><span> </span><span>&lt;-</span><span> </span><span>rollapply</span><span>(</span><span>merged_xts</span><span>,</span><span> </span><span>window</span><span>,</span><span> </span><span>function</span><span>(</span><span>x</span><span>)</span><span> </span><span>cor</span><span>(</span><span>x</span><span>[,</span><span>1</span><span>],</span><span> </span><span>x</span><span>[,</span><span>2</span><span>],</span><span> </span><span>use</span><span> </span><span>=</span><span> </span><span>&quot;pairwise.complete.obs&quot;</span><span>),</span><span> </span><span>by.column</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>names</span><span>(</span><span>merged_xts</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;Sector Returns&quot;</span><span>,</span><span> </span><span>&quot;SPY Returns&quot;</span><span>,</span><span> </span><span>&quot;Sector/SPY Correlation&quot;</span><span>)</span><span>
</span><span>merged_xts</span><span>
</span><span>}</span></code></pre>
</figure>
<h5>Step 2: Apply the function to the data</h5>
<p><em>RViews</em> applies the special function across the “Information Technology” ETF only. Again, <code>purrr</code> is needed to map across all ETF’s if desired, which is an additional step.</p>
<figure>
<pre><code data-lang="r"><span># Choose a sector ETF and a rolling window and pass them to the function we just build. # Let's go with a 5 month window and the Information Technology sector.
# We will now have a new xts object with 3 time series: sector returns, SPY returns
# and the rolling correlation between those return series.
</span><span>
</span><span>IT_SPY_correlation</span><span> </span><span>&lt;-</span><span> </span><span>sector_index_correlation</span><span>(</span><span>etf_returns_rviews</span><span>$</span><span>'Information Technology'</span><span>,</span><span> </span><span>20</span><span>)</span><span>
</span><span># Have a peek. The first 20 rows in the correlation column should be # NAs. </span><span>
</span><span>head</span><span>(</span><span>IT_SPY_correlation</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>25</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">##            Sector Returns   SPY Returns Sector/SPY Correlation
## 2007-01-05    0.007260335 -0.0058884356                     NA
## 2007-01-12    0.021886396  0.0190294841                     NA
## 2007-01-19   -0.027005903 -0.0029364349                     NA
## 2007-01-26   -0.001283929 -0.0048429782                     NA
## 2007-02-02    0.016985546  0.0186803576                     NA
## 2007-02-09   -0.008881386 -0.0060259646                     NA
## 2007-02-16    0.015177315  0.0123590071                     NA
## 2007-02-23    0.003341691 -0.0029549759                     NA
## 2007-03-02   -0.057501940 -0.0467035959                     NA
## 2007-03-09    0.015776007  0.0151013857                     NA
## 2007-03-16   -0.002176236 -0.0161114796                     NA
## 2007-03-23    0.026656737  0.0344812823                     NA
## 2007-03-30   -0.010663355 -0.0097411262                     NA
## 2007-04-05    0.022892097  0.0156515560                     NA
## 2007-04-13    0.001256461  0.0074596420                     NA
## 2007-04-20    0.014131615  0.0224544236                     NA
## 2007-04-27    0.015561370  0.0061043558                     NA
## 2007-05-04    0.016122840  0.0092528469                     NA
## 2007-05-11    0.007171385 -0.0003976208                     NA
## 2007-05-18    0.004752405  0.0115988780              0.8820099
## 2007-05-25   -0.004752405 -0.0061121613              0.8963962
## 2007-06-01    0.018096442  0.0156329832              0.8946237
## 2007-06-08   -0.012159397 -0.0199273050              0.9301269
## 2007-06-15    0.017214861  0.0133507228              0.9311254
## 2007-06-22   -0.009744766 -0.0166001040              0.9302817</code></pre>
</figure>
<h2>BizSci</h2>
<p>And, here’s how we solved it using <code>tidyquant</code>.</p>
<h5>Step 1: Merge SPY ETF Weekly Returns with Sector ETF Weekly Returns</h5>
<p>First, we add the weekly returns for the “SPY” index (which is currently the last group in the tibble) as it’s own column. This is what our correlations will be calculated against. To do this, we will have to isolate that “SPY” data, and merge it with the original data. The easiest way is to <code>filter()</code> the “SPY” weekly returns and then join (<code>inner_join()</code>) as a new column using <code>by = &quot;date&quot;</code> as the merge key.</p>
<figure>
<pre><code data-lang="r"><span># Isolate SPY
</span><span>index</span><span> </span><span>&lt;-</span><span> </span><span>etf_returns_bizsci</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>ticker</span><span> </span><span>==</span><span> </span><span>&quot;SPY&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>select</span><span>(</span><span>date</span><span>,</span><span> </span><span>weekly.returns</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>index.returns</span><span> </span><span>=</span><span> </span><span>weekly.returns</span><span>)</span><span>
</span><span># Join on date
</span><span>etf_returns_bizsci_joined</span><span> </span><span>&lt;-</span><span> </span><span>inner_join</span><span>(</span><span>etf_returns_bizsci</span><span>,</span><span> </span><span>index</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;date&quot;</span><span>)</span><span>
</span><span>head</span><span>(</span><span>etf_returns_bizsci_joined</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## Source: local data frame [6 x 5]
## Groups: ticker, sector [1]
## 
##   ticker                 sector       date weekly.returns
##    &lt;chr>                  &lt;chr>     &lt;date>          &lt;dbl>
## 1    XLY Consumer Discretionary 2007-01-05   -0.007034027
## 2    XLY Consumer Discretionary 2007-01-12    0.026062578
## 3    XLY Consumer Discretionary 2007-01-19    0.008875499
## 4    XLY Consumer Discretionary 2007-01-26   -0.019888442
## 5    XLY Consumer Discretionary 2007-02-02    0.022409825
## 6    XLY Consumer Discretionary 2007-02-09   -0.006315545
##   index.returns
##           &lt;dbl>
## 1  -0.005888436
## 2   0.019029484
## 3  -0.002936435
## 4  -0.004842978
## 5   0.018680358
## 6  -0.006025965</code></pre>
</figure>
<h5>Step 2: Use tq_mutate_xy() to apply runCor()</h5>
<p>Now what? <em>RViews</em> used the more generic <code>rollapply()</code> function, and then created the function for correlations. While this is definitely possible using <code>tq_mutate</code>, it’s easier to just use the <code>runCor()</code> function from the <code>TTR</code> package through <code>tq_mutate_xy()</code> instead. If you are new to <code>tidyquant</code>, the mutate functions will do exactly what we need:</p>
<ul>
<li>
<p><code>tq_mutate()</code> aggregates the functions from <code>quantmod</code>, <code>xts</code>, <code>zoo</code>, and <code>TTR</code> using OHLCV style data and notation. It accepts <code>ohlc_fun</code> and <code>mutate_fun</code> to apply a function to OHLCV inputs. We don’t use this version because it can’t accept non-OHLC data or apply functions that require two primary arguments.</p>
</li>
<li>
<p><code>tq_mutate_xy()</code> works with functions from <code>quantmod</code>, <code>xts</code>, <code>zoo</code>, and <code>TTR</code> packages that require two arguments (x and y). It’s also used when you have data that is not in OHLCV format. Here, we face both situations. It accepts <code>x</code>, <code>y</code>, and <code>mutate_fun</code> args, which handles our situation perfectly!</p>
</li>
</ul>
<p>The usage of <code>runCor</code> by itself looks like: <code>runCor(x, y, n = 10)</code> so we will use <code>tq_mutate_xy()</code> to pass in the <code>x</code> and <code>y</code> arguments, and then pass through <code>n = 20</code> using the <code>...</code>. As an aside, you may be wondering what the <code>col_rename</code> argument is. Simply put it renames the mutation output, which is surprisingly handy by eliminating one extra line of code.</p>
<figure>
<pre><code data-lang="r"><span># Get running correlations
</span><span>etf_returns_runCor_bizsci</span><span> </span><span>&lt;-</span><span> </span><span>etf_returns_bizsci_joined</span><span> </span><span>%>%</span><span> </span><span>tq_mutate_xy</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>weekly.returns</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>index.returns</span><span>,</span><span> </span><span>mutate_fun</span><span> </span><span>=</span><span> </span><span>runCor</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>20</span><span>,</span><span> </span><span>col_rename</span><span> </span><span>=</span><span> </span><span>&quot;cor&quot;</span><span>)</span><span>
</span><span># Isolate Information Technology, and get past the NA's for our viewing pleasure
</span><span>etf_returns_runCor_bizsci</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>sector</span><span> </span><span>==</span><span> </span><span>&quot;Information Technology&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>slice</span><span>(</span><span>20</span><span>:</span><span>n</span><span>())</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## Source: local data frame [508 x 6]
## Groups: ticker, sector [1]
## 
##    ticker                 sector       date weekly.returns
##     &lt;chr>                  &lt;chr>     &lt;date>          &lt;dbl>
## 1     XLK Information Technology 2007-05-18    0.004752405
## 2     XLK Information Technology 2007-05-25   -0.004752405
## 3     XLK Information Technology 2007-06-01    0.018096442
## 4     XLK Information Technology 2007-06-08   -0.012159397
## 5     XLK Information Technology 2007-06-15    0.017214861
## 6     XLK Information Technology 2007-06-22   -0.009744766
## 7     XLK Information Technology 2007-06-29    0.002738081
## 8     XLK Information Technology 2007-07-06    0.021256839
## 9     XLK Information Technology 2007-07-13    0.018939960
## 10    XLK Information Technology 2007-07-20    0.005612700
##    index.returns       cor
##            &lt;dbl>     &lt;dbl>
## 1   0.0115988780 0.8820099
## 2  -0.0061121613 0.8963962
## 3   0.0156329832 0.8946237
## 4  -0.0199273050 0.9301269
## 5   0.0133507228 0.9311254
## 6  -0.0166001040 0.9302817
## 7  -0.0007974617 0.9303941
## 8   0.0168093546 0.9315747
## 9   0.0121497774 0.9328206
## 10 -0.0087563781 0.8887265
## # ... with 498 more rows</code></pre>
</figure>
<p><strong>There’s an added bonus…</strong></p>
<p>As opposed to the <em>RViews</em> function, <strong>we actually calculated the rolling correlations for <em>all</em> of the ETF groups</strong> in the tibble, not just the one that you pass in! This is typically desired because the user is usually interested in understanding how all groups within a data set correlate to a baseline as opposed to just one.</p>
<h1>Dygraph</h1>
<p>Finally, let’s recreate the <a target="_blank" rel="nofollow" href="https://rstudio.github.io/dygraphs/">Dygraph</a> for the “Information Technology” sector. Dygraphs take an xts object as input, which is NOT the format we are in currently (we are in tibble format). The most useful function here is <code>as_xts()</code>, a <code>tidyquant</code> function that provides an easy way to convert from tibbles to xts. Selecting just the “date” and “cor” columns from the input and specifying the <code>date_col = date</code> in <code>as_xts()</code> allows us to use the same code as <em>RViews</em> to create the Dygraph.</p>
<figure>
<pre><code data-lang="r"><span>etf_returns_runCor_bizsci</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>sector</span><span> </span><span>==</span><span> </span><span>&quot;Information Technology&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>select</span><span>(</span><span>date</span><span>,</span><span> </span><span>cor</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>as_xts</span><span>(</span><span>date_col</span><span> </span><span>=</span><span> </span><span>date</span><span>)</span><span> </span><span>%>%</span><span> </span><span>dygraph</span><span>(</span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Correlation between SP500 and Tech ETF&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>dyAxis</span><span>(</span><span>&quot;y&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>&quot;Correlation&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>dyRangeSelector</span><span>(</span><span>height</span><span> </span><span>=</span><span> </span><span>20</span><span>)</span><span> </span><span>%>%</span><span>
</span><span># Add shading for the recessionary period
</span><span> </span><span>dyShading</span><span>(</span><span>from</span><span> </span><span>=</span><span> </span><span>&quot;2007-12-01&quot;</span><span>,</span><span> </span><span>to</span><span> </span><span>=</span><span> </span><span>&quot;2009-06-01&quot;</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;#FFE6E6&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span># Add an event for the financial crisis. </span><span> </span><span>dyEvent</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;2008-09-15&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>&quot;Fin Crisis&quot;</span><span>,</span><span> </span><span>labelLoc</span><span> </span><span>=</span><span> </span><span>&quot;top&quot;</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>)</span></code></pre>
</figure>
<p><iframe src="http://www.mattdancho.com/figure/source/2017-2-2-Recreating-RViews-Reproducible-Finance-Post/dygraph.html"></iframe></p>
<p>And that’s it! Hopefully you have seen that <code>tidyquant</code> is a great way to streamline and even scale your financial analysis workflow. And, we have only scratched the surface of what it can do! You can check out the stable release of <code>tidyquant</code> from <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/tidyquant/index.html">CRAN</a>, and the development release from <a target="_blank" rel="nofollow" href="https://github.com/mdancho84/tidyquant">Github</a>. Stay tuned for more to come!</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://www.mattdancho.com/financial-analysis/2017/02/02/Recreating-RViews-Reproducible-Finance-Post.html"> mattdancho.com - Articles</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/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/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/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/-BH8JjRFzdc"></a></div>