cynthiahqy/conformr-xmap-project

Example using crosswalk & data from other R packages?

Closed this issue · 3 comments

Consider calling crosswalks from countrycode or concordance and showing conversion to crossmap.

An example using comtradr, concordance and an early version of conformr -- though I recall there was an authentication requirement for comtrade data:

Demo dataset

  • comtrade (2012-2007, for two countries, import)
library(comtradr)

q <- list()

q$Germany <- comtradr::ct_search(reporters = "Australia", 
               partners = c("Germany"), 
               start_date = 2016,
               end_date = 2017,
               type = "goods",
               trade_direction = "imports",
               commod_codes = c("All"))

q$Indonesia <- comtradr::ct_search(reporters = "Australia", 
               partners = c("Indonesia"), 
               start_date = 2016,
               end_date = 2017,
               type = "goods",
               trade_direction = "imports",
               commod_codes = "All")
monthly <- ct_search(reporters = "Australia", 
               partners = c("Indonesia"), 
               start_date = 2016,
               end_date = 2016,
               freq = "monthly",
               type = "goods",
               trade_direction = "imports",
               commod_codes = "Total")
q$Indonesia %>% glimpse()

q$Indonesia %>%
  filter(aggregate_level == 4, trade_flow == "Import") %>%
  select(classification, period, reporter_iso, partner_iso, commodity_code, trade_value_usd)
concordance::hs5_hs4 %>% 
  select(ends_with("_4d")) %>%
  distinct() %>%
  conformr::make_cd_equal(., code_from = HS5_4d, code_to = HS4_4d) %>%
  filter(n_dest != 1)
library(dplyr)
# TODO: highlight code, value 2D transformation.

make code-dict (equal weights)

## from codes
countrycode::<table> %>%
  make_cd_equal(., code_A, code_B, direction = c("BA"))

## from code columns in data table
data %>%
  distinct(code_A, code_B) %>%
  make_cd_equal(., code_A, code_B, direction = c("AB")) %>%
  check_cd_coverage(., data, code_from, code_to) ## should definitely pass

verify existing code-dict

code_dict %>%
  check_cd_weights(., code_from, code_to, weight_col) %>%
  check_cd_coverage(., data, code_from, code_to)

apply code-dict to data

data_A %>%
    dplyr::group_by(case) %>%
    conformr::convert(data = .,
                    code_dict = code_dict_BA,
                    code_from = code_A,
                    code_to = code_B,
                    values_from = value_A,
                    values_to = "value_B",
                    weight_col = weight)

Implemented in #117 commits and mock object in #122