# load packages
library(tidyverse)
library(tidyquant)
library(moments)

1 Get stock prices and convert to returns

Ra <- c("AMZN", "TGT", "WMT") %>%
    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: 27 × 3
## # Groups:   symbol [3]
##    symbol date            Ra
##    <chr>  <date>       <dbl>
##  1 AMZN   2022-01-31 -0.122 
##  2 AMZN   2022-02-28  0.0267
##  3 AMZN   2022-03-31  0.0614
##  4 AMZN   2022-04-29 -0.238 
##  5 AMZN   2022-05-31 -0.0328
##  6 AMZN   2022-06-30 -0.116 
##  7 AMZN   2022-07-29  0.271 
##  8 AMZN   2022-08-31 -0.0606
##  9 AMZN   2022-09-22 -0.0746
## 10 TGT    2022-01-31 -0.0497
## # … with 17 more rows

2 Get baseline and convert to returns

Rb <- "^IXIC" %>%
    tq_get(get  = "stock.prices",
           from = "2022-01-01") %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Rb")
Rb
## # A tibble: 9 × 2
##   date            Rb
##   <date>       <dbl>
## 1 2022-01-31 -0.101 
## 2 2022-02-28 -0.0343
## 3 2022-03-31  0.0341
## 4 2022-04-29 -0.133 
## 5 2022-05-31 -0.0205
## 6 2022-06-30 -0.0871
## 7 2022-07-29  0.123 
## 8 2022-08-31 -0.0464
## 9 2022-09-22 -0.0634

3 Join the two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date")) 
RaRb
## # A tibble: 27 × 4
## # Groups:   symbol [3]
##    symbol date            Ra      Rb
##    <chr>  <date>       <dbl>   <dbl>
##  1 AMZN   2022-01-31 -0.122  -0.101 
##  2 AMZN   2022-02-28  0.0267 -0.0343
##  3 AMZN   2022-03-31  0.0614  0.0341
##  4 AMZN   2022-04-29 -0.238  -0.133 
##  5 AMZN   2022-05-31 -0.0328 -0.0205
##  6 AMZN   2022-06-30 -0.116  -0.0871
##  7 AMZN   2022-07-29  0.271   0.123 
##  8 AMZN   2022-08-31 -0.0606 -0.0464
##  9 AMZN   2022-09-22 -0.0746 -0.0634
## 10 TGT    2022-01-31 -0.0497 -0.101 
## # … with 17 more rows

4 Calculate 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 AMZN     -0.0125  0.0343   0.499 1.82    1.95    2.34    0.978   0     -0.0517
## 2 TGT      -0.035  -0.0137  -0.153 0.594  -1.99    1.06    0.352   0.353 -0.0802
## 3 WMT       0.292   0.011    0.140 0.436  -0.872  -0.223   0.43    0.248  1.01  
## # … 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_skew <- RaRb %>%
    tq_performance(Ra = Ra, 
                   # Rb = Rb, 
                   performance_fun = skewness)
RaRb_skew
## # A tibble: 3 × 2
## # Groups:   symbol [3]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 AMZN        0.835
## 2 TGT        -0.468
## 3 WMT        -0.411