# Load packages 
library(tidyverse)
library(tidyquant)

Get stock prices and convert to retruns

Ra <- c("TM", "SBUX", "AEO") %>%
    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: 99 × 3
## # Groups:   symbol [3]
##    symbol date            Ra
##    <chr>  <date>       <dbl>
##  1 TM     2022-01-31  0.0653
##  2 TM     2022-02-28 -0.0781
##  3 TM     2022-03-31 -0.0148
##  4 TM     2022-04-29 -0.0513
##  5 TM     2022-05-31 -0.0271
##  6 TM     2022-06-30 -0.0733
##  7 TM     2022-07-29  0.0546
##  8 TM     2022-08-31 -0.0813
##  9 TM     2022-09-30 -0.128 
## 10 TM     2022-10-31  0.0654
## # ℹ 89 more rows

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: 33 × 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
## # ℹ 23 more rows

Join the two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 99 × 4
## # Groups:   symbol [3]
##    symbol date            Ra      Rb
##    <chr>  <date>       <dbl>   <dbl>
##  1 TM     2022-01-31  0.0653 -0.101 
##  2 TM     2022-02-28 -0.0781 -0.0343
##  3 TM     2022-03-31 -0.0148  0.0341
##  4 TM     2022-04-29 -0.0513 -0.133 
##  5 TM     2022-05-31 -0.0271 -0.0205
##  6 TM     2022-06-30 -0.0733 -0.0871
##  7 TM     2022-07-29  0.0546  0.123 
##  8 TM     2022-08-31 -0.0813 -0.0464
##  9 TM     2022-09-30 -0.128  -0.105 
## 10 TM     2022-10-31  0.0654  0.0390
## # ℹ 89 more rows

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 TM           -0.0577 -0.0021         -0.0253 0.648  0.0055  0.528        0.541
## 2 SBUX         -0.0848 -0.0036         -0.0421 0.598  0.967   0.0272       0.457
## 3 AEO          -0.100  -0.0005         -0.0061 0.911  1.09    0.816        0.414
## # ℹ 5 more variables: `Correlationp-value` <dbl>, InformationRatio <dbl>,
## #   `R-squared` <dbl>, TrackingError <dbl>, TreynorRatio <dbl>

Which of your stock beat the market in 2022?

None of the stocks I chose beat the market in 2022.

Which stock has a positively skewed distribution of returns?

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = NULL, 
                   performance_fun = skewness)
RaRb_capm
## # A tibble: 3 × 2
## # Groups:   symbol [3]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 TM          0.706
## 2 SBUX        0.386
## 3 AEO         0.589

All of the stocks I chose have positively skewed distribution of returns