# 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)
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
##
## ######################### Warning from 'xts' package ##########################
## # #
## # The dplyr lag() function breaks how base R's lag() function is supposed to #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #
## # source() into this session won't work correctly. #
## # #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
## # dplyr from breaking base R's lag() function. #
## # #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #
## # #
## ###############################################################################
##
## Attaching package: 'xts'
##
## The following objects are masked from 'package:dplyr':
##
## first, last
##
##
## Attaching package: 'PerformanceAnalytics'
##
## The following object is masked from 'package:graphics':
##
## legend
##
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
Ra <- c("MSFT", "GOOG", "DELL") %>%
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: 87 × 3
## # Groups: symbol [3]
## symbol date Ra
## <chr> <date> <dbl>
## 1 MSFT 2022-01-31 -0.0710
## 2 MSFT 2022-02-28 -0.0372
## 3 MSFT 2022-03-31 0.0319
## 4 MSFT 2022-04-29 -0.0999
## 5 MSFT 2022-05-31 -0.0181
## 6 MSFT 2022-06-30 -0.0553
## 7 MSFT 2022-07-29 0.0931
## 8 MSFT 2022-08-31 -0.0667
## 9 MSFT 2022-09-30 -0.109
## 10 MSFT 2022-10-31 -0.00331
## # ℹ 77 more rows
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: 29 × 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
## # ℹ 19 more rows
RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 87 × 4
## # Groups: symbol [3]
## symbol date Ra Rb
## <chr> <date> <dbl> <dbl>
## 1 MSFT 2022-01-31 -0.0710 -0.101
## 2 MSFT 2022-02-28 -0.0372 -0.0343
## 3 MSFT 2022-03-31 0.0319 0.0341
## 4 MSFT 2022-04-29 -0.0999 -0.133
## 5 MSFT 2022-05-31 -0.0181 -0.0205
## 6 MSFT 2022-06-30 -0.0553 -0.0871
## 7 MSFT 2022-07-29 0.0931 0.123
## 8 MSFT 2022-08-31 -0.0667 -0.0464
## 9 MSFT 2022-09-30 -0.109 -0.105
## 10 MSFT 2022-10-31 -0.00331 0.0390
## # ℹ 77 more rows
RaRb_capm <- RaRb %>%
tq_performance(Ra = Ra,
Rb = Rb,
performance_fun = VolatilitySkewness)
RaRb_capm
## # A tibble: 3 × 2
## # Groups: symbol [3]
## symbol VolatilitySkewness.1
## <chr> <dbl>
## 1 MSFT 1.69
## 2 GOOG 1.46
## 3 DELL 10.2
All three stocks have positively skewed distribution of return however DELL is the greatest with 10.2