# Load Packages
library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.3.3
## Warning: package 'forcats' was built under R version 4.3.3
library(tidyquant)

1 Get stock prices and convert to returns

Ra <- c("NOC", "WMT", "UPS", "UNH") %>%
    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: 132 × 3
## # Groups:   symbol [4]
##    symbol date              Ra
##    <chr>  <date>         <dbl>
##  1 NOC    2022-01-31 -0.0405  
##  2 NOC    2022-02-28  0.200   
##  3 NOC    2022-03-31  0.0115  
##  4 NOC    2022-04-29 -0.0175  
##  5 NOC    2022-05-31  0.0690  
##  6 NOC    2022-06-30  0.0227  
##  7 NOC    2022-07-29  0.000689
##  8 NOC    2022-08-31  0.00160 
##  9 NOC    2022-09-30 -0.0160  
## 10 NOC    2022-10-31  0.167   
## # ℹ 122 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: 33 × 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
## # ℹ 23 more rows

3 Join the two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 132 × 4
## # Groups:   symbol [4]
##    symbol date              Ra      Rb
##    <chr>  <date>         <dbl>   <dbl>
##  1 NOC    2022-01-31 -0.0405   -0.101 
##  2 NOC    2022-02-28  0.200    -0.0343
##  3 NOC    2022-03-31  0.0115    0.0341
##  4 NOC    2022-04-29 -0.0175   -0.133 
##  5 NOC    2022-05-31  0.0690   -0.0205
##  6 NOC    2022-06-30  0.0227   -0.0871
##  7 NOC    2022-07-29  0.000689  0.123 
##  8 NOC    2022-08-31  0.00160  -0.0464
##  9 NOC    2022-09-30 -0.0160   -0.105 
## 10 NOC    2022-10-31  0.167     0.0390
## # ℹ 122 more rows

4 Calculate Capm

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = Rb, 
                   performance_fun = table.CAPM)
RaRb_capm
## # A tibble: 4 × 13
## # Groups:   symbol [4]
##   symbol ActivePremium   Alpha AnnualizedAlpha   Beta `Beta-` `Beta+`
##   <chr>          <dbl>   <dbl>           <dbl>  <dbl>   <dbl>   <dbl>
## 1 NOC           0.0934  0.0138          0.178  -0.209  0.705   -0.725
## 2 WMT           0.172   0.0157          0.205   0.401  0.0564  -0.341
## 3 UPS          -0.170  -0.013          -0.145   0.746  0.980    0.941
## 4 UNH           0.0277  0.0059          0.0736  0.114  0.241   -0.162
## # ℹ 6 more variables: Correlation <dbl>, `Correlationp-value` <dbl>,
## #   InformationRatio <dbl>, `R-squared` <dbl>, TrackingError <dbl>,
## #   TreynorRatio <dbl>

Which stocks have a positive skew distribution of returns

RaRb_skew <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = NULL, 
                   performance_fun = skewness)
RaRb_skew
## # A tibble: 4 × 2
## # Groups:   symbol [4]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 NOC         0.366
## 2 WMT        -0.467
## 3 UPS        -0.328
## 4 UNH         0.650

NOC and UNH have positive skewing and WMT and UPS have negative skewing. UPS is also the only one not to make a positive return for 2022 on