title: “Week 4: Apply 3” author: “Byron hartley” date: “2022-09-23” Get stock prices and convert to returns Get baseline and convert to returns Join the two tables Calculate CAPM Which stock has a positively skewed distribution of returns?
# Load packages
library(tidyverse)
## Warning: package 'purrr' 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.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ 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)
## 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 '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.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("WMT", "F", "AAPL") %>%
tq_get(get = "stock.prices",
from = "2026-01-01") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Ra")
Ra
## # A tibble: 27 × 3
## # Groups: symbol [3]
## symbol date Ra
## <chr> <date> <dbl>
## 1 WMT 2026-01-30 0.0566
## 2 WMT 2026-02-27 0.0739
## 3 WMT 2026-03-31 -0.0267
## 4 WMT 2026-04-30 0.0616
## 5 WMT 2026-05-29 -0.121
## 6 WMT 2026-06-30 -0.0215
## 7 WMT 2026-07-31 -0.0182
## 8 WMT 2026-08-31 -0.0547
## 9 WMT 2026-09-22 0.0501
## 10 F 2026-01-30 0.0405
## # ℹ 17 more rows
Get baseline and convert to returns
Rb <- "^IXIC" %>%
tq_get(get = "stock.prices",
from = "2026-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: 9 × 2
## date Rb
## <date> <dbl>
## 1 2026-01-30 0.00973
## 2 2026-02-27 -0.0338
## 3 2026-03-31 -0.0475
## 4 2026-04-30 0.153
## 5 2026-05-29 0.0836
## 6 2026-06-30 -0.0281
## 7 2026-07-31 -0.0320
## 8 2026-08-31 0.0393
## 9 2026-09-21 0.0285
Join the two tables
RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 27 × 4
## # Groups: symbol [3]
## symbol date Ra Rb
## <chr> <date> <dbl> <dbl>
## 1 WMT 2026-01-30 0.0566 0.00973
## 2 WMT 2026-02-27 0.0739 -0.0338
## 3 WMT 2026-03-31 -0.0267 -0.0475
## 4 WMT 2026-04-30 0.0616 0.153
## 5 WMT 2026-05-29 -0.121 0.0836
## 6 WMT 2026-06-30 -0.0215 -0.0281
## 7 WMT 2026-07-31 -0.0182 -0.0320
## 8 WMT 2026-08-31 -0.0547 0.0393
## 9 WMT 2026-09-22 0.0501 NA
## 10 F 2026-01-30 0.0405 0.00973
## # ℹ 17 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 WMT -0.304 -0.0055 -0.0054 -0.064 -0.0416 1.04
## 2 F -0.0849 -0.0013 -0.0037 -0.0161 1.52 4.47
## 3 AAPL 0.0602 0.0111 0.0111 0.141 0.627 0.965
## # ℹ 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 positively skewed distribution of returns?
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 WMT -0.222
## 2 F 0.293
## 3 AAPL 0.0716