# Load packages
library(tidyverse)
library(tidyquant)

1 Get prices and convert to returns

Ra <- c("TGT", "WMT", "COST") %>%
    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: 15 × 3
## # Groups:   symbol [3]
##    symbol date            Ra
##    <chr>  <date>       <dbl>
##  1 TGT    2024-01-31 -0.0281
##  2 TGT    2024-02-29  0.108 
##  3 TGT    2024-03-28  0.159 
##  4 TGT    2024-04-30 -0.0916
##  5 TGT    2024-05-21 -0.0256
##  6 WMT    2024-01-31  0.0374
##  7 WMT    2024-02-29  0.0640
##  8 WMT    2024-03-28  0.0301
##  9 WMT    2024-04-30 -0.0136
## 10 WMT    2024-05-21  0.102 
## 11 COST   2024-01-31  0.0680
## 12 COST   2024-02-29  0.0721
## 13 COST   2024-03-28 -0.0151
## 14 COST   2024-04-30 -0.0117
## 15 COST   2024-05-21  0.108

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: 5 × 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-21  0.0750

3 Join the two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 15 × 4
## # Groups:   symbol [3]
##    symbol date            Ra      Rb
##    <chr>  <date>       <dbl>   <dbl>
##  1 TGT    2024-01-31 -0.0281  0.0270
##  2 TGT    2024-02-29  0.108   0.0612
##  3 TGT    2024-03-28  0.159   0.0179
##  4 TGT    2024-04-30 -0.0916 -0.0441
##  5 TGT    2024-05-21 -0.0256  0.0750
##  6 WMT    2024-01-31  0.0374  0.0270
##  7 WMT    2024-02-29  0.0640  0.0612
##  8 WMT    2024-03-28  0.0301  0.0179
##  9 WMT    2024-04-30 -0.0136 -0.0441
## 10 WMT    2024-05-21  0.102   0.0750
## 11 COST   2024-01-31  0.0680  0.0270
## 12 COST   2024-02-29  0.0721  0.0612
## 13 COST   2024-03-28 -0.0151  0.0179
## 14 COST   2024-04-30 -0.0117 -0.0441
## 15 COST   2024-05-21  0.108   0.0750

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 ActivePremium  Alpha AnnualizedAlpha  Beta `Beta-` `Beta+` Correlation
##   <chr>          <dbl>  <dbl>           <dbl> <dbl>   <dbl>   <dbl>       <dbl>
## 1 TGT           -0.101 0.0005          0.0059 0.867      NA   -1.30       0.384
## 2 WMT            0.292 0.0195          0.260  0.891      NA    1.14       0.970
## 3 COST           0.289 0.0171          0.226  0.991      NA    1.59       0.837
## # ℹ 5 more variables: `Correlationp-value` <dbl>, InformationRatio <dbl>,
## #   `R-squared` <dbl>, TrackingError <dbl>, TreynorRatio <dbl>

Which stock has a positivelt skewed distibution of returns?

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = Rb, 
                   performance_fun = VolatilitySkewness)
RaRb_capm
## # A tibble: 3 × 2
## # Groups:   symbol [3]
##   symbol VolatilitySkewness.1
##   <chr>                 <dbl>
## 1 TGT                   0.568
## 2 WMT                 Inf    
## 3 COST                 14.5
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 TGT                     0.196 
## 2 WMT                     0.0111
## 3 COST                   -0.113

I used the year 2024 and wasn’t sure which skew to use so I provided them both. For the first volatility Target and Costo are in positive skewness while for Kurtosis Target and Walamrt are in positive skewness.