library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.4.3
## Warning: package 'lubridate' was built under R version 4.4.3
## ── 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)
## Warning: package 'tidyquant' was built under R version 4.4.3
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Warning: package 'xts' was built under R version 4.4.3
## Warning: package 'zoo' was built under R version 4.4.3
## Warning: package 'quantmod' was built under R version 4.4.3
## Warning: package 'PerformanceAnalytics' was built under R version 4.4.3
## ── 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
Ra <- c("TSLA", "MSFT", "DPZ") %>%
tq_get(get = "stock.prices", from = "2022-01-01") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Ra")
Rb <- tq_get("^IXIC", get = "stock.prices", from = "2022-01-01") %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Rb")
RaRb <- left_join(Ra, Rb, by = "date")
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 TSLA -0.122 -0.0021 -0.002 -0.0248 1.99 1.88
## 2 MSFT 0.0559 0.0054 0.0028 0.0671 0.871 0.635
## 3 DPZ -0.108 -0.0047 -0.0047 -0.0549 0.839 0.656
## # ℹ 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>
If we were to look at the stocks individually we would most likely choose Stock number 2 MSFT, as it is currently skewing postive with 0.055 in contrast to the negative skewing stocks showcasing some form of better performace atleast growth wise.