# Load packages
library(tidyverse)
library(tidyquant)
Ra <- c("TSLA", "WMT", "KO") %>%
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: 27 × 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-20 0.120
## 10 WMT 2022-01-31 -0.0335
## # … with 17 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: 9 × 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-20 -0.0331
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 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-20 0.120 -0.0331
## 10 WMT 2022-01-31 -0.0335 -0.101
## # … with 17 more rows
Looking at the CAPM for the companies chosen, each one outperformed the market in 2022. Tesla by the most, then Walmart, and lastly Coca-Cola.
RaRb_capm <- RaRb %>%
tq_performance(Ra = Ra,
Rb = Rb,
performance_fun = table.CAPM)
RaRb_capm
## # A tibble: 3 × 13
## # Groups: symbol [3]
## symbol Active…¹ Alpha Annua…² Beta `Beta-` `Beta+` Corre…³ Corre…⁴ Infor…⁵
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 TSLA 0.0609 0.0623 1.06 2.27 1.68 0.960 0.897 0.001 0.136
## 2 WMT 0.266 0.0102 0.129 0.451 -0.66 -0.223 0.441 0.235 0.939
## 3 KO 0.397 0.0021 0.0259 -0.0533 -0.520 0.182 -0.154 0.693 1.35
## # … with 3 more variables: `R-squared` <dbl>, TrackingError <dbl>,
## # TreynorRatio <dbl>, and abbreviated variable names ¹ActivePremium,
## # ²AnnualizedAlpha, ³Correlation, ⁴`Correlationp-value`, ⁵InformationRatio
I continued to get this error when trying to use this function to find the skewedness. I tried to look it up but cant seem to figure it out.
RaRb_capm <- RaRb %>% + tq_performance(Ra = Ra, + Rb = Rb, + performance_fun = skewness) Error in
dplyr::mutate(): ! Problem while computingnested.col = purrr::map(...). ℹ The error occurred in group 1: symbol = “KO”. Caused by error inif (na.rm) ...: ! the condition has length > 1 Runrlang::last_error()to see where the error occurred.