This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
# load packages
library(tidyverse)
library(tidyquant)
Ra <- c("AAPL", "GOOG", "NFLX") %>%
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: 90 × 3
## # Groups: symbol [3]
## symbol date Ra
## <chr> <date> <dbl>
## 1 AAPL 2022-01-31 -0.0397
## 2 AAPL 2022-02-28 -0.0541
## 3 AAPL 2022-03-31 0.0575
## 4 AAPL 2022-04-29 -0.0971
## 5 AAPL 2022-05-31 -0.0545
## 6 AAPL 2022-06-30 -0.0814
## 7 AAPL 2022-07-29 0.189
## 8 AAPL 2022-08-31 -0.0312
## 9 AAPL 2022-09-30 -0.121
## 10 AAPL 2022-10-31 0.110
## # ℹ 80 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")
RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 90 × 4
## # Groups: symbol [3]
## symbol date Ra Rb
## <chr> <date> <dbl> <dbl>
## 1 AAPL 2022-01-31 -0.0397 -0.101
## 2 AAPL 2022-02-28 -0.0541 -0.0343
## 3 AAPL 2022-03-31 0.0575 0.0341
## 4 AAPL 2022-04-29 -0.0971 -0.133
## 5 AAPL 2022-05-31 -0.0545 -0.0205
## 6 AAPL 2022-06-30 -0.0814 -0.0871
## 7 AAPL 2022-07-29 0.189 0.123
## 8 AAPL 2022-08-31 -0.0312 -0.0464
## 9 AAPL 2022-09-30 -0.121 -0.105
## 10 AAPL 2022-10-31 0.110 0.0390
## # ℹ 80 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 ActivePremium Alpha AnnualizedAlpha Beta `Beta-` `Beta+` Correlation
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 0.0235 0.0026 0.0319 1.03 0.836 1.54 0.852
## 2 GOOG 0.0325 0.0042 0.0511 0.891 1.13 0.397 0.755
## 3 NFLX 0.0073 0.0073 0.0914 1.76 2.50 1.81 0.773
## # ℹ 5 more variables: `Correlationp-value` <dbl>, InformationRatio <dbl>,
## # `R-squared` <dbl>, TrackingError <dbl>, TreynorRatio <dbl>
RaRb_capm <- RaRb %>%
tq_performance(Ra = Ra,
Rb = Rb,
performance_fun = VolatilitySkewness)
RaRb_capm
## # A tibble: 3 × 2
## # Groups: symbol [3]
## symbol VolatilitySkewness.1
## <chr> <dbl>
## 1 AAPL 1.85
## 2 GOOG 1.47
## 3 NFLX 2.10