# Load packages
library(tidyverse)
library(tidyquant)

1 Get stock prices and convert to returns

Ra <- c("MCD", "ISRG", "KHC", "FIS", "GOOG") %>%
    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: 170 × 3
## # Groups:   symbol [5]
##    symbol date             Ra
##    <chr>  <date>        <dbl>
##  1 MCD    2022-01-31 -0.0340 
##  2 MCD    2022-02-28 -0.0513 
##  3 MCD    2022-03-31  0.0103 
##  4 MCD    2022-04-29  0.00760
##  5 MCD    2022-05-31  0.0122 
##  6 MCD    2022-06-30 -0.0157 
##  7 MCD    2022-07-29  0.0668 
##  8 MCD    2022-08-31 -0.0369 
##  9 MCD    2022-09-30 -0.0854 
## 10 MCD    2022-10-31  0.182  
## # ℹ 160 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: 34 × 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
## # ℹ 24 more rows

3 Join the two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 170 × 4
## # Groups:   symbol [5]
##    symbol date             Ra      Rb
##    <chr>  <date>        <dbl>   <dbl>
##  1 MCD    2022-01-31 -0.0340  -0.101 
##  2 MCD    2022-02-28 -0.0513  -0.0343
##  3 MCD    2022-03-31  0.0103   0.0341
##  4 MCD    2022-04-29  0.00760 -0.133 
##  5 MCD    2022-05-31  0.0122  -0.0205
##  6 MCD    2022-06-30 -0.0157  -0.0871
##  7 MCD    2022-07-29  0.0668   0.123 
##  8 MCD    2022-08-31 -0.0369  -0.0464
##  9 MCD    2022-09-30 -0.0854  -0.105 
## 10 MCD    2022-10-31  0.182    0.0390
## # ℹ 160 more rows

4 Calculate CAPM

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = Rb, 
                   performance_fun = table.CAPM)
RaRb_capm
## # A tibble: 5 × 13
## # Groups:   symbol [5]
##   symbol ActivePremium   Alpha AnnualizedAlpha    Beta `Beta-` `Beta+`
##   <chr>          <dbl>   <dbl>           <dbl>   <dbl>   <dbl>   <dbl>
## 1 MCD           0.0092  0.0038          0.0466  0.402    0.278  0.159 
## 2 ISRG          0.0809  0.009           0.114   1.22     1.16  -0.0466
## 3 KHC          -0.0153  0.0051          0.0624 -0.0197  -0.214 -0.230 
## 4 FIS          -0.108  -0.0048         -0.0559  0.799   -0.192  0.408 
## 5 GOOG         -0.0058  0.001           0.0123  0.916    0.982  0.721 
## # ℹ 6 more variables: Correlation <dbl>, `Correlationp-value` <dbl>,
## #   InformationRatio <dbl>, `R-squared` <dbl>, TrackingError <dbl>,
## #   TreynorRatio <dbl>

Which stock has a positively skewed distribution of returns?

In my Portfolio, ISRG, KHC and MCD have a postively skewed distribution of returns

# Calculate skewness for each asset
RaRb_skewness <- RaRb %>%
    group_by(symbol) %>%   
    summarise(skewness = skewness(Ra))  
RaRb_skewness
## # A tibble: 5 × 2
##   symbol skewness
##   <chr>     <dbl>
## 1 FIS      -0.313
## 2 GOOG     -0.202
## 3 ISRG      0.209
## 4 KHC       0.240
## 5 MCD       1.04