# Load packages
library(tidyverse)
library(tidyquant)

Get stock prices and convert to returns

Ra <- c("NVDA", "TSLA", "MTN") %>%
    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: 99 × 3
## # Groups:   symbol [3]
##    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  
## # ℹ 89 more rows

Get baseline and convert to returns

Rb <- "^NYA" %>%
    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.0329
##  2 2022-02-28 -0.0208
##  3 2022-03-31  0.0219
##  4 2022-04-29 -0.0633
##  5 2022-05-31  0.0136
##  6 2022-06-30 -0.0846
##  7 2022-07-29  0.0580
##  8 2022-08-31 -0.0343
##  9 2022-09-30 -0.0898
## 10 2022-10-31  0.0946
## # ℹ 23 more rows

Join the two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 99 × 4
## # Groups:   symbol [3]
##    symbol date             Ra      Rb
##    <chr>  <date>        <dbl>   <dbl>
##  1 NVDA   2022-01-31 -0.187   -0.0329
##  2 NVDA   2022-02-28 -0.00412 -0.0208
##  3 NVDA   2022-03-31  0.119    0.0219
##  4 NVDA   2022-04-29 -0.320   -0.0633
##  5 NVDA   2022-05-31  0.00674  0.0136
##  6 NVDA   2022-06-30 -0.188   -0.0846
##  7 NVDA   2022-07-29  0.198    0.0580
##  8 NVDA   2022-08-31 -0.169   -0.0343
##  9 NVDA   2022-09-30 -0.196   -0.0898
## 10 NVDA   2022-10-31  0.112    0.0946
## # ℹ 89 more rows

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 ActivePremium   Alpha AnnualizedAlpha  Beta `Beta-` `Beta+` Correlation
##   <chr>          <dbl>   <dbl>           <dbl> <dbl>   <dbl>   <dbl>       <dbl>
## 1 NVDA           0.628  0.0475          0.745  2.17    4.12    1.18        0.607
## 2 TSLA          -0.192 -0.003          -0.0349 1.33    1.18    1.96        0.339
## 3 MTN           -0.202 -0.0166         -0.182  0.961   0.522   0.823       0.681
## # ℹ 5 more variables: `Correlationp-value` <dbl>, InformationRatio <dbl>,
## #   `R-squared` <dbl>, TrackingError <dbl>, TreynorRatio <dbl>

Which Stock has a positive skewed distrobution 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 NVDA       -0.214
## 2 TSLA        0.289
## 3 MTN         0.201