# load packages
library(tidyverse)
library(tidyquant)

1 Get stock prices and convert to returns

Ra <- c("PLUG", "AMR", "NVDA") %>%
    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 PLUG   2022-01-31 -0.240  
##  2 PLUG   2022-02-28  0.156  
##  3 PLUG   2022-03-31  0.131  
##  4 PLUG   2022-04-29 -0.265  
##  5 PLUG   2022-05-31 -0.121  
##  6 PLUG   2022-06-30 -0.103  
##  7 PLUG   2022-07-29  0.288  
##  8 PLUG   2022-08-31  0.314  
##  9 PLUG   2022-09-21 -0.0899 
## 10 AMR    2022-01-31  0.00381
## # … with 17 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: 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-21 -0.0504

3 Join the two 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 PLUG   2022-01-31 -0.240   -0.101 
##  2 PLUG   2022-02-28  0.156   -0.0343
##  3 PLUG   2022-03-31  0.131    0.0341
##  4 PLUG   2022-04-29 -0.265   -0.133 
##  5 PLUG   2022-05-31 -0.121   -0.0205
##  6 PLUG   2022-06-30 -0.103   -0.0871
##  7 PLUG   2022-07-29  0.288    0.123 
##  8 PLUG   2022-08-31  0.314   -0.0464
##  9 PLUG   2022-09-21 -0.0899  -0.0504
## 10 AMR    2022-01-31  0.00381 -0.101 
## # … with 17 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 ActiveP…¹   Alpha Annua…²  Beta `Beta-` `Beta+` Corre…³ Corre…⁴ Infor…⁵
##   <chr>      <dbl>   <dbl>   <dbl> <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
## 1 PLUG       0.220  0.0805  1.53   2.08     3.27   1.75    0.729  0.026    0.370
## 2 AMR        2.11   0.131   3.40   0.666    1.21  -3.73    0.224  0.563    2.72 
## 3 NVDA      -0.297 -0.002  -0.0232 2.06     2.60   0.884   0.952  0.0001  -0.892
## # … with 3 more variables: `R-squared` <dbl>, TrackingError <dbl>,
## #   TreynorRatio <dbl>, and abbreviated variable names ¹​ActivePremium,
## #   ²​AnnualizedAlpha, ³​Correlation, ⁴​`Correlationp-value`, ⁵​InformationRatio
RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = Rb, 
                   performance_fun = SkewnessKurtosisRatio)
RaRb_capm
## # A tibble: 3 × 2
## # Groups:   symbol [3]
##   symbol SkewnessKurtosisRatio.1
##   <chr>                    <dbl>
## 1 PLUG                     0.128
## 2 AMR                      0.176
## 3 NVDA                     0.133

Which Stock Beat Market

PLUG, AMR

##Which Stocks Have Positive Skew PLUG, AMR, NVDA