# load packages
library(tidyverse) 
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.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.9 ──
## ✔ PerformanceAnalytics 2.0.4      ✔ TTR                  0.24.4
## ✔ quantmod             0.4.26     ✔ xts                  0.14.0── 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

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

2 Get baseline and convert to returns

Rb <- "TSLA" %>%
       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")

3 join the two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 216 × 5
##    symbol.x date            Ra symbol.y       Rb
##    <chr>    <date>       <dbl> <chr>       <dbl>
##  1 AAPL     2010-01-29 -0.103  <NA>     NA      
##  2 AAPL     2010-02-26  0.0654 <NA>     NA      
##  3 AAPL     2010-03-31  0.148  <NA>     NA      
##  4 AAPL     2010-04-30  0.111  <NA>     NA      
##  5 AAPL     2010-05-28 -0.0161 <NA>     NA      
##  6 AAPL     2010-06-30 -0.0208 TSLA     -0.00251
##  7 AAPL     2010-07-30  0.0227 TSLA     -0.163  
##  8 AAPL     2010-08-31 -0.0550 TSLA     -0.0231 
##  9 AAPL     2010-09-30  0.167  TSLA      0.0477 
## 10 AAPL     2010-10-29  0.0607 TSLA      0.0701 
## # ℹ 206 more rows

4 calculate CAPM

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra,
                   Rb = Rb,
                   performance_fun = table.CAPM)
RaRb_capm
## # A tibble: 1 × 12
##   ActivePremium  Alpha AnnualizedAlpha   Beta `Beta-` `Beta+` Correlation
##           <dbl>  <dbl>           <dbl>  <dbl>   <dbl>   <dbl>       <dbl>
## 1            NA 0.0263              NA 0.0717 -0.0366 -0.0123      0.0961
## # ℹ 5 more variables: `Correlationp-value` <dbl>, InformationRatio <dbl>,
## #   `R-squared` <dbl>, TrackingError <dbl>, TreynorRatio <dbl>