# 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.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.4
## ── 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.11 ──
## ✔ PerformanceAnalytics 2.0.8 ✔ TTR 0.24.4
## ✔ quantmod 0.4.27 ✔ xts 0.14.1── 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 & Convert To Returns
Ra <- c("NVDA", "PLTR", "MSFT", "CSCO") %>%
tq_get(get = "stock.prices",
from = "2025-01-01",
to = Sys.Date()) %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "weekly",
col_rename = "Ra")
Ra
## # A tibble: 84 × 3
## # Groups: symbol [4]
## symbol date Ra
## <chr> <date> <dbl>
## 1 NVDA 2025-01-03 0.0445
## 2 NVDA 2025-01-10 -0.0593
## 3 NVDA 2025-01-17 0.0132
## 4 NVDA 2025-01-24 0.0357
## 5 NVDA 2025-01-31 -0.158
## 6 NVDA 2025-02-07 0.0814
## 7 NVDA 2025-02-14 0.0694
## 8 NVDA 2025-02-21 -0.0318
## 9 NVDA 2025-02-28 -0.0707
## 10 NVDA 2025-03-07 -0.0979
## # ℹ 74 more rows
2 Get Baseline & Convert To Returns
Rb <- "^IXIC" %>%
tq_get(get = "stock.prices",
from = "2025-01-01") %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "weekly",
col_rename = "Rb")
Rb
## # A tibble: 21 × 2
## date Rb
## <date> <dbl>
## 1 2025-01-03 0.0177
## 2 2025-01-10 -0.0234
## 3 2025-01-17 0.0245
## 4 2025-01-24 0.0165
## 5 2025-01-31 -0.0164
## 6 2025-02-07 -0.00530
## 7 2025-02-14 0.0258
## 8 2025-02-21 -0.0251
## 9 2025-02-28 -0.0347
## 10 2025-03-07 -0.0345
## # ℹ 11 more rows
3 Join the two tables
RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 84 × 4
## # Groups: symbol [4]
## symbol date Ra Rb
## <chr> <date> <dbl> <dbl>
## 1 NVDA 2025-01-03 0.0445 0.0177
## 2 NVDA 2025-01-10 -0.0593 -0.0234
## 3 NVDA 2025-01-17 0.0132 0.0245
## 4 NVDA 2025-01-24 0.0357 0.0165
## 5 NVDA 2025-01-31 -0.158 -0.0164
## 6 NVDA 2025-02-07 0.0814 -0.00530
## 7 NVDA 2025-02-14 0.0694 0.0258
## 8 NVDA 2025-02-21 -0.0318 -0.0251
## 9 NVDA 2025-02-28 -0.0707 -0.0347
## 10 NVDA 2025-03-07 -0.0979 -0.0345
## # ℹ 74 more rows
4 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: 4 × 18
## # Groups: symbol [4]
## symbol ActivePremium Alpha AlphaRobust AnnualizedAlpha Beta `Beta-`
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 NVDA -0.0607 0.002 0.0017 0.110 1.81 1.64
## 2 PLTR 2.27 0.0308 0.0152 3.85 2.07 2.39
## 3 MSFT 0.276 0.005 0.0039 0.293 0.825 0.402
## 4 CSCO 0.272 0.0047 0.0049 0.273 0.754 1.07
## # ℹ 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>
5 Which stock has a positively skewed distribution of returns?
RaRb_capm <- RaRb %>%
tq_performance(Ra = Ra,
Rb = Rb,
performance_fun = VolatilitySkewness)
RaRb_capm
## # A tibble: 4 × 2
## # Groups: symbol [4]
## symbol VolatilitySkewness.1
## <chr> <dbl>
## 1 NVDA 1.26
## 2 PLTR 2.84
## 3 MSFT 1.36
## 4 CSCO 0.946