Warning

This report is based on raw data from the UN Comtrade database, generated automatically based on the selected variables. They are primarily intended for expert use. Country data in the tables below might differ from the validated trade flow data that can be queried via the trade flow database user interface.

productcodeinreport <- 440799
# Generate report data for development purposes only
tfdata2 <- readdbproduct(productcodeinreport, "raw_flow_yearly")
# optional filter by year
#tfdata2 <- tfdata2 %>% filter(year %in% c(2010,2011))

# Then generate the completeness data frame below
# These data frames should be enough to run all chunks below
# Give a value to r to generate only one of the completeness plot 
# in the loop, in the plot chunk below

# Generate report from the development version
createcompletenessreport(440710, inputpath="inst/templates")
# This report is intended for one product only
tfdata2 <- tfdata %>% 
    filter(productcode == productcodeinreport) %>%
    filterworldeu28

completeness <- tfdata2 %>%
    select(reporter, classification, year, quantity, weight, tradevalue) %>%
    melt(id=c("reporter", "classification", "year"))  %>%
    mutate(value = !is.na(value)) %>%
    unique %>% 
    arrange(classification, reporter, year)
productcodeinreport <- as.character(productcodeinreport)

Product definitions

Product r productcodeinreport has the following definitions:

productname <- tfdata2 %>% 
    select(classification, productdescription) %>%
    arrange(classification) %>%
    unique

for(code in productname$classification){
    cat("\n\n__",code,":__ ", sep="")
    cat(productname$product[productname$classification == code])
}

Summary

Number of reporting countries by year and by classification type.

completeness %>% 
    select(reporter, year, classification) %>%
    unique() %>% # Keep only unique combinations of reporter, year, classification
    group_by(year, classification) %>% 
    dcast(year ~ classification, value.var="reporter", length) %>%
    kable()

List of countries by classification type in the most recent year available in the dataset (r max(completeness$year) for most countries).

reporterbyclassif <- completeness %>% 
    group_by(reporter) %>%
    filter(year == max(year)) %>%
    select(year, classification, reporter) %>%
    unique()
#     group_by(classification) %>%
#     do(head(.,2)) # works! how to use this do

for (code in unique(reporterbyclassif$classification)){
    cat("\n\n__",code,":__ ", sep="")
    cat(reporterbyclassif$reporter[reporterbyclassif$classification == code], 
        sep=", ")
}

Completeness by country and by classification type

# Prepare table to show partner countries
# remove world
tfdata3 <- tfdata2 %>% 
    filterworldeu28 %>%
    select(year, classification, flow, reporter, partner) %>%
    arrange(year, classification, flow)
years <- unique(tfdata3$year) # for the scales
for (r in unique(completeness$reporter)){
    cat("\n\n###", r, "\n")

    # plot completenes
    p <- ggplot(data=filter(completeness, reporter == r), 
                aes(x = year, y = variable, 
                    xend = year + 1, yend = variable, 
                    linetype = value)) +
        scale_colour_manual(values = c("TRUE" = "blue", "FALSE" = "red")) +
        scale_linetype_manual(values = c("TRUE" = 2, "FALSE" = 1)) +
        scale_x_continuous(breaks = years) +
        geom_segment(aes(color = value), size=3) + 
        geom_text(aes(y = "classification", label=classification)) +
        ggtitle(paste("Data completeness of", productcodeinreport,
                      "in", r))
    plot(p)

    ###################################################### #
    # Number of trade partners per classification and year #
    ###################################################### #
    # These try statements have been introduced because there is sometimes missing
    # data. It would be nice to deal with these errors and print something when they occur
    # Reported by the country
    npartnersbyr <- tfdata3 %>% 
        filter(reporter == r) %>%
        dcast(year + classification ~ flow, value.var="partner", length) 
    try(print(kable(npartnersbyr,
                    caption = paste("Number of trade partners reported by", r))))

    # Reported by partner countries
    try(npartnersbyothers <- tfdata3 %>% 
            filter(partner == r) %>%
            dcast(year + classification ~ flow, value.var="reporter", length))
    try(print(kable(npartnersbyothers,
                    caption = paste("Number of countries who report trading with",r))))
}


EuropeanForestInstitute/tradeflows documentation built on Oct. 7, 2019, 10:57 a.m.