library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.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.12 ──
## ✔ PerformanceAnalytics 2.1.0      ✔ TTR                  0.24.4
## ✔ quantmod             0.4.29     ✔ xts                  0.14.2── 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

Get Stock Prices and Convert to Returns

Ra <- c("JPM", "GS", "SOFI") %>%
    tq_get(get  = "stock.prices",
           from = "2010-01-01") %>%
    group_by(symbol) %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Ra")
## Warning: There was 1 warning in `dplyr::mutate()`.
## ℹ In argument: `nested.col = purrr::map(...)`.
## ℹ In group 1: `symbol = "GS"`.
## Caused by warning in `to_period()`:
## ! missing values removed from data
Ra
## # A tibble: 471 × 3
## # Groups:   symbol [3]
##    symbol date            Ra
##    <chr>  <date>       <dbl>
##  1 JPM    2010-01-29 -0.0912
##  2 JPM    2010-02-26  0.0778
##  3 JPM    2010-03-31  0.0662
##  4 JPM    2010-04-30 -0.0474
##  5 JPM    2010-05-28 -0.0705
##  6 JPM    2010-06-30 -0.0750
##  7 JPM    2010-07-30  0.102 
##  8 JPM    2010-08-31 -0.0973
##  9 JPM    2010-09-30  0.0468
## 10 JPM    2010-10-29 -0.0100
## # ℹ 461 more rows

Get Baseline and Convert to Returns

Rb <- "XLK" %>%
    tq_get(get  = "stock.prices",
           from = "2026-01-01") %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Rb")
## Warning in to_period(xx, period = on.opts[[period]], ...): missing values
## removed from data
Rb
## # A tibble: 9 × 2
##   date             Rb
##   <date>        <dbl>
## 1 2026-01-30 -0.00291
## 2 2026-02-27 -0.0356 
## 3 2026-03-31 -0.0410 
## 4 2026-04-30  0.200  
## 5 2026-05-29  0.198  
## 6 2026-06-30 -0.00143
## 7 2026-07-31 -0.0796 
## 8 2026-08-31  0.0636 
## 9 2026-09-21  0.0460

Join the Two Tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 471 × 4
## # Groups:   symbol [3]
##    symbol date            Ra    Rb
##    <chr>  <date>       <dbl> <dbl>
##  1 JPM    2010-01-29 -0.0912    NA
##  2 JPM    2010-02-26  0.0778    NA
##  3 JPM    2010-03-31  0.0662    NA
##  4 JPM    2010-04-30 -0.0474    NA
##  5 JPM    2010-05-28 -0.0705    NA
##  6 JPM    2010-06-30 -0.0750    NA
##  7 JPM    2010-07-30  0.102     NA
##  8 JPM    2010-08-31 -0.0973    NA
##  9 JPM    2010-09-30  0.0468    NA
## 10 JPM    2010-10-29 -0.0100    NA
## # ℹ 461 more rows

Calculate CAPM

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = Rb, 
                   performance_fun = table.CAPM)
## Registered S3 method overwritten by 'robustbase':
##   method          from     
##   hatvalues.lmrob RobStatTM
RaRb_capm
## # A tibble: 3 × 18
## # Groups:   symbol [3]
##   symbol ActivePremium   Alpha AlphaRobust AnnualizedAlpha    Beta `Beta-`
##   <chr>          <dbl>   <dbl>       <dbl>           <dbl>   <dbl>   <dbl>
## 1 JPM           -0.288  0.0174      0.0174          0.230  -0.0435  -0.487
## 2 GS            -0.354 -0.0038     -0.0037         -0.0444  0.442    0.393
## 3 SOFI          -0.910 -0.071      -0.071          -0.587   0.823    0.424
## # ℹ 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 Positevely 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 JPM                    -0.0814
## 2 GS                      0.0225
## 3 SOFI                    0.207