有点像是新的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"

推荐答案

使用library(countrycode)可以运行您的大部分代码,这很有帮助.

我认为这个错误与您的xts对象和gdp_value对象的 struct 有关,它们是字符而不是数字.

一步一步,我相信这就是你的for街区正在发生的事情:

您可以这样开始(以第一个国家/地区为例,澳大利亚):

country_j <- countries_total[1]
country_temp=gdp_list[[country_j]] 

那么str(country_temp)就是:

'data.frame':   130 obs. of  4 variables:
 $ country  : chr  "Australia" "Australia" "Australia" "Australia" ...
 $ gdp_value: num  102357 104041 110785 102777 105668 ...
 $ date     : Date, format: "1991-04-01" "1991-07-01" "1991-10-01" "1992-01-01" ...
 $ growth   : num  0.0288 0.0163 0.0628 -0.075 0.0277 ...

您将看到,data.Frame包括日期、数字gdp_valuegrowth,以及字符值country(不同类型的混合).

当您转换为xts个对象时:

country_temp_xts=as.xts(country_temp)

你有str(country_temp_xts)个:

An xts object on 1991-04-01 / 2023-07-01 containing: 
  Data:    character [130, 3]
  Columns: country, gdp_value, growth
  Index:   Date [130] (TZ: "UTC")

请注意,这是Data作为字符,not是数字.这是因为这些对象是具有有序索引属性的矩阵.您不能混合类型,但可以使用data.Frame.如果有一列是字符,那么所有列都将变成字符(类似于矩阵).

一种 Select 是 Select gdp_value,这是您正在使用的唯一值,不包括其他字符列,然后转换为xts对象:

country_temp_xts <- as.xts(country_temp[, c("date", "gdp_value")])

另一种方法是,假设您有一个单字符列,您希望它是数字的,则使用:

storage.mode(<name_of_xts_object>) <- "numeric"

如果这解决了您的问题,请让我知道.

R相关问答推荐

为什么以及如何修复Mapview不显示所有点并且st_buffer合并一些区域R?

Tidyverse/Djirr为从嵌套列表中提取的列名赋值的解决方案

如何在ggplot 2线性图的每个方面显示每个组的误差条?

MCMC和零事件二元逻辑回归

根据R中的另一个日期从多列中 Select 最近的日期和相应的结果

如何在RMarkdown LaTex PDF输出中包含英语和阿拉伯语?

为什么横向页面会导致officeverse中的页码/节头/页脚出现问题?

derrr mutate case_when grepl不能在R中正确返回值

如何使下一个按钮只出现在Rshiny 的一段时间后?""

使用rest从header(h2,h3,table)提取分层信息

将重复项转换为NA

根据r中每行中的日期序列,使用列名序列创建新列

以任意顺序提取具有多个可能匹配项的组匹配项

如何在AER::ivreg中指定仪器?

整理ggmosaic图的标签

R将函数参数传递给ggploy

从矩阵创建系数图

如何在不使用SHINY的情况下将下拉滤镜列表添加到ggploy?

从字符串列中的向量中查找第一个匹配的单词

如何根据每个子框架中分类因子的唯一计数来过滤子框架列表?