# Load packages
library(tidyverse)
library(tidyquant)

Step 1 Get stock prices and convert to returns

Ra <- c("GD", "RTX", "LMT") %>%
    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 GD     2022-01-31  0.0281 
##  2 GD     2022-02-28  0.105  
##  3 GD     2022-03-31  0.0287 
##  4 GD     2022-04-29 -0.0141 
##  5 GD     2022-05-31 -0.0491 
##  6 GD     2022-06-30 -0.0106 
##  7 GD     2022-07-29  0.0245 
##  8 GD     2022-08-31  0.00997
##  9 GD     2022-09-23 -0.0307 
## 10 RTX    2022-01-31  0.0370 
## # … with 17 more rows

Step 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-23 -0.0803

Step 3 Join the two data 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 GD     2022-01-31  0.0281  -0.101 
##  2 GD     2022-02-28  0.105   -0.0343
##  3 GD     2022-03-31  0.0287   0.0341
##  4 GD     2022-04-29 -0.0141  -0.133 
##  5 GD     2022-05-31 -0.0491  -0.0205
##  6 GD     2022-06-30 -0.0106  -0.0871
##  7 GD     2022-07-29  0.0245   0.123 
##  8 GD     2022-08-31  0.00997 -0.0464
##  9 GD     2022-09-23 -0.0307  -0.0803
## 10 RTX    2022-01-31  0.0370  -0.101 
## # … with 17 more rows

Step 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 Active…¹   Alpha Annua…²   Beta `Beta-` `Beta+` Corre…³ Corre…⁴ Infor…⁵
##   <chr>     <dbl>   <dbl>   <dbl>  <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
## 1 GD        0.513  0.0156  0.205   0.141   0.226 -0.0471  0.246    0.524   1.85 
## 2 RTX       0.341 -0.004  -0.0475 -0.035   0.747  0.0579 -0.0414   0.916   0.944
## 3 LMT       0.654  0.0157  0.206  -0.131   0.562 -0.616  -0.183    0.638   1.82 
## # … with 3 more variables: `R-squared` <dbl>, TrackingError <dbl>,
## #   TreynorRatio <dbl>, and abbreviated variable names ¹​ActivePremium,
## #   ²​AnnualizedAlpha, ³​Correlation, ⁴​`Correlationp-value`, ⁵​InformationRatio
RaRb_capm %>% select(symbol, Alpha, Beta)
## # A tibble: 3 × 3
## # Groups:   symbol [3]
##   symbol   Alpha   Beta
##   <chr>    <dbl>  <dbl>
## 1 GD      0.0156  0.141
## 2 RTX    -0.004  -0.035
## 3 LMT     0.0157 -0.131

Which stock has a positively skewed distribution of returns

RaRb_skew <- RaRb %>% tq_performance(Ra = Ra, Rb = Rb, performance_fun = skewness)