HP Filter Using R -
i loading gdp data r fred , using hp filter find cycle component. struggling add date on x axis. tried converting data numeric or dataframe keep getting "cannot coerced" error message. doing wrong?
library(mfilter) library(quantmod) getsymbols('gdp',src='fred') plot(hpfilter(log(gdp),freq = 1600))
you can mimic output of plot.hpfilter
:
library(mfilter) library(quantmod) getsymbols('gdp',src='fred') hpf <- hpfilter(log(gdp),freq = 1600) out <- xts(cbind(hpf$x, hpf$trend, hpf$cycle), index(gdp)) colnames(out) <- c("x", "trend", "cycle") par(mfrow = c(2, 1), mar = c(3, 2, 2, 1)) plot(out[,"x"], t= "n", main = paste(hpf$title, "of", hpf$xname)) lines(out[,"x"], col = "steelblue") lines(out[,"trend"], col = "red") legend("topleft", legend = c(hpf$xname, "trend"), col = c("steelblue", "red"), lty = rep(1, 2), ncol = 2) plot(out[,"cycle"], t = "n", main = "cyclical component (deviations trend)") lines(out[,"cycle"], col = "steelblue")
Comments
Post a Comment