Skip to content

Instantly share code, notes, and snippets.

@christophsax
Created May 17, 2018 17:20
Show Gist options
  • Save christophsax/5b67dc9b94e5283590aef78a59c76e66 to your computer and use it in GitHub Desktop.
Save christophsax/5b67dc9b94e5283590aef78a59c76e66 to your computer and use it in GitHub Desktop.
Multivariate Time Series from Quantmod
library(tsbox)
suppressMessages(library(quantmod))
suppressMessages(library(ggplot2))

ts_fred <- function(..., class = "data.frame") {
  symb <- c(...)
  dta.env <- new.env()
  suppressMessages(getSymbols(symb, env = dta.env, src = "FRED"))
  z <- data.table::rbindlist(lapply(as.list(dta.env), ts_dt), idcol = "id")
  tsbox:::as_class(class)(z)
}

dta <- ts_fred(
  'IRLTLT01CHM156N', 
  'IRLTLT01JPM156N', 
  'IRLTLT01AUM156N', 
  'DGS10', 
  'T10YIE', 
  'T20YIEM', 
  'T30YIEM', 
  'DEXSZUS', 
  'DEXJPUS', 
  'DEXUSAL', 
  'RNCHBIS'
)

dta_m <- ts_frequency(dta, to = "month", aggregate = "mean", na.rm = TRUE)

p <- ts_ggplot(
  ts_pick(
    dta_m,
    `10y bond yields Switzerland` = 'IRLTLT01CHM156N', 
    `10y bond yields US` = 'DGS10', 
    `10y inflation expectations US` = 'T10YIE'
  ),
  title = "Bond yields and inflation expecations"
)
p + scale_color_tsbox() + theme_bw()

Created on 2018-05-17 by the reprex package (v0.2.0).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment