2016-01-21 25 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