# Load packages
library(tidyverse)
library(tidyquant)

1 Get stock prices and convert to returns

Ra <- c("BAC", "LULU", "CLX") %>%
    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 BAC    2022-01-31 -0.000866
##  2 BAC    2022-02-28 -0.0420  
##  3 BAC    2022-03-31 -0.0629  
##  4 BAC    2022-04-29 -0.134   
##  5 BAC    2022-05-31  0.0426  
##  6 BAC    2022-06-30 -0.158   
##  7 BAC    2022-07-29  0.0861  
##  8 BAC    2022-08-31 -0.00592 
##  9 BAC    2022-09-21 -0.00719 
## 10 LULU   2022-01-31 -0.139   
## # … 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-21 -0.0504

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 BAC    2022-01-31 -0.000866 -0.101 
##  2 BAC    2022-02-28 -0.0420   -0.0343
##  3 BAC    2022-03-31 -0.0629    0.0341
##  4 BAC    2022-04-29 -0.134    -0.133 
##  5 BAC    2022-05-31  0.0426   -0.0205
##  6 BAC    2022-06-30 -0.158    -0.0871
##  7 BAC    2022-07-29  0.0861    0.123 
##  8 BAC    2022-08-31 -0.00592  -0.0464
##  9 BAC    2022-09-21 -0.00719  -0.0504
## 10 LULU   2022-01-31 -0.139    -0.101 
## # … with 17 more rows

4 Calculate CAPM

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = Rb, 
                   performance_fun = SkewnessKurtosisRatio)
RaRb_capm
## # A tibble: 3 × 2
## # Groups:   symbol [3]
##   symbol SkewnessKurtosisRatio.1
##   <chr>                    <dbl>
## 1 BAC                     -0.134
## 2 LULU                     0.106
## 3 CLX                     -0.303

Which stock has a positively skewed distribution of returns?

LULU has a psoitvely skewed distribution of returns