# 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.9 ──
## ✔ 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("TSLA", "AMZN", "WMT") %>%
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 TSLA 2022-01-31 -0.219
## 2 TSLA 2022-02-28 -0.0708
## 3 TSLA 2022-03-31 0.238
## 4 TSLA 2022-04-29 -0.192
## 5 TSLA 2022-05-31 -0.129
## 6 TSLA 2022-06-30 -0.112
## 7 TSLA 2022-07-29 0.324
## 8 TSLA 2022-08-31 -0.0725
## 9 TSLA 2022-09-30 -0.0376
## 10 TSLA 2022-10-31 -0.142
## # ℹ 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 TSLA 2022-01-31 -0.219 -0.101
## 2 TSLA 2022-02-28 -0.0708 -0.0343
## 3 TSLA 2022-03-31 0.238 0.0341
## 4 TSLA 2022-04-29 -0.192 -0.133
## 5 TSLA 2022-05-31 -0.129 -0.0205
## 6 TSLA 2022-06-30 -0.112 -0.0871
## 7 TSLA 2022-07-29 0.324 0.123
## 8 TSLA 2022-08-31 -0.0725 -0.0464
## 9 TSLA 2022-09-30 -0.0376 -0.105
## 10 TSLA 2022-10-31 -0.142 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 TSLA -0.225 -0.0105 -0.119 1.81 2.26 4.37 0.645
## 2 AMZN -0.0057 0.0009 0.0108 1.37 1.64 2.00 0.840
## 3 WMT 0.172 0.0157 0.205 0.401 0.0564 -0.341 0.450
## # ℹ 5 more variables: `Correlationp-value` <dbl>, InformationRatio <dbl>,
## # `R-squared` <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 = 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 TSLA -0.225 -0.0105 -0.119 1.81 2.26 4.37 0.645
## 2 AMZN -0.0057 0.0009 0.0108 1.37 1.64 2.00 0.840
## 3 WMT 0.172 0.0157 0.205 0.401 0.0564 -0.341 0.450
## # ℹ 5 more variables: `Correlationp-value` <dbl>, InformationRatio <dbl>,
## # `R-squared` <dbl>, TrackingError <dbl>, TreynorRatio <dbl>