Download monthly stock prices for the previous 5 years for Wesfarmers (WES), Woolworths (WOW), Rio Tinto (RIO), BHP (BHP), and Telstra (TLS). Download monthly values of the ASX 200 for the same period.
# Wesfarmers, Woolworths, Rio Tinto, BHP Billiton, Telstra.
Ra <- c("BHP.AX", "WES.AX", "WOW.AX", "RIO.AX", "TLS.AX") %>%
tq_get(get = "stock.prices",
from = "2012-08-21",
to = "2017-08-21") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Ra")
head(Ra)
## # A tibble: 6 x 3
## # Groups: symbol [1]
## symbol date Ra
## <chr> <date> <dbl>
## 1 BHP.AX 2012-08-31 -0.04448536
## 2 BHP.AX 2012-09-28 0.06759746
## 3 BHP.AX 2012-10-31 0.02360914
## 4 BHP.AX 2012-11-29 0.01685279
## 5 BHP.AX 2012-12-30 0.07880419
## 6 BHP.AX 2013-01-31 0.02210116
# ASX200
Rb <- "^AXJO" %>%
tq_get(get = "stock.prices",
from = "2012-08-21",
to = "2017-08-21") %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Rb")
head(Rb)
## # A tibble: 6 x 2
## date Rb
## <date> <dbl>
## 1 2012-08-31 -0.01535333
## 2 2012-09-28 0.01642684
## 3 2012-10-31 0.01609302
## 4 2012-11-29 0.01085784
## 5 2012-12-30 0.03173546
## 6 2013-01-31 0.05852874
# Combine the two data sets using the “date” field and left_join (dplyr).
RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
# Calculate CAPM metrics
RaRb_capm <- RaRb %>%
tq_performance(Ra = Ra,
Rb = Rb,
performance_fun = table.CAPM)
RaRb_capm
## # A tibble: 5 x 13
## # Groups: symbol [5]
## symbol ActivePremium Alpha AnnualizedAlpha Beta `Beta-` `Beta+`
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 BHP.AX -0.0353 -0.0012 -0.0139 1.0640 1.2217 1.7092
## 2 WES.AX 0.0405 0.0038 0.0466 1.0102 0.7319 0.5508
## 3 WOW.AX -0.0133 -0.0001 -0.0011 0.9067 1.0862 0.6476
## 4 RIO.AX 0.0406 0.0052 0.0647 0.9046 0.8767 1.4248
## 5 TLS.AX 0.0272 0.0038 0.0461 0.7273 0.4970 0.8500
## # ... with 6 more variables: Correlation <dbl>,
## # `Correlationp-value` <dbl>, InformationRatio <dbl>, `R-squared` <dbl>,
## # TrackingError <dbl>, TreynorRatio <dbl>
## Adding missing grouping variables: `symbol`
| symbol | Alpha | Beta |
|---|---|---|
| BHP.AX | -0.0012 | 1.0640 |
| WES.AX | 0.0038 | 1.0102 |
| WOW.AX | -0.0001 | 0.9067 |
| RIO.AX | 0.0052 | 0.9046 |
| TLS.AX | 0.0038 | 0.7273 |
RaRb %>%
ggplot(aes(Rb, Ra, group = symbol, color = symbol)) +
geom_point() +
facet_wrap(~ symbol, ncol = 2) +
labs(title = "Stock Betas",
x = "ASX 200 Return", y = "Stock Returns") +
geom_smooth(method = "lm") +
scale_y_continuous(labels = scales::percent) +
guides(color = FALSE)
Ra %>%
ggplot(aes(date, Ra, group = symbol, color = symbol)) +
geom_line() +
facet_wrap(~ symbol, ncol = 2, scale = "free_y")
Rb %>%
ggplot(aes(date, Rb, fill = Rb)) +
geom_bar(stat = "identity") +
labs(title = "ASX200 Returns",
x = "", y = "Monthly Returns") +
geom_smooth(method = "lm") +
theme_tq() +
scale_color_tq() +
scale_y_continuous(labels = scales::percent) +
guides(fill = FALSE)