library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0 ✔ purrr 1.0.1
## ✔ tibble 3.1.8 ✔ dplyr 1.1.0
## ✔ tidyr 1.3.0 ✔ stringr 1.5.0
## ✔ readr 2.1.3 ✔ forcats 1.0.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(tidyquant)
## Loading required package: lubridate
##
## Attaching package: 'lubridate'
##
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
##
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
##
## Attaching package: 'xts'
##
## The following objects are masked from 'package:dplyr':
##
## first, last
##
##
## Attaching package: 'PerformanceAnalytics'
##
## The following object is masked from 'package:graphics':
##
## legend
##
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
Ra <- c("XOM", "TSLA", "DIS") %>%
tq_get(get = "stock.prices",
from = "2010-01-01") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Ra")
Ra
## # A tibble: 469 × 3
## # Groups: symbol [3]
## symbol date Ra
## <chr> <date> <dbl>
## 1 XOM 2010-01-29 -0.0683
## 2 XOM 2010-02-26 0.0154
## 3 XOM 2010-03-31 0.0305
## 4 XOM 2010-04-30 0.0118
## 5 XOM 2010-05-28 -0.102
## 6 XOM 2010-06-30 -0.0561
## 7 XOM 2010-07-30 0.0457
## 8 XOM 2010-08-31 -0.00246
## 9 XOM 2010-09-30 0.0453
## 10 XOM 2010-10-29 0.0761
## # … with 459 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: 14 × 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
## 11 2022-11-30 0.0437
## 12 2022-12-30 -0.0873
## 13 2023-01-31 0.107
## 14 2023-02-17 0.0175
RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 469 × 4
## # Groups: symbol [3]
## symbol date Ra Rb
## <chr> <date> <dbl> <dbl>
## 1 XOM 2010-01-29 -0.0683 NA
## 2 XOM 2010-02-26 0.0154 NA
## 3 XOM 2010-03-31 0.0305 NA
## 4 XOM 2010-04-30 0.0118 NA
## 5 XOM 2010-05-28 -0.102 NA
## 6 XOM 2010-06-30 -0.0561 NA
## 7 XOM 2010-07-30 0.0457 NA
## 8 XOM 2010-08-31 -0.00246 NA
## 9 XOM 2010-09-30 0.0453 NA
## 10 XOM 2010-10-29 0.0761 NA
## # … with 459 more rows
RaRb_capm <- RaRb %>%
tq_performance(Ra = Ra,
Rb = Rb,
performance_fun = table.CAPM)
RaRb_capm
## # A tibble: 3 × 13
## # Groups: symbol [3]
## symbol ActivePr…¹ Alpha Annua…² Beta `Beta-` `Beta+` Corre…³ Corre…⁴ Infor…⁵
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 XOM 0.961 0.0594 0.998 0.398 0.526 0.490 0.292 0.311 2.39
## 2 TSLA -0.139 0.0207 0.279 2.01 0.669 3.13 0.737 0.0026 -0.236
## 3 DIS -0.0586 0.0016 0.0194 1.22 2.09 2.25 0.78 0.001 -0.210
## # … with 3 more variables: `R-squared` <dbl>, TrackingError <dbl>,
## # TreynorRatio <dbl>, and abbreviated variable names ¹ActivePremium,
## # ²AnnualizedAlpha, ³Correlation, ⁴`Correlationp-value`, ⁵InformationRatio
All three of them have positivly scewed distrabution
RaRb_capm <- RaRb %>%
tq_performance(Ra = Ra,
Rb = NULL,
performance_fun = skewness)
RaRb_capm
## # A tibble: 3 × 2
## # Groups: symbol [3]
## symbol skewness.1
## <chr> <dbl>
## 1 XOM 0.385
## 2 TSLA 1.28
## 3 DIS 0.257