Load Packages
library(tidyverse)
library(tidyquant)
1. Get Stock Prices & Convert to Monthly Returns
Ra <- c("V", "AMZN", "NFLX", "WMT", "KO") %>%
tq_get(get = "stock.prices",
from = "2022-01-01", to = Sys.Date()) %>%
group_by(symbol) %>%
tq_transmute(
select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Ra"
)
2. Get Baseline (XLK) & Convert to Monthly Returns
Rb <- "^IXIC" %>%
tq_get(get = "stock.prices", from = "2022-01-01", to = Sys.Date()) %>%
tq_transmute(
select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Rb"
)
3. Join the Two Tables
RaRb <- left_join(Ra, Rb, by = "date")
4. Calculate CAPM
RaRb_capm <- RaRb %>%
tq_performance(
Ra = Ra,
Rb = Rb,
performance_fun = table.CAPM
)
RaRb_capm
## # A tibble: 5 × 18
## # Groups: symbol [5]
## symbol ActivePremium Alpha AlphaRobust AnnualizedAlpha Beta `Beta-`
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 V 0.105 0.0103 0.0072 0.131 0.553 0.62
## 2 AMZN -0.001 0.0007 0.005 0.008 1.39 1.50
## 3 NFLX 0.172 0.018 0.0278 0.239 1.68 2.24
## 4 WMT 0.191 0.0172 0.0228 0.228 0.469 0.166
## 5 KO 0.0376 0.0075 0.0061 0.0938 0.0776 -0.142
## # ℹ 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_stats <- RaRb %>%
tq_performance(
Ra = Ra,
Rb = NULL,
performance_fun = table.Stats
)
RaRb_stats
## # A tibble: 5 × 17
## # Groups: symbol [5]
## symbol ArithmeticMean GeometricMean Kurtosis `LCLMean(0.95)` Maximum Median
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 V 0.0136 0.0121 0.0255 -0.004 0.166 0.0162
## 2 AMZN 0.009 0.004 0.252 -0.023 0.271 0.0209
## 3 NFLX 0.0281 0.0169 3.16 -0.0165 0.286 0.0372
## 4 WMT 0.0201 0.0182 0.424 0.0006 0.129 0.0171
## 5 KO 0.008 0.0071 0.620 -0.0056 0.122 0.0096
## # ℹ 10 more variables: Minimum <dbl>, NAs <dbl>, Observations <dbl>,
## # Quartile1 <dbl>, Quartile3 <dbl>, SEMean <dbl>, Skewness <dbl>,
## # Stdev <dbl>, `UCLMean(0.95)` <dbl>, Variance <dbl>