Search code examples
rapihttreconomics

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


I am currently trying to download a particular series from the Direction Of Trade Statistics at the IMF for a calculation of trade volumes between countries. There is a r-package imfr that does a fantastic job at doing this. However, when going for a particular set, I run into problems.

This code, works just fine and gets me the full data-series I am interested in for the fiven countries:

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")

This will return an empty series and I did not understand why. So I thought, maybe the US is not in the dataset (although unlikely)

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

However, the result indicates that indeed the US is in the data. So what if I just don´t specify a country? The result finally does download the data, but only the 60 first countries because of rate limits. When doing the same with an httr::GET I directly hit the rate limit and get an error back.

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

Does anybody have an idea what I am doing wrong? I am really at a loss and just hope it is a typo somewhere...

Thanks and all the best!


Solution

  • This kind of answers the question:

    cjyetman over at github gave me the following hint:

    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.

    This does work indeed and makes apparent that either there is truly "missing data" in the API, or I am simply looking for data, where there is none. Since the original quest was to look at trade volumes, I have since found out, that the import value is usually used, with the CIF value and not FOB. Hence the correct indicator for the API call would have been the following:

    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")