knitr::opts_chunk$set(echo = FALSE)
knitr::opts_knit$set(root.dir="../..") # file paths are relative to the root of the project directory
library(eutradeflows)
library(dplyr)
library(tidyr)
library(ggplot2)

Introduction

Given a selection of comodities, the purpose of this document is to select the main partner countries.

Load data

Load data from rds files (Serialization Interface for R Objects). See the notebook bio_imports_update_cache.Rmd to update the cached data.

# Load metadata
partner_names <- readRDS("data_raw/env_impact/partner_names.rds")
reporter_names <- readRDS("data_raw/env_impact/reporter_names.rds")
product_names <- readRDS("data_raw/env_impact/product_names.rds")

product_names_HS2 <- product_names %>% 
    mutate(productcode2d = substr(productcode,1,2)) %>% 
    filter(productcode == productcode2d)

# Load data from cache
system.time(
    yearly <- readRDS("data_raw/env_impact/extra_eu_bio_imports.rds")
)
# 15 seconds for 63 countries
#   user  system elapsed 
# 14.999   0.382  15.389 

yearly <- yearly %>% 
    mutate(year = period %/% 100,
           productcode2d = substr(productcode, 1,2)) 

Aggregate

main_products <- yearly %>% 
    filter(year > max(year) - 5) %>% 
    # Calculate the sum for each CN8 product code 
    group_by(year, productcode2d, productcode) %>% 
    summarise(tradevalue = sum(tradevalue),
              weight = sum(weight),
              tradevalue_b = round(sum(tradevalue)/1e9,1)) %>% 
    # Calculate the percentage of trade within each CN2 product group and each year
    group_by(productcode2d, year) %>% 
    arrange(productcode2d, year, desc(tradevalue))  %>% 
    mutate(sumtradevalue = sum(tradevalue, na.rm = TRUE),
           tradevalue_pct = round(tradevalue / sumtradevalue,2),
           tradevalue_pct_cumul = cumsum(tradevalue_pct)) %>% 
    # Keep only products representing 60% of the total trade value
    filter(tradevalue_pct_cumul < 0.6) %>% 
    left_join(product_names, by="productcode") %>%
    mutate(productdescription = substr(productdescription, 1, 20)) 
main_partners <- yearly %>% 
    filter(year > max(year) -5 & 
               # Keep only main products
               productcode %in% main_products$productcode) %>% 
    # Calculate the sum for each CN8 product code 
    group_by(year, productcode, partnercode, partneriso) %>% 
    summarise(tradevalue = sum(tradevalue),
              weight = sum(weight),
              tradevalue_b = round(sum(tradevalue)/1e9,1)) %>% 
    # Calculate the percentage of trade between partners within each CN8 product group and each year
    group_by(productcode, year) %>% 
    arrange(productcode, year, desc(tradevalue))  %>% 
    mutate(sumtradevalue = sum(tradevalue, na.rm = TRUE),
           tradevalue_pct = round(tradevalue / sumtradevalue,2),
           tradevalue_pct_cumul = cumsum(tradevalue_pct)) %>% 
    # Keep only partners representing 60% of the total trade value
    filter(tradevalue_pct_cumul < 0.8) %>% 
    left_join(product_names, by="productcode") %>%
    left_join(partner_names, by="partnercode") %>% 
    mutate(productdescription = substr(productdescription, 1, 20)) 

Main partners at 8 digit level

List of products which represent around 80% of total trade under the given CN2 chapter (group).

