<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/many-factor-models/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rviews.rstudio.com/2018/11/19/many-factor-models/"> R Views</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Today, we will return to the Fama French (FF) model of asset returns and use it as a proxy for fitting and evaluating multiple linear models. In a <a rel="nofollow" href="https://rviews.rstudio.com/2018/04/11/introduction-to-fama-french/" target="_blank">previous post</a>, we reviewed how to run the FF three-factor model on the returns of a portfolio. That is, we ran one model on one set of returns. Today, we will run multiple models on multiple streams of returns, which will allow us to compare those models and hopefully build a code scaffolding that can be used when we wish to explore other factor models.</p>
<p>Let’s get to it!</p>
<p>We will start by importing daily prices and calculating the returns of our five usual ETFs: SPY, EFA, IJS, EEM and AGG. I covered the logic for this task in a <a rel="nofollow" href="http://www.reproduciblefinance.com/2017/09/25/asset-prices-to-log-returns/" target="_blank">previous post</a> and the code is below.</p>
<pre><code>library(tidyverse)
library(broom)
library(tidyquant)
library(timetk)


symbols &lt;- c(&quot;SPY&quot;, &quot;EFA&quot;, &quot;IJS&quot;, &quot;EEM&quot;, &quot;AGG&quot;)


# The prices object will hold our daily price data.
prices &lt;- 
  getSymbols(symbols, src = 'yahoo', 
             from = &quot;2012-12-31&quot;,
             to = &quot;2017-12-31&quot;,
             auto.assign = TRUE, 
             warnings = FALSE) %>% 
  map(~Ad(get(.))) %>% 
  reduce(merge) %>%
  `colnames&lt;-`(symbols)


asset_returns_long &lt;-  
  prices %>% 
  tk_tbl(preserve_index = TRUE, rename_index = &quot;date&quot;) %>%
  gather(asset, returns, -date) %>% 
  group_by(asset) %>%  
  mutate(returns = (log(returns) - log(lag(returns)))) %>% 
  na.omit()


asset_returns_long %>% 
  head()</code></pre>
<pre><code># A tibble: 6 x 3
# Groups:   asset [1]
  date       asset  returns
  </code></pre>