#Load Packages
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("AAPL", "GOOG","NFLX") %>%
tq_get(get = "stock.prices",
from = "2022-01-01") %>%
group_by(symbol) %>%
tq_transmute(select =adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Ra")
## Warning: There were 3 warnings in `dplyr::mutate()`.
## The first warning was:
## ℹ In argument: `nested.col = purrr::map(...)`.
## ℹ In group 1: `symbol = "AAPL"`.
## Caused by warning in `to_period()`:
## ! missing values removed from data
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 2 remaining warnings.
Ra
## # A tibble: 171 × 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
## # ℹ 161 more rows
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")
## Warning in to_period(xx, period = on.opts[[period]], ...): missing values
## removed from data
Rb
## # A tibble: 57 × 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
## # ℹ 47 more rows
Join the Two Tables
RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 171 × 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
## # ℹ 161 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 AAPL 0.0255 0.0042 0.0047 0.0517 0.860 1.00
## 2 GOOG 0.0867 0.0078 0.0073 0.0981 1.06 1.14
## 3 NFLX -0.0758 -0.0008 0.0087 -0.0095 1.24 1.92
## # ℹ 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 Positive Skew
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.0883
## 2 GOOG 0.102
## 3 NFLX -0.151
# AAPL and GOOG have positive skewness