# Load packages 
library(tidyverse)
library(tidyquant)

1 Get stock prices and convert to returns

Ra <- c("HMC", "WMT", "TGT") %>%
    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 HMC    2022-01-31  0.0253
##  2 HMC    2022-02-28  0.0342
##  3 HMC    2022-03-31 -0.0620
##  4 HMC    2022-04-29 -0.0711
##  5 HMC    2022-05-31 -0.0514
##  6 HMC    2022-06-30 -0.0301
##  7 HMC    2022-07-29  0.0650
##  8 HMC    2022-08-31  0.0311
##  9 HMC    2022-09-30 -0.175 
## 10 HMC    2022-10-31  0.0570
## # … with 32 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: 14 × 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-30 -0.105 
## 10 2022-10-31  0.0390
## 11 2022-11-30  0.0437
## 12 2022-12-30 -0.0873
## 13 2023-01-31  0.107 
## 14 2023-02-15  0.0420

3 Join the two 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 HMC    2022-01-31  0.0253 -0.101 
##  2 HMC    2022-02-28  0.0342 -0.0343
##  3 HMC    2022-03-31 -0.0620  0.0341
##  4 HMC    2022-04-29 -0.0711 -0.133 
##  5 HMC    2022-05-31 -0.0514 -0.0205
##  6 HMC    2022-06-30 -0.0301 -0.0871
##  7 HMC    2022-07-29  0.0650  0.123 
##  8 HMC    2022-08-31  0.0311 -0.0464
##  9 HMC    2022-09-30 -0.175  -0.105 
## 10 HMC    2022-10-31  0.0570  0.0390
## # … with 32 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 ActivePr…¹  Alpha Annua…²  Beta `Beta-` `Beta+` Corre…³ Corre…⁴ Infor…⁵
##   <chr>       <dbl>  <dbl>   <dbl> <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
## 1 HMC        0.121  0.0048  0.0586 0.600   0.834   0.760   0.667  0.0092  0.547 
## 2 WMT        0.232  0.0121  0.156  0.471  -0.830  -0.262   0.531  0.0509  0.891 
## 3 TGT        0.0155 0.0038  0.0466 0.860  -1.79    1.27    0.571  0.033   0.0439
## # … 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 postive skewed distribution of returns?

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = NULL, 
                   performance_fun = skewness)
RaRb_capm
## # A tibble: 3 × 2
## # Groups:   symbol [3]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 HMC        -0.758
## 2 WMT        -0.446
## 3 TGT        -0.565

HMC: (Negative skewed distribution of returns)
WMT: (Negative skewed distribution of returns)
TGT: (Negative skewed distribution of returns)