2016-01-21 74 views
2

我正在将GDP数据加载到Fred的R中,并使用HP过滤器查找循环组件。我正在努力在X轴上添加日期。我试图将数据转换为数字或数据框,但我一直收到“不能被强制”的错误消息。我究竟做错了什么?使用R的HP过滤器

library(mFilter) 
library(quantmod) 
getSymbols('GDP',src='FRED') 
plot(hpfilter(log(GDP),freq = 1600)) 

enter image description here

+2

你尝试像'出< - hpfilter(日志(GDP),频率= 1600 ); cbind(GDP,out $ cycle)'? – 2016-01-21 03:48:46

+0

嘿帕斯卡尔。 Thx为您的建议。我能够得到它的工作。为了把两个时间序列放在同一个图表上(比如Y和Z)。图(x,y)和线(x,z)是最有效的吗?或者你使用另一种方法,如果x = cbind(stuff1,stuff2 ... stuffn),你可以在同一个图上一次性绘制所有的时间序列?再次感谢! – jessica

回答

3

你可以模仿的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") 

enter image description here