Reputation: 1355
I am loading GDP data into R from Fred and using a HP filter to find the cycle component. I am struggling to add the date on the X axis. I tried converting the data into a numeric or a dataframe but I keep getting a "cannot be coerced" error message. What am I doing wrong?
library(mFilter)
library(quantmod)
getSymbols('GDP',src='FRED')
plot(hpfilter(log(GDP),freq = 1600))
Upvotes: 2
Views: 15825
Reputation:
You can mimic the 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 from trend)")
lines(out[,"cycle"], col = "steelblue")
Upvotes: 5