# Load packages
library(tidyverse)
library(tidyquant)

1 Get stock prices and convert to returns

Ra <- c("SOXX", "TKR", "VTV") %>%
    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: 171 × 3
## # Groups:   symbol [3]
##    symbol date               Ra
##    <chr>  <date>          <dbl>
##  1 SOXX   2022-01-31 -0.133    
##  2 SOXX   2022-02-28 -0.0111   
##  3 SOXX   2022-03-31  0.0000304
##  4 SOXX   2022-04-29 -0.153    
##  5 SOXX   2022-05-31  0.0633   
##  6 SOXX   2022-06-30 -0.178    
##  7 SOXX   2022-07-29  0.165    
##  8 SOXX   2022-08-31 -0.0924   
##  9 SOXX   2022-09-30 -0.133    
## 10 SOXX   2022-10-31  0.0243   
## # ℹ 161 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: 57 × 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
## # ℹ 47 more rows

3 Join the two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 171 × 4
## # Groups:   symbol [3]
##    symbol date               Ra      Rb
##    <chr>  <date>          <dbl>   <dbl>
##  1 SOXX   2022-01-31 -0.133     -0.101 
##  2 SOXX   2022-02-28 -0.0111    -0.0343
##  3 SOXX   2022-03-31  0.0000304  0.0341
##  4 SOXX   2022-04-29 -0.153     -0.133 
##  5 SOXX   2022-05-31  0.0633    -0.0205
##  6 SOXX   2022-06-30 -0.178     -0.0871
##  7 SOXX   2022-07-29  0.165      0.123 
##  8 SOXX   2022-08-31 -0.0924    -0.0464
##  9 SOXX   2022-09-30 -0.133     -0.105 
## 10 SOXX   2022-10-31  0.0243     0.0390
## # ℹ 161 more rows

4 Calculate CAPM

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra,
                   Rb = Rb,
                   performance_fun = table.CAPM)
RaRb_capm
## # A tibble: 3 × 18
## # Groups:   symbol [3]
##   symbol ActivePremium  Alpha AlphaRobust AnnualizedAlpha  Beta `Beta-`
##   <chr>          <dbl>  <dbl>       <dbl>           <dbl> <dbl>   <dbl>
## 1 SOXX          0.153  0.0086      0.0019          0.108  1.56    1.35 
## 2 TKR           0.0114 0.0065      0.0033          0.0807 0.716   1.04 
## 3 VTV          -0.0056 0.0051      0.0038          0.0631 0.420   0.608
## # ℹ 11 more variables: `Beta-Robust` <dbl>, `Beta+` <dbl>, `Beta+Robust` <dbl>,
## #   BetaRobust <dbl>, Correlation <dbl>, `Correlationp-value` <dbl>,
## #   InformationRatio <dbl>, `R-squared` <dbl>, `R-squaredRobust` <dbl>,
## #   TrackingError <dbl>, TreynorRatio <dbl>

Which stock has a positive skewed distribution of returns

RaRb_skewness <- RaRb %>%
  tq_performance(Ra = Ra,
                 Rb = NULL,
                 performance_fun = skewness)
RaRb_skewness
## # A tibble: 3 × 2
## # Groups:   symbol [3]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 SOXX        0.523
## 2 TKR         0.619
## 3 VTV        -0.173

##SOXX and TKR are positively skewed, VTV is negatively skewed and suffers large downturns.