# Load packages
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.2 ✔ tibble 3.3.0
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.1.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyquant)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## ── Attaching core tidyquant packages ─────────────────────── tidyquant 1.0.11 ──
## ✔ PerformanceAnalytics 2.0.8 ✔ TTR 0.24.4
## ✔ quantmod 0.4.28 ✔ xts 0.14.1── Conflicts ────────────────────────────────────────── tidyquant_conflicts() ──
## ✖ zoo::as.Date() masks base::as.Date()
## ✖ zoo::as.Date.numeric() masks base::as.Date.numeric()
## ✖ dplyr::filter() masks stats::filter()
## ✖ xts::first() masks dplyr::first()
## ✖ dplyr::lag() masks stats::lag()
## ✖ xts::last() masks dplyr::last()
## ✖ PerformanceAnalytics::legend() masks graphics::legend()
## ✖ quantmod::summary() masks base::summary()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Ra <- c("TSLA", "AMZN", "HD", "NVDA", "LLY", "UAA") %>%
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: 270 × 3
## # Groups: symbol [6]
## 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
## # ℹ 260 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: 270 × 4
## # Groups: symbol [6]
## 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
## # ℹ 260 more rows
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: 6 × 18
## # Groups: symbol [6]
## symbol ActivePremium Alpha AlphaRobust AnnualizedAlpha Beta `Beta-`
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 TSLA -0.0819 -0.0008 -0.0008 -0.0101 1.95 1.88
## 2 AMZN -0.0078 -0.0013 0.0026 -0.015 1.37 1.50
## 3 HD -0.062 -0.0016 -0.0015 -0.0192 0.709 0.212
## 4 NVDA 0.503 0.0313 0.0282 0.447 2.13 2.87
## 5 LLY 0.234 0.0264 0.0264 0.367 0.154 0.0679
## 6 UAA -0.421 -0.0351 -0.038 -0.348 1.32 0.332
## # ℹ 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>
RaRb_skewness <- RaRb %>%
mutate (skewness_Ra = skewness(Ra),
skewness_Rb = skewness(Rb))
RaRb_skewness
## # A tibble: 270 × 6
## # Groups: symbol [6]
## 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
## # ℹ 260 more rows
So, based of the results that are showed above, in 2022, the following stocks beat the market: NVDA and LLY
# 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 LLY
## 4 UAA