christophergandrud / imfr

R package for interacting with the IMF RESTful JSON API
49 stars 5 forks source link

Fiscal Monitor Data now only provides the URL, not the full dataset? #35

Open iamgyang opened 3 years ago

iamgyang commented 3 years ago

Hi, I was recently using this package to pull the FM dataset.

The output is no longer a nested list with a dataframe, but rather a list with URLs in it. Could you let me know if I'm doing something wrong, or is this a known issue with the Fiscal Monitor data?

Thanks so much for your time and for creating this package!

library(imfr) # To access IMF data API
library(magrittr)
library(tidyr)
library(data.table)
library(countrycode)

imf_databases$description %>% grep("fiscal monitor", .,
                                   ignore.case = T,
                                   value = T)

imf_databases <- imf_ids() %>% as.data.table()
database_id_val <-
    imf_databases[description == "Fiscal Monitor (FM)"]$database_id
codelist <-
    imf_codelist(database_id = database_id_val) %>% as.data.table()
codelist_val <-
    codelist[description == "Geographical Areas"]$codelist
indicator_val <-
    codelist[description == "Indicator"]$codelist
AREA_codes <-
    imf_codes(codelist = codelist_val) %>% as.data.table()
INDICATOR_codes <-
    imf_codes(codelist = indicator_val) %>% as.data.table()
data(codelist)
country_set <- codelist
country_set <- country_set %>%
    select(country.name.en , iso2c, iso3c, imf, continent, region) %>%
    filter(!is.na(imf) &
               !is.na(iso2c))
iso2_vec <- country_set$iso2c

# Fiscal monitor data

fm_ls <- list()
start_num <- 1
end_index <- start_num + 49
while (end_index < length(iso2_vec)) {
    end_index <- min(start_num + 49, length(iso2_vec))
    fm_ls[as.character(start_num)] <-
        imf_data(
            database_id = "FM" ,
            country = iso2_vec[start_num:end_index],
            start = 2010,
            end = current_year(),
            return_raw = TRUE,
            indicator = "All_indicators"
        )
    start_num <- start_num + 50
}

fm_ls
cjyetman commented 2 years ago

The All_Indicators indicator in database_id FM doesn't seem to have any data behind it, even though the API returns it in the list of available indicators... but all the other indicators seem to work as expected...

Side note: If you specify country = "all" (or do not specify country, because "all" is the default) in the imf_data() function, imf_data() will automatically fill in all valid 2 letter iso2c codes it knows, i.e. imfr::all_iso2c$iso2c. If you specify country = "", it will not include any country/region specification in the API call, and hence return all values regardless of the country/region they are specified as, which is useful if you want to include region codes that are not valid iso2c codes, e.g. "1C_035".

library(imfr)
library(tidyverse)

imf_codes(codelist = "CL_INDICATOR_FM")$codes
#> [1] "GGCB_G01_PGDP_PT"   "GGCBP_G01_PGDP_PT"  "G_X_G01_GDP_PT"    
#> [4] "G_XWDG_G01_GDP_PT"  "GGXWDN_G01_GDP_PT"  "GGXCNL_G01_GDP_PT" 
#> [7] "GGXONLB_G01_GDP_PT" "GGR_G01_GDP_PT"     "All_Indicators"

INDICATOR_codes <- imf_codes(codelist = "CL_INDICATOR_FM")
codes <- setdiff(INDICATOR_codes$codes, "All_Indicators")

setNames(map(codes, ~ as_tibble(
  imf_data(
    database_id = "FM",
    indicator = .x,
    country = "",
    start = 2010
  )
)),
codes)

