# Load packages
library(tidyverse)
library(tidyquant)
1 Get stock prices and convert to returns
Ra <- c("AAPL", "GOOG", "MSFT") %>%
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 AAPL 2026-01-30 -0.0425
## 2 AAPL 2026-02-27 0.0191
## 3 AAPL 2026-03-31 -0.0393
## 4 AAPL 2026-04-30 0.0692
## 5 AAPL 2026-05-29 0.151
## 6 AAPL 2026-06-30 -0.0727
## 7 AAPL 2026-07-31 0.0676
## 8 AAPL 2026-08-31 0.0266
## 9 AAPL 2026-09-25 0.0764
## 10 GOOG 2026-01-30 0.0736
## # ℹ 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-25 0.0265
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 AAPL 2026-01-30 -0.0425 0.00973
## 2 AAPL 2026-02-27 0.0191 -0.0338
## 3 AAPL 2026-03-31 -0.0393 -0.0475
## 4 AAPL 2026-04-30 0.0692 0.153
## 5 AAPL 2026-05-29 0.151 0.0836
## 6 AAPL 2026-06-30 -0.0727 -0.0281
## 7 AAPL 2026-07-31 0.0676 -0.0320
## 8 AAPL 2026-08-31 0.0266 0.0393
## 9 AAPL 2026-09-25 0.0764 0.0265
## 10 GOOG 2026-01-30 0.0736 0.00973
## # ℹ 17 more rows
4 Calculate CAPM
RaRb_capm <- RaRb %>%
tq_performance(Ra = Ra,
Rb = Rb,
performance_fun = table.CAPM)
RaRb_capm
## # A tibble: 3 × 18
## # Groups: symbol [3]
## symbol ActivePremium Alpha AlphaRobust AnnualizedAlpha Beta `Beta-`
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 0.138 0.0163 0.0163 0.214 0.638 0.965
## 2 GOOG -0.112 -0.0138 -0.0134 -0.153 1.54 1.95
## 3 MSFT -0.0926 0.003 -0.0427 0.0367 0.784 0.677
## # ℹ 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?
Skewness measures whether a stock’s returns are more heavily tilted
toward large positive or large negative 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 AAPL 0.142
## 2 GOOG 1.79
## 3 MSFT 0.238
# All three stocks (AAPL, GOOG, and MSFT) have a positively skewed distribution of returns because their skewness values are all positive. GOOG has the highest skewness at 1.79, followed by MSFT at 0.238 and AAPL at 0.142. This means that Google had the strongest tendency toward larger positive monthly returns during this period.
The SkewnessKurtosisRatio measures the relationship between the
asymmetry of returns and the presence of extreme returns.
RaRb_SkewnessKurtosisRatio <- RaRb %>%
tq_performance(Ra = Ra,
Rb = Rb,
performance_fun = SkewnessKurtosisRatio)
RaRb_SkewnessKurtosisRatio
## # A tibble: 3 × 2
## # Groups: symbol [3]
## symbol SkewnessKurtosisRatio.1
## <chr> <dbl>
## 1 AAPL 0.0667
## 2 GOOG 0.348
## 3 MSFT 0.111
# All three stocks have a positive ratio, with GOOG having the highest ratio at 0.348, followed by MSFT at 0.111 and AAPL at 0.0667. This indicates that Google had the strongest positive skew relative to its overall level of extreme returns during this period.