i = 1
for (this_product in unique(main_products$productcode)){
    i = i+1
    # if(i==5) break # Short loop for development
    # Sub section title 
    product_description <- product_names$productdescription[product_names$productcode==this_product]
    cat(sprintf('\n\n## %s %s \n\n',this_product, substr(product_description,1,57)))
    cat(product_description, '\n\n')
    # Filter only products under this CN2 code
    main_partners %>% 
        filter(productcode == this_product) %>% 
        ungroup() %>% 
        mutate(year = ifelse(year==lag(year, default=0),"",year)) %>% 
        select(year, partner, tradevalue, tradevalue_pct, weight) %>% 
        knitr::kable() %>% 
        print()
}
# ENcoding issue
# upon compiling this notebook I receive following error:
# ! Package inputenc Error: Unicode character ’ (U+0092)
# (inputenc)                not set up for use with LaTeX.
# 
# Try other LaTeX engines instead (e.g., xelatex) if you are using pdflatex. For R Markdown users, see https://bookdown.org/yihui/rmarkdown/pdf-document.html
# Error: Failed to compile bio_imports_by_country_CN8.tex. See https://yihui.name/tinytex/r/#debugging for debugging tips. See bio_imports_by_country_CN8.log for more info.
# Execution halted
partner_names$partner[!validUTF8(partner_names$partner)]
product_names$productdescription[!validUTF8(product_names$productdescription)]
# The issue is here according to the last line of the latex log
# ...                                              
#                                                   
# l.3034 & Laos (People’
#                        s Democratic Republic) & 28922963 & 0.05 & 
partner_names$partner[grepl("Lao", partner_names$partner)] 


# Other latex error:
# l.8716 COMPOUNDS, IN THE FORM OF A LIQUID AT 20�
#                                                   C, N.E.S. 
# 

product_names$productdescription[grepl("IN THE FORM OF A LIQUID AT",product_names$productdescription)]
#validUTF8(product_names$productdescription[grepl("IN THE FORM OF A LIQUID AT",product_names$productdescription)])
iconv(product_names$productdescription[grepl("IN THE FORM OF A LIQUID AT",product_names$productdescription)], from='utf8')


# Detect file encoding of the CN.txt file
# /tmp/comext/COMEXT_METADATA/CLASSIFICATIONS_AND_RELATIONS/ENGLISH
# Command line utility
# $ uchardet /tmp/comext/COMEXT_METADATA/CLASSIFICATIONS_AND_RELATIONS/ENGLISH/CN.txt 
# UTF-8
# For lack of a better method, 
# I did a replace all for the question mark characters in 
# product_names$productdescription[grepl("�",product_names$productdescription)]
# [1] "WINES PRODUCED IN VALL�E DU RH�NE, IN CONTAINERS HOLDING <= 2 L AND OF AN ACTUAL ALCOHOLIC STRENGTH OF <= 15% VOL, WITH PDO (OTHER THAN SPARKLING WINE, SEMI-SPARKLING WINE AND WHITE WINE)"                                                                        
# [2] "WINES PRODUCED IN VALL�E DU RH�NE, IN CONTAINERS HOLDING > 2 L AND OF AN ACTUAL ALCOHOLIC STRENGTH OF <= 15% VOL, WITH PDO (OTHER THAN SPARKLING WINE, SEMI-SPARKLING WINE AND WHITE WINE)"                                                                         
# [3] "WINES PRODUCED IN EU, IN CONTAINERS HOLDING > 2 L AND OF AN ACTUAL ALCOHOLIC STRENGTH OF <= 15% VOL, WITH PDO (OTHER THAN BORDEAUX, BOURGOGNE, BEAUJOLAIS, VALL�E DU RH�NE, LANGUEDOC-ROUSSILLON, VAL DE LOIRE, SPARKLING WINE, SEMI-SPARKLING WINE AND WHITE WINE)"
# [4] "CHEMICAL PRODUCTS OR PREPARATIONS, PREDOMINANTLY COMPOSED OF ORGANIC COMPOUNDS, IN THE FORM OF A LIQUID AT 20�C, N.E.S."                                                                                                                                            
# [5] "CHEMICAL PRODUCTS OR PREPARATIONS, PREDOMINANTLY COMPOSED OF ORGANIC COMPOUNDS, N.E.S. (EXCL. IN THE FORM OF A LIQUID AT 20�C)"  


stix-global/eutradeflows documentation built on Nov. 13, 2020, 9:23 p.m.