# Load packages
library(tidyverse)
library(tidyquant)

1 Get stock prices and convert to returns

Ra <- c("AAPL", "GOOG", "MSFT") %>%
    tq_get(get  = "stock.prices",
           from = "2026-01-01") %>%
    group_by(symbol) %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Ra")
Ra
## # A tibble: 27 × 3
## # Groups:   symbol [3]
##    symbol date            Ra
##    <chr>  <date>       <dbl>
##  1 AAPL   2026-01-30 -0.0425
##  2 AAPL   2026-02-27  0.0191
##  3 AAPL   2026-03-31 -0.0393
##  4 AAPL   2026-04-30  0.0692
##  5 AAPL   2026-05-29  0.151 
##  6 AAPL   2026-06-30 -0.0727
##  7 AAPL   2026-07-31  0.0676
##  8 AAPL   2026-08-31  0.0266
##  9 AAPL   2026-09-21  0.0698
## 10 GOOG   2026-01-30  0.0736
## # ℹ 17 more rows

2 Get baseline and convert to returns

Rb <- "^IXIC" %>%
    tq_get(get  = "stock.prices",
           from = "2026-01-01") %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Rb")
Rb
## # A tibble: 9 × 2
##   date             Rb
##   <date>        <dbl>
## 1 2026-01-30  0.00973
## 2 2026-02-27 -0.0338 
## 3 2026-03-31 -0.0475 
## 4 2026-04-30  0.153  
## 5 2026-05-29  0.0836 
## 6 2026-06-30 -0.0281 
## 7 2026-07-31 -0.0320 
## 8 2026-08-31  0.0393 
## 9 2026-09-21  0.0285

3 Join the two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 27 × 4
## # Groups:   symbol [3]
##    symbol date            Ra       Rb
##    <chr>  <date>       <dbl>    <dbl>
##  1 AAPL   2026-01-30 -0.0425  0.00973
##  2 AAPL   2026-02-27  0.0191 -0.0338 
##  3 AAPL   2026-03-31 -0.0393 -0.0475 
##  4 AAPL   2026-04-30  0.0692  0.153  
##  5 AAPL   2026-05-29  0.151   0.0836 
##  6 AAPL   2026-06-30 -0.0727 -0.0281 
##  7 AAPL   2026-07-31  0.0676 -0.0320 
##  8 AAPL   2026-08-31  0.0266  0.0393 
##  9 AAPL   2026-09-21  0.0698  0.0285 
## 10 GOOG   2026-01-30  0.0736  0.00973
## # ℹ 17 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.124   0.0154      0.0154          0.201  0.638   0.965
## 2 GOOG         -0.0729 -0.011      -0.0098         -0.124  1.55    1.95 
## 3 MSFT         -0.138  -0.0002     -0.0466         -0.0023 0.776   0.677
## # ℹ 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 positively skewed distribution of returns?

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 AAPL                    0.0747
## 2 GOOG                    0.346 
## 3 MSFT                    0.146