Ra <- c("TSLA", "AMZN", "AAPL", "NVDA", "PG") %>%
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: 225 × 3
## # Groups: symbol [5]
## 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-30 -0.0376
## 10 TSLA 2022-10-31 -0.142
## # ℹ 215 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: 45 × 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-30 -0.105
## 10 2022-10-31 0.0390
## # ℹ 35 more rows
RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 225 × 4
## # Groups: symbol [5]
## 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-30 -0.0376 -0.105
## 10 TSLA 2022-10-31 -0.142 0.0390
## # ℹ 215 more rows
RaRb_capm <- RaRb %>%
tq_performance(Ra = Ra,
Rb = Rb,
performance_fun = table.CAPM)
RaRb_capm
## # A tibble: 5 × 13
## # Groups: symbol [5]
## symbol ActivePremium Alpha AnnualizedAlpha Beta `Beta-` `Beta+` Correlation
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 TSLA -0.0819 -0.0008 -0.0101 1.95 1.88 3.01 0.641
## 2 AMZN -0.0078 -0.0013 -0.015 1.37 1.50 1.71 0.848
## 3 AAPL -0.0161 0.0006 0.007 0.888 0.967 0.995 0.736
## 4 NVDA 0.503 0.0313 0.447 2.13 2.87 2.16 0.834
## 5 PG -0.0791 0.0015 0.0187 0.114 -0.196 -0.226 0.138
## # ℹ 5 more variables: `Correlationp-value` <dbl>, InformationRatio <dbl>,
## # `R-squared` <dbl>, TrackingError <dbl>, TreynorRatio <dbl>
RaRb_skewness <- RaRb %>%
mutate (skewness_Ra = skewness(Ra),
skewness_Rb = skewness(Rb))
RaRb_skewness
## # A tibble: 225 × 6
## # Groups: symbol [5]
## symbol date Ra Rb skewness_Ra skewness_Rb
## <chr> <date> <dbl> <dbl> <dbl> <dbl>
## 1 TSLA 2022-01-31 -0.219 -0.101 0.218 -0.383
## 2 TSLA 2022-02-28 -0.0708 -0.0343 0.218 -0.383
## 3 TSLA 2022-03-31 0.238 0.0341 0.218 -0.383
## 4 TSLA 2022-04-29 -0.192 -0.133 0.218 -0.383
## 5 TSLA 2022-05-31 -0.129 -0.0205 0.218 -0.383
## 6 TSLA 2022-06-30 -0.112 -0.0871 0.218 -0.383
## 7 TSLA 2022-07-29 0.324 0.123 0.218 -0.383
## 8 TSLA 2022-08-31 -0.0725 -0.0464 0.218 -0.383
## 9 TSLA 2022-09-30 -0.0376 -0.105 0.218 -0.383
## 10 TSLA 2022-10-31 -0.142 0.0390 0.218 -0.383
## # ℹ 215 more rows
Based on the results that are showed above, in 2022, the following stocks beat the market: AAPL, PG, and NVDA.
# Create a bar plot of skewness
library(ggplot2)
ggplot(RaRb_skewness, aes(x = symbol, y = skewness_Ra, fill = symbol)) +
geom_bar(stat = "identity", position = "dodge", show.legend = FALSE) +
labs(x = "Stock", y = "Skewness", title = "Skewness of Returns") +
theme_minimal()
# Filter stocks with positive skewness and get distinct symbols
positive_skew_stocks <- RaRb_skewness %>%
filter(skewness_Ra > 0) %>%
distinct(symbol)
# Print the stocks with positive skewness
positive_skew_stocks
## # A tibble: 4 × 1
## # Groups: symbol [4]
## symbol
## <chr>
## 1 TSLA
## 2 AMZN
## 3 AAPL
## 4 PG