# Load packages
library(tidyverse)
library(tidyquant)

Get stock prices and convert to returns

Ra <- c("NVDA", "MSFT", "AAPL", "AVGO", "ORCL", "PLTR") %>%
    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: 270 × 3
## # Groups:   symbol [6]
##    symbol date             Ra
##    <chr>  <date>        <dbl>
##  1 NVDA   2022-01-31 -0.187  
##  2 NVDA   2022-02-28 -0.00412
##  3 NVDA   2022-03-31  0.119  
##  4 NVDA   2022-04-29 -0.320  
##  5 NVDA   2022-05-31  0.00674
##  6 NVDA   2022-06-30 -0.188  
##  7 NVDA   2022-07-29  0.198  
##  8 NVDA   2022-08-31 -0.169  
##  9 NVDA   2022-09-30 -0.196  
## 10 NVDA   2022-10-31  0.112  
## # ℹ 260 more rows

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: 45 × 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
## # ℹ 35 more rows

Join two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 270 × 4
## # Groups:   symbol [6]
##    symbol date             Ra      Rb
##    <chr>  <date>        <dbl>   <dbl>
##  1 NVDA   2022-01-31 -0.187   -0.101 
##  2 NVDA   2022-02-28 -0.00412 -0.0343
##  3 NVDA   2022-03-31  0.119    0.0341
##  4 NVDA   2022-04-29 -0.320   -0.133 
##  5 NVDA   2022-05-31  0.00674 -0.0205
##  6 NVDA   2022-06-30 -0.188   -0.0871
##  7 NVDA   2022-07-29  0.198    0.123 
##  8 NVDA   2022-08-31 -0.169   -0.0464
##  9 NVDA   2022-09-30 -0.196   -0.105 
## 10 NVDA   2022-10-31  0.112    0.0390
## # ℹ 260 more rows

Calculate CAPM

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = Rb, 
                   performance_fun = table.CAPM)
RaRb_capm
## # A tibble: 6 × 18
## # Groups:   symbol [6]
##   symbol ActivePremium  Alpha AlphaRobust AnnualizedAlpha  Beta `Beta-`
##   <chr>          <dbl>  <dbl>       <dbl>           <dbl> <dbl>   <dbl>
## 1 NVDA          0.509  0.0313      0.0282          0.447  2.12    2.87 
## 2 MSFT          0.0285 0.0037      0.0027          0.0455 0.867   0.635
## 3 AAPL          0      0.0018      0.0027          0.0215 0.903   0.967
## 4 AVGO          0.477  0.0328      0.0193          0.474  1.24    1.26 
## 5 ORCL          0.325  0.0238      0.0079          0.326  1.33    0.960
## 6 PLTR          0.741  0.0549      0.0225          0.898  1.93    0.878
## # ℹ 11 more variables: `Beta-Robust` <dbl>, `Beta+` <dbl>, `Beta+Robust` <dbl>,
## #   BetaRobust <dbl>, Correlation <dbl>, `Correlationp-value` <dbl>,
## #   InformationRatio <dbl>, `R-squared` <dbl>, `R-squaredRobust` <dbl>,
## #   TrackingError <dbl>, TreynorRatio <dbl>

Which stock has a positively skewed distribution of returns

RaRb_skewness <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = NULL, 
                   performance_fun = skewness)
RaRb_skewness
## # A tibble: 6 × 2
## # Groups:   symbol [6]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 NVDA       -0.122
## 2 MSFT        0.293
## 3 AAPL        0.257
## 4 AVGO        0.825
## 5 ORCL        0.753
## 6 PLTR        1.33

Stocks with a positive skew are NVDA, MSFT, AAPL, AVGRO, ORCL, and PLTR