使用 imfr 查询 IMF API - 错误号 result/does 不接受过滤器

Querying IMF API with imfr - error no result/does not accept filter

我目前正在尝试从国际货币基金组织的贸易统计方向下载一个特定的系列,用于计算国家间的贸易量。有一个 r-package imfr 在这方面做得非常出色。但是,在进行特定设置时,我 运行 遇到了问题。

此代码运行良好,为我提供了五个国家/地区我感兴趣的完整数据系列:

library(imfr)

# get the list of imf datasets 
imf_ids()

# I am interested in direction of trade "DOT", so check the list of codes that are in the datastructure
imf_codelist(database_id = "DOT")

# I want the export and import data between countries FOB so "TXG_FOB_USD" and "TMG_FOB_USD"
imf_codes("CL_INDICATOR_DOT")

# works nicely for exports:
data_list_exports <- imf_data(database_id = "DOT", indicator = c("TXG_FOB_USD"), 
                      country = c("US","JP","KR"), 
                      start = "1995",
                      return_raw = TRUE, 
                      freq = "A")

# however the same code does not work for imports
data_list_imports <- imf_data(database_id = "DOT", indicator = c("TMG_FOB_USD"), 
                                                  country = c("US","JP","KR"), 
                                                  start = "1995",
                                                  return_raw = TRUE, 
                                                  freq = "A")

这将 return 一个空系列,我不明白为什么。所以我想,也许美国不在数据集中(虽然不太可能)

library(httr)
library(jsonlite) 
# look at the API endpoint, that provides us with the data structure behind a dataset
result <- httr::GET("http://dataservices.imf.org/REST/SDMX_JSON.svc/DataStructure/DTO") %>% httr::content(as = "parsed") 

structure_url <- "http://dataservices.imf.org/REST/SDMX_JSON.svc/DataStructure/DOT"

raw_data <- jsonlite::fromJSON(structure_url )
test <- raw_data$Structure$CodeLists

但是,结果表明美国确实在数据中。那么,如果我只是不指定国家呢?结果最后确实下载了数据,但是因为速率限制只能下载前60个国家。当对 httr::GET 做同样的事情时,我直接达到了速率限制并返回错误。

data_list_imports <- imf_data(database_id = "DOT", indicator = c("TMG_FOB_USD"), 
                                                  start = "1995",
                                                  return_raw = TRUE, 
                                                  freq = "A")

有人知道我做错了什么吗?我真的很茫然,只是希望这是某个地方的错字...

谢谢,祝一切顺利!

这种回答问题:

cjyetman 在 github 给了我以下提示:

You can use the print_url = TRUE argument to see the actual API call.

With...

imf_data(database_id = "DOT", indicator = c("TMG_FOB_USD"), 
         country = c("US","JP","KR"), 
         start = "1995",
         return_raw = TRUE, 
         freq = "A", 
         print_url = TRUE) 

you get... http://dataservices.imf.org/REST/SDMX_JSON.svc/CompactData/DOT/.US+JP+KR.TMG_FOB_USD?startPeriod=1995&endPeriod=2021

which does not return any data.

But if you add "AU" as a country to that list, you do get data with... http://dataservices.imf.org/REST/SDMX_JSON.svc/CompactData/DOT/.AU+US+JP+KR.TMG_FOB_USD?startPeriod=1995&endPeriod=2021

So I guess either there is something wrong currently with their API, or they actually do not have data for specifically that indicator for those countries with that frequency, etc.

这确实有效,并且很明显 API 中确实存在“缺失数据”,或者我只是在查找 none 中的数据。由于最初的任务是查看贸易量,后来我发现,通常使用进口价值,并带有 CIF value and not FOB。 因此,API 调用的正确指示符如下:

library(imfr)
data_list_imports <- imf_data(database_id = "DOT", indicator = c("TMG_CIF_USD"), 
                                                  country = c("US","JP","KR"), 
                                                  start = "1995",
                                                  return_raw = TRUE, 
                                                  freq = "A")