# Load Packages
library(tidyverse)
library(tidyquant)

1 Get Stock Prices and convert to returns

Ra <- c("AAPL", "GOOG", "NFLX") %>%
    tq_get(get  = "stock.prices",
           from = "2022-01-01",) %>%
    group_by(symbol) %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Ra")
Ra
## # A tibble: 42 × 3
## # Groups:   symbol [3]
##    symbol date            Ra
##    <chr>  <date>       <dbl>
##  1 AAPL   2022-01-31 -0.0397
##  2 AAPL   2022-02-28 -0.0541
##  3 AAPL   2022-03-31  0.0575
##  4 AAPL   2022-04-29 -0.0971
##  5 AAPL   2022-05-31 -0.0545
##  6 AAPL   2022-06-30 -0.0814
##  7 AAPL   2022-07-29  0.189 
##  8 AAPL   2022-08-31 -0.0312
##  9 AAPL   2022-09-30 -0.121 
## 10 AAPL   2022-10-31  0.110 
## # … with 32 more rows

2 Get baseline and convert to returns

Rb <- "XLK" %>%
    tq_get(get  = "stock.prices",
           from = "2022-01-01") %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Rb")
Rb
## # A tibble: 14 × 2
##    date             Rb
##    <date>        <dbl>
##  1 2022-01-31 -0.0772 
##  2 2022-02-28 -0.0488 
##  3 2022-03-31  0.0335 
##  4 2022-04-29 -0.110  
##  5 2022-05-31 -0.00686
##  6 2022-06-30 -0.0926 
##  7 2022-07-29  0.135  
##  8 2022-08-31 -0.0621 
##  9 2022-09-30 -0.120  
## 10 2022-10-31  0.0765 
## 11 2022-11-30  0.0633 
## 12 2022-12-30 -0.0821 
## 13 2023-01-31  0.0926 
## 14 2023-02-17  0.0284

3 Join the two data tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 42 × 4
## # Groups:   symbol [3]
##    symbol date            Ra       Rb
##    <chr>  <date>       <dbl>    <dbl>
##  1 AAPL   2022-01-31 -0.0397 -0.0772 
##  2 AAPL   2022-02-28 -0.0541 -0.0488 
##  3 AAPL   2022-03-31  0.0575  0.0335 
##  4 AAPL   2022-04-29 -0.0971 -0.110  
##  5 AAPL   2022-05-31 -0.0545 -0.00686
##  6 AAPL   2022-06-30 -0.0814 -0.0926 
##  7 AAPL   2022-07-29  0.189   0.135  
##  8 AAPL   2022-08-31 -0.0312 -0.0621 
##  9 AAPL   2022-09-30 -0.121  -0.120  
## 10 AAPL   2022-10-31  0.110   0.0765 
## # … with 32 more rows

4 Calclulate CAPM

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = Rb, 
                   performance_fun = table.CAPM)
RaRb_capm
## # A tibble: 3 × 13
## # Groups:   symbol [3]
##   symbol ActiveP…¹   Alpha Annua…²  Beta `Beta-` `Beta+` Corre…³ Corre…⁴ Infor…⁵
##   <chr>      <dbl>   <dbl>   <dbl> <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
## 1 AAPL      0.0351  0.005   0.0617 1.05    0.628   1.32    0.908  0        0.250
## 2 GOOG     -0.137  -0.0165 -0.181  0.843   1.30    0.910   0.841  0.0002  -0.838
## 3 NFLX     -0.201   0.0065  0.0808 1.78    2.02    3.29    0.735  0.0027  -0.383
## # … with 3 more variables: `R-squared` <dbl>, TrackingError <dbl>,
## #   TreynorRatio <dbl>, and abbreviated variable names ¹​ActivePremium,
## #   ²​AnnualizedAlpha, ³​Correlation, ⁴​`Correlationp-value`, ⁵​InformationRatio

Which Stock Has a positively skewed distribution of returns?

RaRb_Correlation <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = Rb, 
                   performance_fun = table.Correlation)
RaRb_Correlation
## # A tibble: 3 × 5
## # Groups:   symbol [3]
##   symbol  `p-value` `Lower CI` `Upper CI` to.Rb
##   <chr>       <dbl>      <dbl>      <dbl> <dbl>
## 1 AAPL   0.00000709      0.729      0.971 0.908
## 2 GOOG   0.000166        0.560      0.948 0.841
## 3 NFLX   0.00274         0.335      0.911 0.735