Introduction

Take a look at the clothes tag for the new shirt you just bought, at the bottom of that jug right by your table, or at the bottom of the Macbook in front of you. How many of them actually say made in America? Or do you see “made in China”, “made in Vietnam”, “made in Germany” , etc? The reason why those objects exist in front of you is all due to trade.

Trade has been a quintessential component of human interaction since the dawn of history. In its simplest fashion we had the barter system, and as society developed to the point it is today, we have a complex international trade system, operated by international organizations and multiple trade agreements. A long way from the barter system of old, international trade today is facilitated by large financial institutions, and in 2018 alone the total value of global exports was approximately 19.5 trillion USD (Worldwide export trade volume 1950-2018).

Countries enter into economic transactions with one another whereby they are able to produce and export goods in which they have the comparative advantage and import goods that other countries have more efficient production in. Through trading, countries are able to take advantage of their different specialties, which leads to accelerated growth for their individual economies. But if everyone is happy with trade, why are we in a trade war with China?

Our Analysis

Different countries will participate in world trade to a different extent, because some countries benefit more from trade than others. In the United States, there’s a widespread view that China is the major exporter, and the US the major importer in the global economy. But how much does China dominate the export market, and is the United States as big of a player in the import market as we think? In addition, are there natural divisions among countries in terms of trade volumes? What does the global trade network really look like? These are the questions we look to address.

These questions all relate in some way to the distribution of trade worldwide, and to understand more about this, we will proceed in three steps. To get a broad overview, we will first look at choropleths depicting share in total exports, and in total imports, by country. This gives us a preliminary understanding, not only of how countries relate to one another in terms of exports and imports, but also of how imports and exports relate within a particular country. We then move to the second step where we conduct a cluster analysis using k-means algorithm to explore whether natural groupings arise among countries based on trade volume. This will form the basis for our third step: network analysis, where we look at how countries in these clusters interact, and which appear to be the most important.

Data Background

The visualizations and analysis that follow are based on worldwide trade flow data taken from CEPII, which incorporates yearly bilateral trades down to the product level. This data is directly taken from reports submitted to the United Nations Statistical Division, and includes variables such as year, product category, exporter, importer, value of the trade (in $1,000), and lastly quantity (in metric tons) (Actualites du CEPII).

We restrict our focus particularly to the year 2017. Through grouping by exporter and importer and summing the value of trade, we are able to calculate trade volumes (in terms of value, for better comparison) between various different countries. We further use this to calculate a country’s share in total exports and imports.

World Exports and Imports

To get a broad overview of each country’s contribution to global trade, we look at two choropleths – the first colored by country share in total exports (as a percentage), and the second colored by country share in total imports (again, a percentage). The choropleths are interactive, and hovering over a country gives its export/import percentage, depending on which graph you’re looking at.

Contribution to World Exports by Country


The map above displays individual country contributions, as a percentage of total world export value. Countries in gray correspond to those for which we did not have data. As expected, China appears to dominate exports, accounting for around 14% of total world exports. The United States and Germany also contribute significantly, with each contributing around 8% of total world exports. However the rest of the countries seem to contribute very little individually, with most accounting for less than 2% of global exports each.

Contribution to World Imports by Country


Here we look at the contributions of each country to total world imports, as a percentage. Once again, countries in gray correspond to those for which data were unavailable. We see that Germany, the United States, and China appear to be the major players. Interestingly, though China is the leading exporter in 2017, it is the United States that dominates with respect to imports, accounting for around 13% of the global total. This might indicate an interesting relationship between these two countries, which we will explore in the clustering and network analyses.

Clustering

From our initial visualizations, it seems at least that the United States, Germany, and China, are distinct in terms of exports and imports. In order to uncover any other natural groups, as well as to see if these three countries do indeed form a group, we undertake a cluster analysis using k-means – a partitioning method.

Testing the Optimal Number of Clusters

Since the k-means solution requires us to specify a number of clusters, we look at the total within group sum of squares as a metric by which to select the optimal number of clusters. Below, we plot the total within group sum of squares vs. the optimal number of clusters, looking for an “elbow” in the plot.

From this graph of within group sum of squares against the number of clusters, we can see that there is a clear “elbow” at 3 clusters, which indicates that the optimal number of clusters for our k-means solution is 3. Beyond this, an increase in number of clusters doesn’t provide us much improvement in the solution, and so would be an unnecessary increase in complexity.

Visualizing the Cluster Solution

From the graph above, we can see the large disparity between clusters, particularly with respect to the countries in cluster 2. These three countries contributed significantly more to world exports and imports than the rest of the other countries as they, on average, contribute 10% to world trade individually.

In cluster 3, the countries, on average, contribute close to 2.5% of total world trade, much lower than in cluster 2 but also much higher than cluster 1 which contains most of the countries in the world but each contributed negligibly in comparison to world trade.