#> $GGCB_G01_PGDP_PT
#> # A tibble: 1,277 × 3
#>    iso2c  year  GGCB_G01_PGDP_PT
#>    <chr>  <chr>            <dbl>
#>  1 1C_035 2010            -0.444
#>  2 1C_035 2011            -0.320
#>  3 1C_035 2012            -0.313
#>  4 1C_035 2013            -0.372
#>  5 1C_035 2014            -0.398
#>  6 1C_035 2015            -0.604
#>  7 1C_035 2016            -0.660
#>  8 1C_035 2017            -0.642
#>  9 1C_035 2018            -0.685
#> 10 1C_035 2019            -0.827
#> # … with 1,267 more rows
#> 
#> $GGCBP_G01_PGDP_PT
#> # A tibble: 1,251 × 3
#>    iso2c  year  GGCBP_G01_PGDP_PT
#>    <chr>  <chr>             <dbl>
#>  1 1C_035 2010            -0.167 
#>  2 1C_035 2011            -0.0354
#>  3 1C_035 2012            -0.0515
#>  4 1C_035 2013            -0.108 
#>  5 1C_035 2014            -0.132 
#>  6 1C_035 2015            -0.295 
#>  7 1C_035 2016            -0.345 
#>  8 1C_035 2017            -0.305 
#>  9 1C_035 2018            -0.344 
#> 10 1C_035 2019            -0.488 
#> # … with 1,241 more rows
#> 
#> $G_X_G01_GDP_PT
#> # A tibble: 2,724 × 3
#>    iso2c  year  G_X_G01_GDP_PT
#>    <chr>  <chr>          <dbl>
#>  1 1C_035 2010            29.9
#>  2 1C_035 2011            29.9
#>  3 1C_035 2012            30.4
#>  4 1C_035 2013            30.6
#>  5 1C_035 2014            30.9
#>  6 1C_035 2015            31.5
#>  7 1C_035 2016            31.4
#>  8 1C_035 2017            30.8
#>  9 1C_035 2018            31.2
#> 10 1C_035 2019            31.7
#> # … with 2,714 more rows
#> 
#> $G_XWDG_G01_GDP_PT
#> # A tibble: 2,703 × 3
#>    iso2c  year  G_XWDG_G01_GDP_PT
#>    <chr>  <chr>             <dbl>
#>  1 1C_035 2010               38.1
#>  2 1C_035 2011               37.2
#>  3 1C_035 2012               37.1
#>  4 1C_035 2013               38.3
#>  5 1C_035 2014               40.4
#>  6 1C_035 2015               43.9
#>  7 1C_035 2016               48.4
#>  8 1C_035 2017               50.5
#>  9 1C_035 2018               52.4
#> 10 1C_035 2019               54.7
#> # … with 2,693 more rows
#> 
#> $GGXWDN_G01_GDP_PT
#> # A tibble: 1,335 × 3
#>    iso2c  year  GGXWDN_G01_GDP_PT
#>    <chr>  <chr>             <dbl>
#>  1 1C_035 2010               26.2
#>  2 1C_035 2011               24.4
#>  3 1C_035 2012               23.0
#>  4 1C_035 2013               23.2
#>  5 1C_035 2014               24.7
#>  6 1C_035 2015               29.1
#>  7 1C_035 2016               34.7
#>  8 1C_035 2017               35.8
#>  9 1C_035 2018               36.7
#> 10 1C_035 2019               38.4
#> # … with 1,325 more rows
#> 
#> $GGXCNL_G01_GDP_PT
#> # A tibble: 2,724 × 3
#>    iso2c  year  GGXCNL_G01_GDP_PT
#>    <chr>  <chr>             <dbl>
#>  1 1C_035 2010             -2.36 
#>  2 1C_035 2011             -0.962
#>  3 1C_035 2012             -0.952
#>  4 1C_035 2013             -1.58 
#>  5 1C_035 2014             -2.48 
#>  6 1C_035 2015             -4.30 
#>  7 1C_035 2016             -4.78 
#>  8 1C_035 2017             -4.08 
#>  9 1C_035 2018             -3.71 
#> 10 1C_035 2019             -4.68 
#> # … with 2,714 more rows
#> 
#> $GGXONLB_G01_GDP_PT
#> # A tibble: 2,630 × 3
#>    iso2c  year  GGXONLB_G01_GDP_PT
#>    <chr>  <chr>              <dbl>
#>  1 1C_035 2010             -0.630 
#>  2 1C_035 2011              0.749 
#>  3 1C_035 2012              0.602 
#>  4 1C_035 2013             -0.0212
#>  5 1C_035 2014             -0.866 
#>  6 1C_035 2015             -2.57  
#>  7 1C_035 2016             -3.07  
#>  8 1C_035 2017             -2.29  
#>  9 1C_035 2018             -1.94  
#> 10 1C_035 2019             -2.89  
#> # … with 2,620 more rows
#> 
#> $GGR_G01_GDP_PT
#> # A tibble: 2,733 × 3
#>    iso2c  year  GGR_G01_GDP_PT
#>    <chr>  <chr>          <dbl>
#>  1 1C_035 2010            27.5
#>  2 1C_035 2011            28.9
#>  3 1C_035 2012            29.4
#>  4 1C_035 2013            29.0
#>  5 1C_035 2014            28.4
#>  6 1C_035 2015            27.2
#>  7 1C_035 2016            26.6
#>  8 1C_035 2017            26.7
#>  9 1C_035 2018            27.5
#> 10 1C_035 2019            27.0
#> # … with 2,723 more rows

imf_data(
  database_id = "FM",
  indicator = "All_Indicators",
  country = "",
  start = 2010
)
#> Error: No data found.

Created on 2022-02-20 by the reprex package (v2.0.1)