Computing Inter-City Matrix and Global Network Connectivities with R – A Brief Guide
Tom Storme and Ben Derudder
Introduction
This document contains R code that can be used to transform two-mode service value matrices (svm) into one-mode inter-city connectivity matrices (icc) using the interlocking network model (inm) (Taylor, 2001), and subsequently compute the global network connectivity (gnc) for every city. Details on the input matrices, the model, and its output can be found in Taylor and Derudder (2016).
R is a free software environment that provides a wide variety of statistical techniques. R needs to be downloaded, together with a development environment that supports direct code execution, and tools for plotting, history, debugging and workspace management. We used RStudio and generated this document with R Markdown. R Markdown is designed for easier reproducibility, because both computing code and narratives are in the same document. The code runs with the ‘dplyr’-package. The below example refers to the 2018 data as shown by the file names, but the code works with any other service value matrix in .csv-format as long as it is properly structured.
Basically, three steps are explained in this brief guide: (i) creating a numerical service value matrix; (ii) computing the inter-city connectivity matrix and (iii) generating a table with global network connectivities.
Step 1 – Creating a numerical service value matrix
First of all, GaWC’s two-mode service value matrix needs to be read and stored. Data are stored in a dataframe called ‘svm’ with the following code:
svm <- read.csv2("GaWC_svm_18.csv", header=TRUE, sep=",")
A dataframe can contain characters, strings and numbers, and is therefore not fit for matrix algebra. We extract the cities and firms from the dataframe and transform what is left into a numerical matrix ‘svm_mat’ (dimensions: 175 firms x 707 cities) as follows:
Cities <- svm[,"City"]
Firms <- colnames(svm)[-c(1:3)]
svm <- svm[,-c(1:3)]
row.names(svm) <- Cities
svm_mat <- data.matrix(svm, rownames.force=TRUE)
Step 2 – Constructing inter-city connectivity matrix
The icc matrix is basically the product of the svm matrix and its transpose (Derudder, 2020). This can easily be done with the tcrossprod-function. Keep in mind that the diagonals are zero.
res <- tcrossprod(svm_mat, y=NULL)
diag(res) <- 0
Step 3 – Calculating global network connectivity for every city
To calculate absolute and relative global network connectivity for every city, we use code that calculates the total icc-value for every city (which is the absolute GNC-value), and sorts the output. We add a GNC_rel value and a rank to the table.
GNC <- sort(rowSums(res), decreasing=TRUE)
Cities2 <- names(GNC)
df <- data.frame(Cities2, GNC)
names(df) <- c("City","GNC_abs")
df_mut <- mutate(df, GNC_rel=round(GNC/max(GNC), 3), rank = dense_rank(desc(df$GNC_abs)))