library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── 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
Get Stock Prices and Convert to Returns
Ra <- c("AAPL", "GOOG", "NFLX") %>%
tq_get(get = "stock.prices" ,
from = "2010-01-01",
to = "2015-12-31") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Ra")
Ra
## # A tibble: 216 × 3
## # Groups: symbol [3]
## symbol date Ra
## <chr> <date> <dbl>
## 1 AAPL 2010-01-29 -0.103
## 2 AAPL 2010-02-26 0.0654
## 3 AAPL 2010-03-31 0.148
## 4 AAPL 2010-04-30 0.111
## 5 AAPL 2010-05-28 -0.0161
## 6 AAPL 2010-06-30 -0.0208
## 7 AAPL 2010-07-30 0.0227
## 8 AAPL 2010-08-31 -0.0550
## 9 AAPL 2010-09-30 0.167
## 10 AAPL 2010-10-29 0.0607
## # … with 206 more rows
Get Baseline and Convert to Returns
Rb <- c("XLK") %>%
tq_get(get = "stock.prices" ,
from = "2010-01-01",
to = "2015-12-31") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Rb")
Rb
## # A tibble: 72 × 3
## # Groups: symbol [1]
## symbol date Rb
## <chr> <date> <dbl>
## 1 XLK 2010-01-29 -0.0993
## 2 XLK 2010-02-26 0.0348
## 3 XLK 2010-03-31 0.0684
## 4 XLK 2010-04-30 0.0126
## 5 XLK 2010-05-28 -0.0748
## 6 XLK 2010-06-30 -0.0540
## 7 XLK 2010-07-30 0.0745
## 8 XLK 2010-08-31 -0.0561
## 9 XLK 2010-09-30 0.117
## 10 XLK 2010-10-29 0.0578
## # … with 62 more rows
Join the Two Tables
RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 216 × 5
## symbol.x date Ra symbol.y Rb
## <chr> <date> <dbl> <chr> <dbl>
## 1 AAPL 2010-01-29 -0.103 XLK -0.0993
## 2 AAPL 2010-02-26 0.0654 XLK 0.0348
## 3 AAPL 2010-03-31 0.148 XLK 0.0684
## 4 AAPL 2010-04-30 0.111 XLK 0.0126
## 5 AAPL 2010-05-28 -0.0161 XLK -0.0748
## 6 AAPL 2010-06-30 -0.0208 XLK -0.0540
## 7 AAPL 2010-07-30 0.0227 XLK 0.0745
## 8 AAPL 2010-08-31 -0.0550 XLK -0.0561
## 9 AAPL 2010-09-30 0.167 XLK 0.117
## 10 AAPL 2010-10-29 0.0607 XLK 0.0578
## # … with 206 more rows
Calculate CAPM
RaRb_capm <- RaRb %>%
tq_performance(Ra = Ra,
Rb = Rb,
performance_fun = table.CAPM)
RaRb_capm
## # A tibble: 1 × 12
## ActiveP…¹ Alpha Annua…² Beta `Beta-` `Beta+` Corre…³ Corre…⁴ Infor…⁵ R-squ…⁶
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 NA 0.0216 NA 0.880 0.150 0.737 0.285 0 NA 0.081
## # … with 2 more variables: TrackingError <dbl>, TreynorRatio <dbl>, and
## # abbreviated variable names ¹ActivePremium, ²AnnualizedAlpha, ³Correlation,
## # ⁴`Correlationp-value`, ⁵InformationRatio, ⁶`R-squared`