Data Table

Trade Networks

From our cluster analysis, we were able to identify 3 groups of countries, corresponding to low, medium, and high volumes of trade. Of these, we want to look at relationships within the medium and high trade volume countries in particular, since there are far too many low trade volume countries to be able to get a meaningful visualization.

The networks below display the relationships among high trade volume countries. Arrows indicate the direction of exports, and as such point from the exporting country to the importing country. Edges in this graph are colored corresponding to their country of origin or exporter country, and sized based on the volume of trade, with larger edges indicating more volume.

You can interact with the nodes by dragging and moving them, or using the navigation buttons. Further, you can highlight a particular country’s exports by clicking on its corresponding nodes. You can select multiple countries, by clicking and holding their corresponding nodes.

High Trade Volume Countries

In the network above, which displays the relationship between high trade volume countries, we can see in particular that there is a large volume of trade flow from China to the United States. While we might previously have expected this given that China is the largest exporter, and the United States the largest importer, we did not know until now whether that was due to a direct relationship between the two.

Medium Trade Volume Countries

This network looks slightly hard to read at first, but becomes clearer when you take advantage of the interactive elements. Highlighting the Netherlands, we can see that there seems to be a large volume of trade flow from the Netherlands to Belgium and Luxembourg, and the United Kingdom. Highlighting Belgium shows that they appear to export a lot to France as well, and this relationship is reciprocal. France also appears to have noticeable exports to the United Kingdom. So all of these countries appear to be closely related.

We can continue to extract insight from this network – Korea, for example, seems to have strong export relations with several regions including Japan, Hong Kong, and Vietnam, and there is a strong bilateral relationship between Singapore and Malaysia.

Behind the Scenes

Below is the code we used, where we wrote a function to take a data frame with exporting country, importing country, and total trade volume, and create the kind of network we just showed. We used the visNetwork package both to create the base network, as well as to add node and edge attributes, and interactive elements.

To create our function, all of our code went inside of:

make_network <- function(network) {

}

make_network is the name we gave to the function, and network, as above, is a placeholder for the only argument it takes. This is a data frame with the necessary columns – exporter_name, importer_name, and total_trade.

In the body of the function, first we had to set up the network data so we could use it in the visNetwork function. We identified unique export-import pairs, and the total trade between them. We used this to create our nodes (countries) and edges (trade flows), with the edges weighted by the value of trade flow.

# ensure that concatenating returns a factor
  c.factor <- function(..., recursive=TRUE) unlist(list(...), recursive=recursive)
  
  # identify all unique countries in the data (either exporting or importing)
  name_vec <- name_vec <- c.factor(unique(c.factor(unique(network$exporter_name), 
                     unique(network$importer_name))))
  
  # information required for visNetwork
  from <- network$exporter_name
  to <- network$importer_name
  trade <- network$total_trade
  
  # create edges                            # thickness determined by trade volume
  edges <- data.frame(from = from, to = to, value = trade)
  
  # create nodes
  nodes <- data.frame(id = name_vec, label = name_vec, group = name_vec)

We added the visualizations using visNetwork, which takes as arguments the nodes and edges. We used visNodes and visEdges to modify nodes and edges respectively, and visOptions and visInteraction to add the interactive elements.

  # network visualizations
  visNetwork(nodes, edges, height = "500px", width = "100%")  %>% 
    visNodes(shape = "dot") %>%  
             # arrows show exports
    visEdges(arrows = "to", arrowStrikethrough = F, smooth = T, physics = F) %>%
    # hide other nodes and incoming edges when a node is selected
    visOptions(highlightNearest = list(enabled = T, degree = 0)) %>%
    # add navigation buttons and allow multiple nodes to be selected at once
    visInteraction(navigationButtons = TRUE, multiselect = TRUE) %>%
  visLayout(randomSeed = 17)

Conclusion

Looking at the distribution of trade worldwide, we see that China, the United States, and Germany appear to dominate both with respect to exports and imports. China is by far responsible for the most exports, while the US is responsible for the most imports.

Examining the trade relationships within clusters, we see a strong flow of goods and services from China to the United States – by far the strongest relationship we identify overall. Seeing how the US import so much from China, you might be less surprised why so many items you found in your house have “made in China” slapped on it.

Even in the medium trade flow cluster, we are able to identify some strong relationships. Netherlands, Belgium-Luxembourg, France, and the UK seem to have high trade flow between them, as do Singapore and Malaysia.

Unfortunately, we were unable to visualize the last cluster due to its sheer size, which would have made a similar network visualization far too cluttered to be meaningful. As such, we are unable to provide an all-encompassing picture of international trade relations, which we would have liked to see.

Another thing that we realize is that no African countries were included in the cluster 2 and 3, meaning that the continent did not participate too much in world trade. However, it would be interesting if we can see within the continent what the trade network looks like.

Citations