1 Get stock prices and convert to returns
Ra <- c("HD", "LOW", "COST") %>%
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 HD 2026-01-30 0.0832
## 2 HD 2026-02-27 0.0164
## 3 HD 2026-03-31 -0.130
## 4 HD 2026-04-30 -0.000274
## 5 HD 2026-05-29 -0.0355
## 6 HD 2026-06-30 0.120
## 7 HD 2026-07-31 -0.0588
## 8 HD 2026-08-31 -0.0124
## 9 HD 2026-09-21 -0.0868
## 10 LOW 2026-01-30 0.0866
## # ℹ 17 more rows
2 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")
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
3 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 HD 2026-01-30 0.0832 0.00973
## 2 HD 2026-02-27 0.0164 -0.0338
## 3 HD 2026-03-31 -0.130 -0.0475
## 4 HD 2026-04-30 -0.000274 0.153
## 5 HD 2026-05-29 -0.0355 0.0836
## 6 HD 2026-06-30 0.120 -0.0281
## 7 HD 2026-07-31 -0.0588 -0.0320
## 8 HD 2026-08-31 -0.0124 0.0393
## 9 HD 2026-09-21 -0.0868 0.0285
## 10 LOW 2026-01-30 0.0866 0.00973
## # ℹ 17 more rows
4 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 HD -0.388 -0.0121 -0.0123 -0.136 0.0281 10.7
## 2 LOW -0.504 -0.0261 -0.0262 -0.272 0.0765 6.17
## 3 COST -0.154 0.0105 0.0102 0.133 -0.165 0.915
## # ℹ 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_skewness <- RaRb %>%
tq_performance(Ra = Ra,
Rb = NULL,
performance_fun = skewness)
RaRb_skewness
## # A tibble: 3 × 2
## # Groups: symbol [3]
## symbol skewness.1
## <chr> <dbl>
## 1 HD 0.256
## 2 LOW 0.242
## 3 COST 0.623
# All three stocks have a positive distribution with Costco performing the best of the group