# Load packages
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyquant)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo 
## ── Attaching core tidyquant packages ──────────────────────── tidyquant 1.0.8 ──
## ✔ PerformanceAnalytics 2.0.4      ✔ TTR                  0.24.4
## ✔ quantmod             0.4.26     ✔ xts                  0.14.0── Conflicts ────────────────────────────────────────── tidyquant_conflicts() ──
## ✖ zoo::as.Date()                 masks base::as.Date()
## ✖ zoo::as.Date.numeric()         masks base::as.Date.numeric()
## ✖ dplyr::filter()                masks stats::filter()
## ✖ xts::first()                   masks dplyr::first()
## ✖ dplyr::lag()                   masks stats::lag()
## ✖ xts::last()                    masks dplyr::last()
## ✖ PerformanceAnalytics::legend() masks graphics::legend()
## ✖ quantmod::summary()            masks base::summary()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

1 Get stock prices and convert to returns

Ra <- c("AAPL", "MSFT", "GOOG") %>%
    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 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 
## # ℹ 89 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: 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

3 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 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
## # ℹ 89 more rows

4 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 AAPL          0.0451 0.0043          0.0529 1.02    0.992   1.41        0.840
## 2 MSFT          0.063  0.006           0.0742 0.825   0.750   0.543       0.806
## 3 GOOG          0.0014 0.0016          0.0191 0.916   0.982   0.674       0.756
## # ℹ 5 more variables: `Correlationp-value` <dbl>, InformationRatio <dbl>,
## #   `R-squared` <dbl>, TrackingError <dbl>, TreynorRatio <dbl>

Which stock has a positively skewed distribution of returns?

Two of my stocks are positively skewed with AAPL at 0.277 and MSFT at 0.104.

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 AAPL        0.277
## 2 MSFT        0.104
## 3 GOOG       -0.208