I’ll use this demo dataset to illustrate the essential usage of the functions within this package.
## # A tibble: 44,435 x 3
## # Groups: reporter_iso [158]
## country product value
## <chr> <chr> <dbl>
## 1 afg 0106 621
## 2 afg 0405 3380
## 3 afg 0407 60807
## 4 afg 0501 1466840
## 5 afg 0504 1466840
## 6 afg 0703 85258
## 7 afg 0712 9057
## 8 afg 0714 164694
## 9 afg 0801 8482381
## 10 afg 0802 8437060
## # … with 44,425 more rows
You can obtain RCA with ec_rca()
.
rca <- ec_rca(
data = ec_trade_1962,
c = "country",
p = "product",
v = "value"
)
# 5x5 preview
rca[1:5,1:3]
## 5 x 3 sparse Matrix of class "dgCMatrix"
## 0101 0102 0103
## afg . . .
## ago . . .
## alb . . .
## and . . .
## ant . . .
And also you can obtain it in tabular version.
rca_tbl <- ec_rca(
data = ec_trade_1962,
c = "country",
p = "product",
v = "value",
tbl = T
)
rca_tbl
## # A tibble: 44,435 x 3
## country product value
## <chr> <chr> <dbl>
## 1 afg 0106 0
## 2 afg 0405 0
## 3 afg 0407 0
## 4 afg 0501 1
## 5 afg 0504 1
## 6 afg 0703 0
## 7 afg 0712 0
## 8 afg 0714 1
## 9 afg 0801 1
## 10 afg 0802 1
## # … with 44,425 more rows
Another possibility, not used to build networks from bipartite relations, is to obtain RCA as a matrix or tibble without discretization.
rca_decimal <- ec_rca(
data = ec_trade_1962,
c = "country",
p = "product",
v = "value",
discrete = F
)
# 5x3 preview
rca_decimal[1:5,1:3]
## 5 x 3 sparse Matrix of class "dgCMatrix"
## 0101 0102 0103
## afg . . .
## ago . 0.01826164 .
## alb . . .
## and . . .
## ant . . .
rca_decimal_tbl <- ec_rca(
data = ec_trade_1962,
c = "country",
p = "product",
v = "value",
tbl = T,
discrete = F
)
rca_decimal_tbl
## # A tibble: 44,435 x 3
## country product value
## <chr> <chr> <dbl>
## 1 afg 0106 0.0412
## 2 afg 0405 0.00944
## 3 afg 0407 0.275
## 4 afg 0501 14.4
## 5 afg 0504 20.7
## 6 afg 0703 0.271
## 7 afg 0712 0.220
## 8 afg 0714 3.18
## 9 afg 0801 34.4
## 10 afg 0802 48.0
## # … with 44,425 more rows
You can compute both Economic Complexity Index (ECI) and Product Complexity Index (PCI) by using ec_complexity_measures()
. The calculations methods are reflections, eigenvalues and fitness (default). See (Hausmann et al. 2014) and (Mariani et al. 2015) for the methodological details.
cm_reflections <- ec_complexity_measures(
rca = rca,
method = "reflections",
tbl = T
)
cm_reflections$complexity_index_c
## # A tibble: 158 x 2
## country value
## <chr> <dbl>
## 1 deu 2.23
## 2 spm 2.20
## 3 fro 2.04
## 4 che 2.03
## 5 swe 1.99
## 6 jpn 1.91
## 7 aut 1.86
## 8 fra 1.86
## 9 ddr 1.84
## 10 gbr 1.83
## # … with 148 more rows
## # A tibble: 991 x 2
## product value
## <chr> <dbl>
## 1 7111 1.73
## 2 9112 1.57
## 3 8804 1.43
## 4 8208 1.40
## 5 9108 1.37
## 6 7107 1.34
## 7 4906 1.33
## 8 8468 1.32
## 9 3813 1.32
## 10 7221 1.31
## # … with 981 more rows
cm_eigenvalues <- ec_complexity_measures(
rca = rca,
method = "eigenvalues",
tbl = T
)
cm_eigenvalues$complexity_index_c
## # A tibble: 158 x 2
## country value
## <chr> <dbl>
## 1 deu 2.23
## 2 spm 2.20
## 3 fro 2.04
## 4 che 2.03
## 5 swe 1.99
## 6 jpn 1.91
## 7 aut 1.86
## 8 fra 1.86
## 9 ddr 1.84
## 10 gbr 1.83
## # … with 148 more rows
## # A tibble: 991 x 2
## product value
## <chr> <dbl>
## 1 7111 1.73
## 2 9112 1.57
## 3 8804 1.43
## 4 8208 1.40
## 5 9108 1.37
## 6 7107 1.34
## 7 4906 1.33
## 8 8468 1.32
## 9 3813 1.32
## 10 7221 1.31
## # … with 981 more rows
cm_fitness <- ec_complexity_measures(
rca = rca,
method = "fitness",
tbl = T
)
cm_fitness$complexity_index_c
## # A tibble: 158 x 2
## country value
## <chr> <dbl>
## 1 deu 28.8
## 2 fra 21.4
## 3 usa 14.1
## 4 nld 11.5
## 5 che 10.5
## 6 gbr 9.81
## 7 ita 9.61
## 8 swe 7.35
## 9 aut 7.34
## 10 jpn 7.27
## # … with 148 more rows
## # A tibble: 991 x 2
## product value
## <chr> <dbl>
## 1 7111 75.1
## 2 2850 55.9
## 3 8804 32.1
## 4 9307 24.7
## 5 9112 20.2
## 6 3505 19.6
## 7 3813 14.3
## 8 7507 12.6
## 9 2927 12.1
## 10 2810 11.8
## # … with 981 more rows
Proximity matrices are used to create both country-country and product-product networks. Using proximity_matrices()
is straightforward.
pro <- ec_proximity(
rca = rca,
d = cm_fitness$diversity,
u = cm_fitness$ubiquity,
tbl = T
)
pro$proximity_c
## # A tibble: 10,432 x 3
## from to value
## <chr> <chr> <dbl>
## 1 ago afg 0.0962
## 2 alb afg 0.219
## 3 arg afg 0.120
## 4 aus afg 0.111
## 5 aut afg 0.00690
## 6 bdi afg 0.0714
## 7 bel afg 0.00938
## 8 ben afg 0.107
## 9 bfa afg 0.0816
## 10 bgr afg 0.0945
## # … with 10,422 more rows
## # A tibble: 417,822 x 3
## from to value
## <chr> <chr> <dbl>
## 1 0102 0101 0.346
## 2 0103 0101 0.263
## 3 0104 0101 0.421
## 4 0106 0101 0.2
## 5 0201 0101 0.381
## 6 0203 0101 0.474
## 7 0204 0101 0.158
## 8 0205 0101 0.368
## 9 0206 0101 0.316
## 10 0207 0101 0.368
## # … with 417,812 more rows
The proximity_networks()
function is designed to use igraph
for the internal computations and also to pass proximity-based networks to igraph
, ggraph
or export to Cytoscape by saving the output as csv/tsv.
To create some reduced networks I’ll use a high proximity cutoff, so that the networks are limited to the spanning trees for clarity and fast rendering.
net <- ec_networks(
pc = pro$proximity_c,
pp = pro$proximity_p,
cutoff_c = 1,
cutoff_p = 1,
tbl = T
)
net$network_c
## # A tibble: 157 x 3
## from to value
## <chr> <chr> <dbl>
## 1 irn alb 0.375
## 2 irn afg 0.5
## 3 irn tur 0.348
## 4 npl afg 0.214
## 5 syr afg 0.357
## 6 ago ben 0.339
## 7 ago per 0.358
## 8 ago sen 0.308
## 9 sur ant 0.304
## 10 sur and 0.130
## # … with 147 more rows
## # A tibble: 990 x 3
## from to value
## <chr> <chr> <dbl>
## 1 9614 4202 0.529
## 2 9614 0101 0.579
## 3 0102 0201 0.5
## 4 0102 0104 0.423
## 5 0103 0505 0.647
## 6 1102 1005 0.5
## 7 1102 0105 0.417
## 8 4803 4707 0.615
## 9 4803 0105 0.417
## 10 0106 1302 0.514
## # … with 980 more rows
Just two basic examples with ggraph
.
library(igraph)
library(ggraph)
library(magrittr)
set.seed(200100)
net$network_c %>%
graph_from_data_frame(directed = F) %>%
ggraph(layout = "kk") +
geom_edge_link(aes(edge_alpha = value, edge_width = value),
edge_colour = "#a8a8a8") +
geom_node_point(color = "darkslategray4", size = 8) +
geom_node_text(aes(label = name), vjust = 2.2) +
ggtitle("The Country Space") +
theme_void()
net$network_p %>%
graph_from_data_frame(directed = F) %>%
ggraph(layout = "kk") +
geom_edge_link(aes(edge_alpha = value, edge_width = value),
edge_colour = "#a8a8a8") +
geom_node_point(color = "darkslategray4", size = 4) +
geom_node_text(aes(label = name), vjust = 2.2) +
ggtitle("The Product Space") +
theme_void()
Hausmann, Ricardo, César Hidalgo, Sebastián Bustos, Michele Coscia, Alexander Simoes, and Muhammed Yildirim. 2014. The Atlas of Economic Complexity: Mapping Paths to Prosperity. MIT Press. https://doi.org/10.7551/mitpress/9647.001.0001.
Mariani, Manuel, Alexandre Vidmer, Matsúš Medo, and Yi-Cheng Zhang. 2015. “Measuring Economic Complexity of Countries and Products: Which Metric to Use?” The European Physical Journal B 88 (11). Springer: 293. https://doi.org/10.1140/epjb/e2015-60298-7.