我正在将 GDP 数据从 Fred 加载到 R 中,并使用 HP 过滤器来查找周期分量。我正在努力在 X 轴上添加日期。我尝试将数据转换为数字或数据框,但我不断收到“无法强制”错误消息。我究竟做错了什么?
library(mFilter)
library(quantmod)
getSymbols('GDP',src='FRED')
plot(hpfilter(log(GDP),freq = 1600))
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以模仿以下输出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")
Run Code Online (Sandbox Code Playgroud)