# Load packages
library(tidyverse)
library(tidyquant)
1 Get stock prices and convert to returns
Ra <- c("KO", "PEP") %>%
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: 84 × 3
## # Groups: symbol [2]
## symbol date Ra
## <chr> <date> <dbl>
## 1 KO 2022-01-31 0.0288
## 2 KO 2022-02-28 0.0202
## 3 KO 2022-03-31 0.00377
## 4 KO 2022-04-29 0.0421
## 5 KO 2022-05-31 -0.0190
## 6 KO 2022-06-30 -0.000244
## 7 KO 2022-07-29 0.0200
## 8 KO 2022-08-31 -0.0383
## 9 KO 2022-09-30 -0.0856
## 10 KO 2022-10-31 0.0684
## # ℹ 74 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: 42 × 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
## # ℹ 32 more rows
3 Join the two tables
RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 84 × 4
## # Groups: symbol [2]
## symbol date Ra Rb
## <chr> <date> <dbl> <dbl>
## 1 KO 2022-01-31 0.0288 -0.101
## 2 KO 2022-02-28 0.0202 -0.0343
## 3 KO 2022-03-31 0.00377 0.0341
## 4 KO 2022-04-29 0.0421 -0.133
## 5 KO 2022-05-31 -0.0190 -0.0205
## 6 KO 2022-06-30 -0.000244 -0.0871
## 7 KO 2022-07-29 0.0200 0.123
## 8 KO 2022-08-31 -0.0383 -0.0464
## 9 KO 2022-09-30 -0.0856 -0.105
## 10 KO 2022-10-31 0.0684 0.0390
## # ℹ 74 more rows
4 Calculate CAPM
RaRb_capm <- RaRb %>%
tq_performance(Ra = Ra,
Rb = Rb,
performance_fun = table.CAPM)
RaRb_capm
## # A tibble: 2 × 18
## # Groups: symbol [2]
## symbol ActivePremium Alpha AlphaRobust AnnualizedAlpha Beta `Beta-`
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 KO 0.0171 0.0067 0.0047 0.0838 0.0725 -0.142
## 2 PEP -0.113 -0.0044 -0.0061 -0.0515 0.111 -0.0373
## # ℹ 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 = table.CAPM)
RaRb_capm
## # A tibble: 2 × 18
## # Groups: symbol [2]
## symbol ActivePremium Alpha AlphaRobust AnnualizedAlpha Beta `Beta-`
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 KO 0.0171 0.0067 0.0047 0.0838 0.0725 -0.142
## 2 PEP -0.113 -0.0044 -0.0061 -0.0515 0.111 -0.0373
## # ℹ 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>