library(tidyverse)
library(tidyquant)

1 Get stock prices and convert to returns

Ra <- c("AAPL", "MSFT", "WMT") %>%
  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

2 Get baseline and convert to returns

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

3 Join the two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))

RaRb
## # 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

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 AAPL          0.0569 0.0104      0.0109          0.132  1.08    0.532
## 2 MSFT          0.0621 0.0012     -0.008           0.0151 0.826   0.657
## 3 WMT           0.335  0.0208      0.0367          0.28   0.586  -0.830
## # ℹ 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 beat the market?

The CAPM results above compare AAPL, MSFT, and WMT with the NASDAQ Composite Index. A positive Alpha indicates that the stock performed better than expected relative to the benchmark, while a negative Alpha indicates that it performed worse than expected.

Which stock has a positively skewed distribution of returns?

Ra %>%
  summarise(Skewness = PerformanceAnalytics::skewness(Ra))
## # A tibble: 3 × 2
##   symbol Skewness
##   <chr>     <dbl>
## 1 AAPL      1.08 
## 2 MSFT      0.731
## 3 WMT      -0.337

A positive skewness value means the stock has a positively skewed distribution of returns. A negative value means the distribution is negatively skewed.