# Load packages
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyquant)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo 
## ── Attaching core tidyquant packages ─────────────────────── tidyquant 1.0.12 ──
## ✔ PerformanceAnalytics 2.1.0      ✔ TTR                  0.24.4
## ✔ quantmod             0.4.29     ✔ xts                  0.14.2── Conflicts ────────────────────────────────────────── tidyquant_conflicts() ──
## ✖ zoo::as.Date()                 masks base::as.Date()
## ✖ zoo::as.Date.numeric()         masks base::as.Date.numeric()
## ✖ dplyr::filter()                masks stats::filter()
## ✖ xts::first()                   masks dplyr::first()
## ✖ dplyr::lag()                   masks stats::lag()
## ✖ xts::last()                    masks dplyr::last()
## ✖ PerformanceAnalytics::legend() masks graphics::legend()
## ✖ quantmod::summary()            masks base::summary()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

1 Get stock prices and convert to returns

Ra <- c("AAPL", "GOOG", "NFLX") %>%
  tq_get(get = "stock.prices",
  from = "2010-01-01",
  to = "2015-12-31") %>%
  group_by(symbol) %>%
  tq_transmute(select = adjusted,
  mutate_fun = periodReturn,
  period = "monthly",
  col_rename = "Ra")
Ra
## # A tibble: 216 × 3
## # Groups:   symbol [3]
##    symbol date            Ra
##    <chr>  <date>       <dbl>
##  1 AAPL   2010-01-29 -0.103 
##  2 AAPL   2010-02-26  0.0654
##  3 AAPL   2010-03-31  0.148 
##  4 AAPL   2010-04-30  0.111 
##  5 AAPL   2010-05-28 -0.0161
##  6 AAPL   2010-06-30 -0.0208
##  7 AAPL   2010-07-30  0.0227
##  8 AAPL   2010-08-31 -0.0550
##  9 AAPL   2010-09-30  0.167 
## 10 AAPL   2010-10-29  0.0607
## # ℹ 206 more rows

Get baseline and convert to returns

Rb <- "XLK" %>%
  tq_get(get = "stock.prices",
  from = "2010-01-01",
  to = "2015-12-31") %>%
  group_by(symbol) %>%
  tq_transmute(select = adjusted,
  mutate_fun = periodReturn,
  period = "monthly",
  col_rename = "Rb")
Rb
## # A tibble: 72 × 3
## # Groups:   symbol [1]
##    symbol date            Rb
##    <chr>  <date>       <dbl>
##  1 XLK    2010-01-29 -0.0993
##  2 XLK    2010-02-26  0.0348
##  3 XLK    2010-03-31  0.0684
##  4 XLK    2010-04-30  0.0126
##  5 XLK    2010-05-28 -0.0748
##  6 XLK    2010-06-30 -0.0540
##  7 XLK    2010-07-30  0.0745
##  8 XLK    2010-08-31 -0.0561
##  9 XLK    2010-09-30  0.117 
## 10 XLK    2010-10-29  0.0578
## # ℹ 62 more rows

3 Join the two tables

RaRb <- left_join(Ra, Rb, by = "date") %>%
  select(symbol.x, date, Ra, Rb)

4 Calculate CAPM

RaRb_capm <- RaRb %>%
  group_by(symbol.x) %>%
  tq_performance(
    Ra = Ra,
    Rb = Rb,
    performance_fun = table.CAPM
  )
## Registered S3 method overwritten by 'robustbase':
##   method          from     
##   hatvalues.lmrob RobStatTM
RaRb_capm
## # A tibble: 3 × 18
## # Groups:   symbol.x [3]
##   symbol.x ActivePremium  Alpha AlphaRobust AnnualizedAlpha  Beta `Beta-`
##   <chr>            <dbl>  <dbl>       <dbl>           <dbl> <dbl>   <dbl>
## 1 AAPL             0.119 0.0089      0.0095           0.112 1.11    0.578
## 2 GOOG             0.034 0.0028     -0.0005           0.034 1.14    1.39 
## 3 NFLX             0.447 0.053       0.0439           0.859 0.384  -1.52 
## # ℹ 11 more variables: `Beta-Robust` <dbl>, `Beta+` <dbl>, `Beta+Robust` <dbl>,
## #   BetaRobust <dbl>, Correlation <dbl>, `Correlationp-value` <dbl>,
## #   InformationRatio <dbl>, `R-squared` <dbl>, `R-squaredRobust` <dbl>,
## #   TrackingError <dbl>, TreynorRatio <dbl>