Step 1: Get stock prices and convert to returns (2022 only)

symbols <- c("AAPL", "NFLX", "AMZN")

RA <- symbols %>%
  tq_get(get = "stock.prices", from = "2022-01-01", to = "2022-12-31") %>%
  group_by(symbol) %>%
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn,
               period = "monthly",
               col_rename = "Ra")
RA
## # A tibble: 36 × 3
## # Groups:   symbol [3]
##    symbol date            Ra
##    <chr>  <date>       <dbl>
##  1 AAPL   2022-01-31 -0.0397
##  2 AAPL   2022-02-28 -0.0541
##  3 AAPL   2022-03-31  0.0575
##  4 AAPL   2022-04-29 -0.0971
##  5 AAPL   2022-05-31 -0.0545
##  6 AAPL   2022-06-30 -0.0814
##  7 AAPL   2022-07-29  0.189 
##  8 AAPL   2022-08-31 -0.0312
##  9 AAPL   2022-09-30 -0.121 
## 10 AAPL   2022-10-31  0.110 
## # ℹ 26 more rows

Step 2: Get baseline (NASDAQ Composite Index) and convert to returns

The NASDAQ Composite ticker on Yahoo Finance is ^IXIC — note the caret (^) prefix, which indicates it’s an index rather than a stock.

RB <- "^IXIC" %>%
  tq_get(get = "stock.prices", from = "2022-01-01", to = "2022-12-31") %>%
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn,
               period = "monthly",
               col_rename = "Rb")
RB
## # A tibble: 12 × 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
## 11 2022-11-30  0.0437
## 12 2022-12-30 -0.0873

Step 3: Join the two return tables

RAb <- left_join(RA, RB, by = "date")
RAb
## # A tibble: 36 × 4
## # Groups:   symbol [3]
##    symbol date            Ra      Rb
##    <chr>  <date>       <dbl>   <dbl>
##  1 AAPL   2022-01-31 -0.0397 -0.101 
##  2 AAPL   2022-02-28 -0.0541 -0.0343
##  3 AAPL   2022-03-31  0.0575  0.0341
##  4 AAPL   2022-04-29 -0.0971 -0.133 
##  5 AAPL   2022-05-31 -0.0545 -0.0205
##  6 AAPL   2022-06-30 -0.0814 -0.0871
##  7 AAPL   2022-07-29  0.189   0.123 
##  8 AAPL   2022-08-31 -0.0312 -0.0464
##  9 AAPL   2022-09-30 -0.121  -0.105 
## 10 AAPL   2022-10-31  0.110   0.0390
## # ℹ 26 more rows

Step 4: Calculate capital asset pricing model

A positive alpha (Alpha / AnnualizedAlpha in the CAPM table) means that stock outperformed the NASDAQ Composite benchmark in 2022.

RAb %>%
  tq_performance(Ra = Ra, Rb = Rb, performance_fun = table.CAPM)
## # A tibble: 3 × 18
## # Groups:   symbol [3]
##   symbol ActivePremium   Alpha AlphaRobust AnnualizedAlpha  Beta `Beta-`
##   <chr>          <dbl>   <dbl>       <dbl>           <dbl> <dbl>   <dbl>
## 1 AAPL          0.0569  0.0104      0.0109          0.132   1.08   0.532
## 2 NFLX         -0.167   0.0313      0.0313          0.448   2.07   3.12 
## 3 AMZN         -0.168  -0.0076      0.0358         -0.0871  1.36   1.82 
## # ℹ 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>

Question 2: Which stock (if any) has a positively skewed return distribution?

tq_performance_fun_options()$performance_fun
## NULL
RAb %>%
  tq_performance(Ra = Ra, performance_fun = skewness)
## # A tibble: 3 × 2
## # Groups:   symbol [3]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 AAPL        1.08 
## 2 NFLX       -0.617
## 3 AMZN        1.23

A positive skewness value indicates a stock’s monthly returns are positively skewed (more frequent small losses offset by occasional large gains); a negative value indicates the opposite.