Introduction

International trade data can be analysed using a network perspective, where the network is a set of countries linked by weighted and directed trade ties. This network is referred to by various names, the International Trade Network (ITN), World Trade Web (WTW) and the World Trade Network (WTN) etc.

Here we provide a guide on how to implement network analysis to the international trade network in R, outlining how key network approaches and advanced models can be implemented.

This guide refers to a number of R packages, in particular the ITNr package (which is available on CRAN).

Data

International trade data can be downloaded from a number of sources. The most widely use are WITS (https://wits.worldbank.org/) and UN Comtrade (https://comtrade.un.org/).

Here we present a set of functions for how data downaloded/extracted from these sources can be cleaned, processed and transformed into a International Trade Network (ITN).

This function can clean and process it into an igraph object with a number of attributes attached.

The attributes the function attaches include
- Region
- Income Level
- GDP
- GDP per capita
- GDP growth
- FDI

The network is also weighted, where the weights refer to the proportion of world trade (of the product).

WITS

The WITSclean command is used to clean and process a CSV file downloaded from WITS into a International Trade Network.

The function requires a number of inputs:
- CSV file name
- Year (as the csv file may contain trade data for multiple years, this therefore allows you to create a ITN for a single year from this CSV file)
- Threshold TRUE/FALSE. TRUE - you want to apply a threshold with a cutoff. FALSE - instead of applying a thrshold you extract the backbone of the network, retaining only statistically signifcant edges.
- Cutoff - for the threshold TRUE this is the cutoff point, for FALSE this is the signficance level for the backbone.

#Example
#Year - 2016
#Threshold - TRUE
#Cutoff - 0.01 (Only ties that are at least 0.01% of world trade are retained)

ITN<-WITSclean("CSV file name.csv",2016,TRUE,0.01)

Reference for extracting the backbone of a network: Serrano, M. Á., Boguñá, M. and Vespignani, A. (2009) Extracting the multiscale backbone of complex weighted networks, Proceedings of the National Academy of Sciences, 106(16), pp. 6483–6488.

UN Comtrade

A similar set of commands are also available for data extracted from UN comtrade. However, this command is apllied to a dataframe, rather than the raw CSV file, so that it can be also used if the user made use of the uncomtradr package to download the data.

library(comtradr)

#Find all the codes associated with "motor vechicle"
auto_codes <- ct_commodity_lookup("motor vehicle",
                                    return_code = FALSE,
                                    return_char = TRUE)

##Download the data for trade in these codes (motor vechicles)
auto_trade<- ct_search(reporters = "All",
                  partners = "All",
                  trade_direction = "imports",
                  start_date = "2016-01-01",
                  end_date = "2016-12-31",
                  commod_codes = auto_codes)

##Alteratively you could just read the CSV file of trade data. Where 
##the CSV file was downloaded directly from UN Comtrade. 
##auto_trade<-read.csv("UNcomtrade file name.csv")

##These results involve multiple codes, so have repeated dyads, for instance, the
##dataframe would contain the trade between US and Canada for code 1, and would
##be present in the data for code 2 etc. 

##The following command aggregates this data, so there would be only one 
##US-Canada dyad for all codes, with the value of the dyad the sum of 
##value for the individual codes
auto_trade_clean<-aggregate(trade_value_usd~reporter_iso+partner_iso, 
                            auto_trade, sum)

##We don't apply this in the WITS command, as the search and download
##functionality already incorpoates this routine. 

library(ITNr)
ITN_auto<-Comtradrclean(auto_trade_clean,2016,
                        TRUE,0.01)

If using the uncomtradr package to download trade data, the concordance package is useful to complement it. The concordance package can be used to translate from different commodity coding systems, which is helpful when using uncomtradr as this only allows you to search using certain commodity coding systems.

Example Data

The ITNr package comes with example ITNs (igraph objects), which we use to demonstrate the available functions and models in this guide.

The network data is trade in electrical automotive goods, where this category is defined by Amighini & Gorgoni (2014):
Amighini, A. and Gorgoni, S. (2014) The International Reorganisation of Auto Production, The World Economy, 37(7), pp. 923–952.

These ITNs are igraph objects with a number of attributes (region, GDP, GDP per captia etc). The edge weights are proportion of global trade in the electrical automtoive goods category group. A threshold has beeen applied to the edge weights so only the most relevant edges and countries are included in th network. Therefore only edges that are at least 0.01% of global trade are included in the network.

The first object is a single network for 2016, the second is a list of networks for 2006 - 2016.

library(ITNr)
#Single ITN 2016 
data("ELEnet16")

#List of ITNs 2006 - 2016
data("ELEnetList")

Visualisation & Plots

Network Visualisation

The ITNr package contains two functions for plotting the International Trade Networks:
1. Plot set
2. Single Plot

The first produces a panel with four network plots. The purpose of this function for a quick inspection of the network structure, whilst the second plot function provides a better, higher quality plot.

Plot Set

In the plot set function, the four plots produced include:
- Network plot highlighting clusters using the fast greedy algorithm.
- Network plot with node colours for commnities detected using the spinglass algorithm.
- Network plot with nodes coloured by regional partition. - Network plot with nodes coloured by regional partition and node size based on outdegree centrality.

library(ITNr)
data("ELEnet16")

ITNplotset(ELEnet16)

Single Plot

The following command provides a single network plot, where it requires you to specify whether the nodes should be coloured by region and whether labels should be present.

If the nodes are coloured by region, the ties between countries in the same region (intra-regional) are the same colour as the node, and all other ties (inter-regional) are grey.

Node size is based on weighted out degree centrality - so reflects export performance. The edge size is based on the weights of the ties.

In the example below the ITN is coloured by region and the node labels are not present.

library(ITNr)
data("ELEnet16")

#No labels and node coloured by region
ITN_make_plot(ELEnet16,FALSE,TRUE)
## Loading required package: network
## network: Classes for Relational Data
## Version 1.13.0 created on 2015-08-31.
## copyright (c) 2005, Carter T. Butts, University of California-Irvine
##                     Mark S. Handcock, University of California -- Los Angeles
##                     David R. Hunter, Penn State University
##                     Martina Morris, University of Washington
##                     Skye Bender-deMoll, University of Washington
##  For citation information, type citation("network").
##  Type help("network-package") to get started.
## Loading required package: sna
## Loading required package: statnet.common
## 
## Attaching package: 'statnet.common'
## The following object is masked from 'package:base':
## 
##     order
## sna: Tools for Social Network Analysis
## Version 2.4 created on 2016-07-23.
## copyright (c) 2005, Carter T. Butts, University of California-Irvine
##  For citation information, type citation("sna").
##  Type help(package="sna") to get started.
## Loading required package: scales

Plots

Degree Distribtuion

We often want to examine the degree distribution of the ITN - that it the distribtuion of import and export ties. We present two plots of the degree distribtuion: probability and histogram.

This plot shows the probability of degree scores over the whole network.

library(ITNr)
data("ELEnet16")

ITNdegdist(ELEnet16)

This plot shows the count/number of degree scores over the whole network.

library(ITNr)
data("ELEnet16")

ITNhistdegdist(ELEnet16)

Imports Vs Exports Plot

The following function produces a plot showing imports (in degree) vs exports (out degree). This allows us to identify whether in the ITN, countries that export high levels also import high levels. The plot can be produced for either weighted or binary import and export ties.

library(ITNr)
data("ELEnet16")

#Plot is for binary imports and exports
ITNimvex(ELEnet16,FALSE)

Node Strength - Degree Correlation Plot

This function creates a correlation plot to examine the correlation between node strength and node degree.

library(ITNr)
data("ELEnet16")

ITNcorr(ELEnet16)

Circular Plot

In the ITNr package,there is a function to create a circular plot based on regional partitions. This allows us to examine the level of regionalisation in the network, along with a inspection of the level of trade between regions.

library(ITNr)
data("ELEnet16")

region_circle_plot(ELEnet16)

Blockmodels and Structural Equivalence

Blockmodelling and strucutral equivalence are important concepts in international trade. Blockmodels indicates the sets of countries that are structurally equivalent in the network.

Where strucutral equivalence allows us to explore to what extent two countries hold equivalent positions in the ITN.

This function gives the block membership for each country and a structural equivalence matrix, which indicates how equivalent pairs of countries are in the ITN.

library(ITNr)
blockse<-ITNblock_se(ELEnet16)

Blockmodel Plot

In addition to calculating the blockmodel membership, we introduce a function to plot the ITN with node coloured by block membership.
You can also set whether you want node labels present (TRUE) or absent (FALSE).

library(ITNr)
data("ELEnet16")

#No labels 
ITNblock_plot(ELEnet16,FALSE)

Brokerage Roles

It can be important to analyse brokerage roles in the ITN, where we consider a country’s role as a broker between various regional partitions. The following function conducts the Gould & Fernandez (1989) brokerage analysis.
Gould, R. V. and Fernandez, R. M. (1989) Structures of mediation: A formal approach to brokerage in transaction networks, Sociological methodology, 19(1989), pp. 89–126.

In this function - you specify the network (igraph object with region attribute) and the name of the file, where the analysis is saved.

library(ITNr)
GFbroker<-RegionalBrokerage(ELEnet16,"ITN Regional Brokerage")

Clustering

In the plots section we plotted networks with nodes coloured by clusters. In this function, we present the cluster membership for each country for a variety of cluster algorithms. We also present correlation between cluster membership and regional partitions. This allows you to investigate whether clustering in the ITN occurs at the regional level.

library(ITNr)

CLU<-ITNcluster(ELEnet16)

Network Properties

A number of network level properties can be calculated for the ITN using the ITNr package. These include:
-Size (number of nodes)
-Density
-Reciprocity
-Diameter
-Average path length
-Average node strength
-Average Degree
-Betweenness Centralisation
-Closeness Centralisation
-Eigenvector Centralisation
-Out Degree Centralisation
-In Degree Centralisation
-All Degree Centralisation
-Clustering coefficent (transitivity)
-Clustering Weighted
-Region Homophily
-Degree Assortativity

library(ITNr)
data("ELEnet16")

NetProp<-ITNproperties(ELEnet16)

head(NetProp)
##                       Network.Properties
## Size                             99.0000
## Density                           0.0747
## Reciprocity                       0.4248
## Diameter                          0.1726
## Average.path.length               2.3190
## Average.node.strength             0.9137

Centrality Measures

We present a function that calculates several centrality metrics for the ITN. These capture the importance of countries in the ITN, with a focus on their export or import performance for the majority of measures.

library(ITNr)
data("ELEnet16")

CENT<-ITNcentrality(ELEnet16)

head(CENT)
##     NAMES Weighted.Out.Degree Binary.Out.Degree Weighted.In.Degree
## BRA   BRA              0.2783                 5             1.1272
## CHN   CHN             22.9228                76             7.1466
## COL   COL              0.1680                 6             0.1781
## GBR   GBR              0.7155                19             3.3002
## HKG   HKG              0.4401                11             5.0964
## JPN   JPN              8.7375                32             3.3107
##     Binary.In.Degree Weighted.Degree.All Binary.Degree.All Betweenness
## BRA               13              0.2783                 5         147
## CHN               17             22.9228                76        3426
## COL                3              0.1680                 6          61
## GBR               26              0.7155                19         156
## HKG               12              0.4401                11          87
## JPN               13              8.7375                32          33
##     Closeness Eigenvector    Hub Authority
## BRA    0.0891      0.0542 0.0666    0.6107
## CHN    0.0699      0.8780 1.0000    0.6708
## COL    0.1186      0.0171 0.0705    0.2149
## GBR    0.0967      0.1116 0.4376    0.9139
## HKG    0.1676      0.2725 0.2448    0.5447
## JPN    0.0735      0.5156 0.7079    0.6096

Advanced Network Models

Smith (2016) notes that the application of advanced models from SNA to the international trade network present a number of avenues for future research, such as explaining global production pattern and the extent of regionalisation patterns global economy. Here we present how to implement a selection of advanced SNA models to the international trade network.

Reference: Smith, M. P. (2016) Corporate networks of international investment and trade, PhD Thesis, University of Greenwich, Available at:https://figshare.com/articles/CORPORATE_NETWORKS_OF_INTERNATIONAL_INVESTMENT_AND_TRADE/5401822

ERGM & Extensions

Exponential Random Graph models (ERGMs) are generative models that treat network topology (local network substructures) as the response variable. This make them ideal to inform on how and why economic ties occur.

There are a number of variants of the ERGM, and a selection of these can be implemented in R. In this section, we give the very basic exaples of how these can be applied to the ITN, using the network created using the ITNr package. There is a wide range of literature and onlien resoucres, which provide in depth guide on how to implement ERGMs.

When examining the model results, we suggest using the texreg, in particular the screenreg command, as these present the model results in a nice format.

Literature:
Hunter, D. R., Handcock, M. S., Butts, C. T., Goodreau, S. M. and Morris, M. (2008) ergm: A Package to Fit, Simulate and Diagnose Exponential-Family Models for Networks, Journal of statistical software, 24(3), p. nihpa54860.

Lusher, D., Koskinen, J. and Robins, G. (2013) Exponential random graph models for social networks: theory, methods, and applications.

Robins, G., Pattison, P., Kalish, Y. and Lusher, D. (2007) An introduction to exponential random graph (p*) models for social networks, Social Networks, 29(2), pp. 173–191.

Other helpful resources:
-http://michaellevy.name/blog/ERGM-tutorial/
-https://statnet.org/trac/raw-attachment/wiki/Sunbelt2016/ergm_tutorial.html

ERGM

Below we present the commands to apply an ERGM to the igraph object produced using the ITNr package. This is a basic example, please refer to the ltierature and guides to specify a model to test hypothesis and run a convergent model with a good fit.

##Load the data
library(ITNr)
data("ELEnet16")

##Convert igraph object to network object
library(intergraph)
ITN<-asNetwork(ELEnet16)

library(statnet)
##Basic example (not implemented)
ITN.ergm.fit<-ergm(ITN~edges+mutual
                   +gwidegree(0.7,fixed=TRUE)+gwodegree(0.7,fixed=FALSE)
                   +gwesp(0.7,fixed=TRUE)+gwdsp(0.7,fixed=TRUE)
                   +nodematch("region")
                   )

##Examine model results
library(texreg)
screenreg(ITN.ergm.fit)

##Goodness of fit
ITN.ergm.gof<-gof(ITN.ergm.fit)

##Plot goodness of fit
par(mfrow=c(2,2))
plot(ITN.ergm.gof)

TERGM & STERGM

A number of longitudinal extensions to the cross sectional ERGM exist and can be imeplemented in R. These include: -stergm -tergm -btergm

Useful resources for the implemtation of the models include:
Krivitsky, P. N. and Goodrea, S. M. (2013) STERGM-Separable Temporal ERGMs for modeling discrete relational dynamics with statnet, Available at: http://statnet.csde.washington.edu/NME2013/day3/STERGM.pdf.

Krivitsky, P. N. and Handcock, M. S. (2014) A separable model for dynamic networks, Journal of the Royal Statistical Society: Series B (Statistical Methodology), 76(1), pp. 29–46.

Leifeld, P., Cranmer, S. J. and Desmarais, B. A. (2017) Temporal exponential random graph models with btergm: estimation and bootstrap confidence intervals, Journal of Statistical Software.

tergm: https://statnet.org/trac/raw-attachment/wiki/Sunbelt2016/tergm_tutorial.html

stergm: https://statnet.github.io/nme/d2-tut1.html

GERGM

For more details on imeplementing the Generalised ERGM see: -Wilson, J. D., Denny, M. J., Bhamidi, S., Cranmer, S. J. and Desmarais, B. A. (2017) Stochastic weighted graphs: Flexible model specification and simulation, Social Networks, 49, pp. 37–47. -https://cran.r-project.org/web/packages/GERGM/vignettes/getting_started.html

##Load data
library(ITNr)
data("ELEnet16")

##Convert ITN to weighted adjacency matrix
library(igraph)
ITNmat<-get.adjacency(ELEnet16,attr="weight") %>% as.matrix()

##Get covariates dataframe
ITN_covariates<-get.data.frame(ELEnet16,what="vertices")

library(GERGM)

##Basic example (not implemented)
formula <- ITNmat ~ edges + mutual(alpha = 0.8)+nodematch("region")+ sender("logGDP") +  receiver("logGDP")  

GERGM.test<- gergm(formula,covariate_data = ITN_covariates,
                   number_of_networks_to_simulate = 40000,
                   thin = 1/100,
                   proposal_variance = 0.05,
                   MCMC_burnin = 10000,
                   seed = 456,
                   convergence_tolerance = 0.5)

Latent Space Models

Below is an example of how to implement a latent space model using latentnet. However, this is a very basic example, see Krivitsky & Handcock (2008) for more detail on implementation of this model.

Reference: Krivitsky, P. N. and Handcock, M. S. (2008) Fitting Latent Cluster Models for Networks with latentnet, Journal of Statistical Software, 24(i05), Available at: http://econpapers.repec.org/article/jssjstsof/24_3ai05.htm.

##Load the data
library(ITNr)
data("ELEnet16")

##Convert igraph object to network object
library(intergraph)
ITN<-asNetwork(ELEnet16)

library(latentnet)
ITN.latentnet<-ergmm(ITN~nodematch("region",diff=TRUE),tofit="mle", verbose=TRUE)

##Summary of the results
library(texreg)
screenreg(ITN.latentnet)

plot(ITN.latentnet,what="mle", pie=TRUE,labels=TRUE)

Preferential Attachment & Fitness Models

The following code provides an example of how the PAFit package can be applied to longitudinal network data - a set of ITNs, one for each year. TO examine whetehr the degree distribution of the networks follwoing a power law or a fitness model.

We use example data from the ITNr package and a command to convert this into a dynamic network object - these commands are described in more detail in the Longitudinal Network Analysis section.

For more details on how to implement the PAFit package and interpret the results see: Pham, T., Sheridan, P. and Shimodaira, H. (2015) PAFit: A Statistical Method for Measuring Preferential Attachment in Temporal Complex Networks, PLOS ONE, 10(9), p. e0137796.

Tutorial:https://cran.r-project.org/web/packages/PAFit/vignettes/Tutorial.pdf

##Load a list of ITNs - this is 11 ITNs, one for each year for 2006-2016
data("ELEnetList")

##Create a dynamic network object for the list of ITNs
ITNdyn<-ITNdynamic(ELEnetList)

##Create a PAFit object from the dynamic network object
ITN_PAFIT<-from_networkDynamic(ITNdyn)

##summary statistics of the temporal network
net_stats <- get_statistics(ITN_PAFIT)
summary(net_stats)

##Estimate the attachment function and node fitness simultaneously.
full_result <- joint_estimate(ITN_PAFIT, net_stats)

##Estimation summary
summary(full_result)

##Plot Attachment
plot(full_result, net_stats, plot = "A")

##Plot Fitness
plot(full_result, net_stats, plot = "f")

Network Autocorrelation Models

Network Autocorrelation models can be implmented using the lnam function in the sna package, or the tnam function in the xergm package. For additional details on how to implement the lnam function function see: https://rdrr.io/cran/sna/man/lnam.html

The tnam is the temporal version of the network autocorrelation model. For a full list of terms available to specify in the tnamsee:https://cran.r-project.org/web/packages/tnam/tnam.pdf and https://www.rdocumentation.org/packages/tnam/versions/1.6.5/topics/tnam

In the context of international trade, these models can be used Smith et al (2016)provide an empirical example, implement the temporal network autocorrelation model to assess how developing efficient trade ties with competitive countries impacts performance in the automotive sector.

Smith, Matthew, Gorgoni, Sara and Cronin, Bruce (2016) The Fragmentation of Production and the Competitiveness of Nations in the Automotive Sector–A Network Approach, Available at: http://economics.uark.edu/conference-files/The-Fragmentation-of-Production-and-the-Competitiveness-of-Nations-in-the-Automotive-Sector.pdf.

Longitudinal Network Analysis

Trade data is availble for decades, therefore longitudnal analysis is also avaiable. Here we present function on how to create a dynamic network object based on a set of (temporal) ITNs (e.g. one ITN for each year).

To create a dynamic network object, we need to specify a list of ITNs. These are usually an ITN for each year, where they are trade in the same product groups across time.

##Load a list of ITNs - this is 11 ITNs, one for each year for 2006-2016
data("ELEnetList")

##Create a dynamic network object for the list of ITNs
ITNdyn<-ITNdynamic(ELEnetList)
## Neither start or onsets specified, assuming start=0
## Onsets and termini not specified, assuming each network in network.list should have a discrete spell of length 1
## Argument base.net not specified, using first element of network.list instead
## Created net.obs.period to describe network
##  Network observation period info:
##   Number of observation spells: 1 
##   Maximal time range observed: 0 until 11 
##   Temporal mode: discrete 
##   Time unit: step 
##   Suggested time increment: 1
##We can also inspect this dynamic network object
head(as.data.frame(ITNdyn))
##   onset terminus tail head onset.censored terminus.censored duration
## 1     0        8   24    2          FALSE             FALSE        8
## 2     9       10   24    2          FALSE             FALSE        1
## 3     0        1   33    2          FALSE             FALSE        1
## 4     2        7   33    2          FALSE             FALSE        5
## 5     8        9   33    2          FALSE             FALSE        1
## 6    10       11   33    2          FALSE             FALSE        1
##   edge.id
## 1       1
## 2       1
## 3       2
## 4       2
## 5       2
## 6       2

Dynamic Network Analysis & Animation

There are many additional packages and functions to analyse this dynamic network object. These are listed at:http://statnet.csde.washington.edu/workshops/SUNBELT/current/ndtv/ndtv_workshop.html#introduction-to-this-workshop

Below is some example code to complement this package and analyse the dynamic network ITN object.

Film Strip Plots

Film strip plots can be used to create a static plot of a series ofdynamic networks. Below is the code to create a film strip plot from a dynamic network object.

##Create a dynamic network object for the list of ITNs
ITNdyn<-ITNdynamic(ELEnetList)

par(mar=c(1,1,1,1))

filmstrip(ITNdyn,displaylabels=FALSE,vertex.cex=2,
          edge.col="#CCCCCC55",vertex.col='red')

title("filmstrip example")

Animation

The dynamic network object can be used to create network animations using the networkDynamic and ndtv packages. There are other tools available to create network animations - that do not use a dynamic network object, see http://curleylab.psych.columbia.edu/netviz/netviz5.html#/10 for these alternative approaches.

To save the animations different file formats you need to have image magik installed, (https://www.imagemagick.org/script/index.php). When installing check the “install legacy programs” box. You also need to installation directory to path in Environment Variables.

##Create a dynamic network object for the list of ITNs
ITNdyn<-ITNdynamic(ELEnetList)

render.d3movie(ITNdyn,
               render.par=list(tween.frames=20),
               vertex.cex=0.8,
               vertex.col='red',
               edge.col="#CCCCCC55",
               output.mode = 'htmlWidget')

##Commands to save animation as GIF (to insert in presentations)
saveGIF(render.animation(ITNdyn,
                       render.par=list(tween.frames=20),
                       vertex.cex=0.8,vertex.col='red',
                       edge.col="#CCCCCC55"),movie.name = "Example.gif")

Temporal Metrics

These commands include timeline options, where we can view the dynamics of a network as a timeline by plotting the active spells of edges and vertices.

We can also make use of the tsna package to undertake some basic temporal social network analysis. This includes examining how many edges form at each time step for the dynamic network oject (tEdgeFormation) and producing a plot of this information. The tsna package allows us to compute a time series using network configruation terms (terms specified in the ergm package). In the example below the gtrans function to calculate and plot transitivity in the networks over time.

##Create a dynamic network object for the list of ITNs
ITNdyn<-ITNdynamic(ELEnetList)

##Timeline Plots
timeline(ITNdyn)
timeline(ITNdyn,plot.edge.spells=FALSE)

#tsna - Temporal Social Network Analysis
library(tsna)
tEdgeFormation(ITNdyn)
plot(tEdgeFormation(ITNdyn))

tSnaStats(ITNdyn,'gtrans')
plot(tSnaStats(ITNdyn,'gtrans'))