library(povcalnetR)
library(dplyr)
library(purrr)

Specify parameters at the country level

This function can be very handy is you maintain a list of countries and parameters in a table like the one below. Note: This function only works with survey years. There is no fill_gaps option available

# Read values from a table
data("sample_input")
sample_input
#> # A tibble: 5 x 5
#>   country poverty_line  year   ppp coverage_type
#>   <chr>          <dbl> <dbl> <dbl> <chr>        
#> 1 ALB              3.2  1996    50 national     
#> 2 AGO              1.9  2008    50 national     
#> 3 KAZ              3.2  1993    50 national     
#> 4 BOL              3.2  1992    50 urban        
#> 5 ZAF              5.5  1993    50 national
# Use table values to send a request to the API
# Only works for survey years
povcalnet_cl(country = sample_input$country,
             povline = sample_input$poverty_line,
             year = sample_input$year,
             ppp = sample_input$ppp)
#> # A tibble: 5 x 31
#>   countrycode countryname regioncode coveragetype  year datayear datatype
#>   <chr>       <chr>       <chr>      <chr>        <dbl>    <dbl> <chr>   
#> 1 ALB         Albania     ECA        N             1996    1996  consump~
#> 2 AGO         Angola      SSA        N             2008    2008. consump~
#> 3 KAZ         Kazakhstan  ECA        N             1993    1993  income  
#> 4 BOL         Bolivia     LAC        N             1992    1992  income  
#> 5 ZAF         South Afri~ SSA        N             1993    1993  consump~
#> # ... with 24 more variables: isinterpolated <dbl>, usemicrodata <dbl>,
#> #   ppp <dbl>, povertyline <dbl>, mean <dbl>, headcount <dbl>,
#> #   povertygap <dbl>, povertygapsq <dbl>, watts <dbl>, gini <dbl>,
#> #   median <dbl>, mld <dbl>, polarization <dbl>, population <dbl>,
#> #   decile1 <dbl>, decile2 <dbl>, decile3 <dbl>, decile4 <dbl>, decile5 <dbl>,
#> #   decile6 <dbl>, decile7 <dbl>, decile8 <dbl>, decile9 <dbl>, decile10 <dbl>

Helper functions

Information table

povcalnet_info() %>%
glimpse()
#> Rows: 180
#> Columns: 9
#> $ country_code   <chr> "ALB", "DZA", "AGO", "ARG", "ARM", "AUS", "AUT", "AZ...
#> $ country_name   <chr> "Albania", "Algeria", "Angola", "Argentina", "Armeni...
#> $ wb_region      <chr> "ECA", "MNA", "SSA", "LAC", "ECA", "OHI", "OHI", "EC...
#> $ un_region      <chr> "EUS", "AFN", "AFM", "LAS", "ASW", "OCA", "EUW", "AS...
#> $ income_region  <chr> "UMC", "UMC", "LMC", "HIC", "UMC", "HIC", "HIC", "UM...
#> $ coverage_level <chr> "national", "national", "national", "urban", "nation...
#> $ coverage_type  <chr> "national", "national", "national", "urban", "nation...
#> $ coverage_code  <chr> "3", "3", "3", "2", "3", "3", "3", "3", "3", "3", "3...
#> $ year           <list> [<1996, 2002, 2005, 2008, 2012, 2014, 2015, 2016, 2...

Get all available countries from a World Bank region

get_countries(c("ECA"))
#>  [1] "ALB" "ARM" "AZE" "BLR" "BIH" "BGR" "HRV" "CZE" "EST" "GEO" "HUN" "KAZ"
#> [13] "XKX" "KGZ" "LVA" "LTU" "MKD" "MDA" "MNE" "POL" "ROU" "RUS" "SRB" "SVK"
#> [25] "SVN" "TJK" "TUR" "TKM" "UKR" "UZB"

Get all available countries from a World Bank income classification

get_countries(c("LIC"))
#>  [1] "BEN" "BFA" "BDI" "CAF" "TCD" "COM" "COD" "ETH" "GMB" "GIN" "GNB" "HTI"
#> [13] "LBR" "MDG" "MWI" "MLI" "MOZ" "NPL" "NER" "RWA" "SEN" "SLE" "SSD" "SYR"
#> [25] "TJK" "TZA" "TGO" "UGA" "YEM" "ZWE"

Combining functions

Pass different poverty lines to countries from different income groups

income_groups <- c("LIC", "LMC", "UMC")
poverty_lines <- c(1.9, 3.2, 5.5)
map2_df(income_groups, poverty_lines, ~povcalnet(country = get_countries(.x),
                                                 povline = .y,
                                                 year = 2015,
                                                 aggregate = TRUE)
        )
#> # A tibble: 3 x 9
#>   regiontitle regioncode  year povertyline  mean headcount povertygap
#>   <chr>       <chr>      <dbl>       <dbl> <dbl>     <dbl>      <dbl>
#> 1 XX          XX          2015         1.9  88.3     0.448     0.171 
#> 2 XX          XX          2015         3.2 150.      0.439     0.139 
#> 3 XX          XX          2015         5.5 400.      0.245     0.0772
#> # ... with 2 more variables: povertygapsq <dbl>, population <dbl>