有点像是新的R用户. 我正在try 使用哈密尔顿提出的过滤器将GDP序列分解为趋势和周期 我正在关注Never hpFilter程序包(https://github.com/JustinMShea/neverhpfilter?tab=readme-ov-file#readme)
这是我的设置
library(OECD)
library(tidyr)
library(neverhpfilter)
library(lubridate)
gdp_data=get_dataset("QNA")
gdp_data=gdp_data[gdp_data$SUBJECT=="B1_GS1",]
gdp_data=gdp_data[gdp_data$MEASURE=="CQR",]
gdp_data=gdp_data[gdp_data$TIME_FORMAT=="P3M",]
gdp_data=gdp_data[,c(2,5,10)]
gdp_data$ObsValue=as.numeric(gdp_data$ObsValue)
gdp_growth=gdp_data%>%
group_by(LOCATION)%>%
mutate(growth=log(ObsValue)-dplyr::lag(log(ObsValue)))
gdp_growth$Time=yq(gdp_growth$Time)
gdp_growth=gdp_growth[gdp_growth$Time>="1991-04-01",]
gdp_growth$LOCATION <- countrycode(gdp_growth$LOCATION, origin = "iso3c", destination = "country.name")
colnames(gdp_growth)=c("country", "gdp_value", "date", "growth")
gdp_list=split(gdp_growth, gdp_growth$country)
countries_total=names(gdp_list)
filtered_gdp=list()
for (country_j in countries_total) {
country_temp=gdp_list[[country_j]]
country_temp_xts=as.xts(country_temp)
gdp_value=country_temp_xts[,c(2)]
country_temp_hf=yth_filter(100*log(gdp_value), h=8, p=4, output=c("x", "trend", "cycle"))
filtered_gdp[[country_j]]=country_temp_hf
}
我收到一个错误,说
Error in log(gdp_value) : non-numeric argument to mathematical function
但在这些示例中,流程中使用的数据与我的数据属于同一类
类(COUNTRY_TEMP_XTS) [1]"XTS""ZOO"