#load Packages
library(tidyquant)
library(tidyverse)
1 Get Stock prices and convert to returns
Ra <- c("WMT", "PG", "JNJ") %>%
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: 27 × 3
## # Groups: symbol [3]
## symbol date Ra
## <chr> <date> <dbl>
## 1 WMT 2022-01-31 -0.0335
## 2 WMT 2022-02-28 -0.0333
## 3 WMT 2022-03-31 0.106
## 4 WMT 2022-04-29 0.0273
## 5 WMT 2022-05-31 -0.156
## 6 WMT 2022-06-30 -0.0548
## 7 WMT 2022-07-29 0.0861
## 8 WMT 2022-08-31 0.00816
## 9 WMT 2022-09-21 0.0167
## 10 PG 2022-01-31 -0.00972
## # … with 17 more rows
2 Get baseline and convert to returns
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: 9 × 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-21 -0.0504
3 Join the two data 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 WMT 2022-01-31 -0.0335 -0.101
## 2 WMT 2022-02-28 -0.0333 -0.0343
## 3 WMT 2022-03-31 0.106 0.0341
## 4 WMT 2022-04-29 0.0273 -0.133
## 5 WMT 2022-05-31 -0.156 -0.0205
## 6 WMT 2022-06-30 -0.0548 -0.0871
## 7 WMT 2022-07-29 0.0861 0.123
## 8 WMT 2022-08-31 0.00816 -0.0464
## 9 WMT 2022-09-21 0.0167 -0.0504
## 10 PG 2022-01-31 -0.00972 -0.101
## # … with 17 more rows
4 Calculate CAPM
Which Stock has positivly skewed distribution of returns
Proctor and gamble and Johnson and Johnson have positivly skewed
distribution of returns