<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/introduction-to-portfolio-returns/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rviews.rstudio.com/2017/10/11/from-asset-to-portfolio-returns/"> R Views</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Today, we go back a bit to where we probably should have started in the first place, but it wouldn’t have been as much fun. In our previous work on <a rel="nofollow" href="https://rviews.rstudio.com/2017/07/12/introduction-to-volatility/" target="_blank">volatility</a>, we zipped through the steps of data import, tidy and transformation. Let’s correct that oversight and do some spade work on transforming daily asset prices to monthly portfolio log returns.</p>
<p>Our five-asset portfolio will consist of the following securities and weights:</p>
<pre><code>+ SPY (S&amp;P500 fund) weighted 25%
+ EFA (a non-US equities fund) weighted 25%
+ IJS (a small-cap value fund) weighted 20%
+ EEM (an emerging-mkts fund) weighted 20%
+ AGG (a bond fund) weighted 10%</code></pre>
<p>A brief interlude on portfolios: a portfolio is a weighted collection of assets (its etymology harkens back to the Latin for “to carry sheets of paper”, which I s’pose made its way to the world of investments because securities used to be sheets of paper). The theoretical reason that rational investors prefer a portfolio to a single asset is that a portfolio can offer a better risk/return trade-off due to low or negative covariance amongst portfolio components.</p>
<p>Back to the task at hand: transform a collection of daily ETF prices into an object of portfolio log returns.</p>
<p>Let’s load up our packages.</p>
<pre><code>library(tidyverse)
library(tidyquant)
library(timetk)</code></pre>
<p>First, we import daily prices for the five ETFs, using <code>getSymbols</code> to grab the data, <code>map(~Ad(get(.)))</code> to select adjusted prices only, and <code>reduce(merge)</code> to mash our five prices into one <code>xts</code> object.</p>
<pre><code># The symbols vector holds our tickers. 
symbols &lt;- c(&quot;SPY&quot;,&quot;EFA&quot;, &quot;IJS&quot;, &quot;EEM&quot;,&quot;AGG&quot;)

# The prices object will hold our raw price data throughout this book.
prices &lt;- 
  getSymbols(symbols, src = 'yahoo', from = &quot;2005-01-01&quot;, 
             auto.assign = TRUE, warnings = FALSE) %>% 
  map(~Ad(get(.))) %>% 
  reduce(merge) %>%
  `colnames&lt;-`(symbols)</code></pre>
<p>Next, we convert those daily adjusted prices to monthly log returns using two methods. For the first method, we stay in the <code>xts</code> world.</p>
<pre><code>prices_monthly &lt;- to.monthly(prices, indexAt = &quot;last&quot;, OHLC = FALSE)
asset_returns_xts &lt;- na.omit(Return.calculate(prices_monthly, method = &quot;log&quot;))</code></pre>
<p>For the second method, we will head to the <code>tidyverse/tidyquant</code> world. We will convert from <code>xts</code> to <code>tibble</code> using a call to <code>tk_tbl(preserve_index = TRUE, rename_index = &quot;date&quot;)</code>. We will add a column for log returns using <code>mutate(returns = (log(returns) - log(lag(returns))))</code>.</p>
<pre><code># Tidyverse method, to long, tidy format
asset_returns_long &lt;- 
  prices %>% 
  to.monthly(indexAt = &quot;last&quot;, OHLC = FALSE) %>% 
  tk_tbl(preserve_index = TRUE, rename_index = &quot;date&quot;) %>%
  gather(asset, returns, -date) %>% 
  group_by(asset) %>%  
  mutate(returns = (log(returns) - log(lag(returns))))</code></pre>
<p>Have a peek at both asset return objects.</p>
<pre><code>head(asset_returns_xts)</code></pre>
<pre><code>##                     SPY          EFA         IJS         EEM          AGG
## 2005-02-28  0.020688126  0.037150948  0.02860922  0.09241734 -0.003721035
## 2005-03-31 -0.018461970 -0.026583514 -0.02388198 -0.08240681 -0.009790455
## 2005-04-29 -0.018913092 -0.016309073 -0.05255677 -0.01255390  0.017081857
## 2005-05-31  0.031716351 -0.008674664  0.05973603  0.03111818  0.008242118
## 2005-06-30  0.001514103  0.014225362  0.03840792  0.03892299  0.008724056
## 2005-07-29  0.037547542  0.029527397  0.05677107  0.07400825 -0.010408874</code></pre>
<pre><code>head(asset_returns_long)</code></pre>
<pre><code>## # A tibble: 6 x 3
## # Groups:   asset [1]
##         date asset      returns
##       </code></pre>