# Load packages
library(tidyverse)
library(tidyquant)

1 Get stock prices and convert to returns

Ra <- c("GOOG", "GME", "NVDA", "V") %>%
    tq_get(get  = "stock.prices",
           from = "2024-01-01") %>%
    group_by(symbol) %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Ra")
Ra
## # A tibble: 36 × 3
## # Groups:   symbol [4]
##    symbol date            Ra
##    <chr>  <date>       <dbl>
##  1 GOOG   2024-01-31  0.0161
##  2 GOOG   2024-02-29 -0.0142
##  3 GOOG   2024-03-28  0.0893
##  4 GOOG   2024-04-30  0.0813
##  5 GOOG   2024-05-31  0.0566
##  6 GOOG   2024-06-28  0.0556
##  7 GOOG   2024-07-31 -0.0560
##  8 GOOG   2024-08-30 -0.0464
##  9 GOOG   2024-09-17 -0.0280
## 10 GME    2024-01-31 -0.146 
## # ℹ 26 more rows

2 Get baseline and convert to returns

Rb <- "^IXIC" %>%
    tq_get(get  = "stock.prices",
           from = "2024-01-01") %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Rb")
Rb
## # A tibble: 9 × 2
##   date             Rb
##   <date>        <dbl>
## 1 2024-01-31  0.0270 
## 2 2024-02-29  0.0612 
## 3 2024-03-28  0.0179 
## 4 2024-04-30 -0.0441 
## 5 2024-05-31  0.0688 
## 6 2024-06-28  0.0596 
## 7 2024-07-31 -0.00751
## 8 2024-08-30  0.00649
## 9 2024-09-17 -0.00483

3 Join the two data tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 36 × 4
## # Groups:   symbol [4]
##    symbol date            Ra       Rb
##    <chr>  <date>       <dbl>    <dbl>
##  1 GOOG   2024-01-31  0.0161  0.0270 
##  2 GOOG   2024-02-29 -0.0142  0.0612 
##  3 GOOG   2024-03-28  0.0893  0.0179 
##  4 GOOG   2024-04-30  0.0813 -0.0441 
##  5 GOOG   2024-05-31  0.0566  0.0688 
##  6 GOOG   2024-06-28  0.0556  0.0596 
##  7 GOOG   2024-07-31 -0.0560 -0.00751
##  8 GOOG   2024-08-30 -0.0464  0.00649
##  9 GOOG   2024-09-17 -0.0280 -0.00483
## 10 GME    2024-01-31 -0.146   0.0270 
## # ℹ 26 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+` Correlation
##   <chr>          <dbl>   <dbl>           <dbl> <dbl>   <dbl>   <dbl>       <dbl>
## 1 GOOG         -0.0598  0.0137           0.178 0.165 -3.20     0.512       0.112
## 2 GME           0.022  -0.0565          -0.502 5.93   0.0175  10.5         0.571
## 3 NVDA          1.95    0.0457           0.709 3.15   0.0711   2.52        0.831
## 4 V            -0.0857  0.0122           0.156 0.115  1.93    -0.396       0.119
## # ℹ 5 more variables: `Correlationp-value` <dbl>, InformationRatio <dbl>,
## #   `R-squared` <dbl>, TrackingError <dbl>, TreynorRatio <dbl>

5 Which stock has a positive skewed distribution of returns?

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = NULL, 
                   performance_fun = skewness) 
RaRb_capm
## # A tibble: 4 × 2
## # Groups:   symbol [4]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 GOOG     -0.00939
## 2 GME       2.29   
## 3 NVDA      0.113  
## 4 V        -0.350

The stocks that have a positive skewed distributions are GameStop (GME) and NVIDIA (NVDA). Both Google and Visa have a negative skewed distribution.