-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.Rmd
More file actions
1608 lines (1291 loc) · 89.5 KB
/
Copy pathindex.Rmd
File metadata and controls
1608 lines (1291 loc) · 89.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Markdown - Breaking in or Breaking Through? How Local Specialisations Shape the Integration of AI Technologies"
author: "Matheus E. Leusin"
date: "2024-09-12"
output:
html_document:
toc: true
toc_float: true # This makes it a floating sidebar in HTML
theme: united
df_print: paged
word_document: default
pdf_document: default
---
<br>
> 💡 **Welcome to the Methodological Appendix!**
>
> This file provides a fully transparent and reproducible account of the data analysis performed for the paper, *'Breaking in or Breaking Through? How Local Specialisations Shape the Integration of AI Technologies'* (doi: https://doi.org/10.1080/10438599.2025.2558626). Its goal is to offer a clear roadmap of the methodological steps, from initial data sourcing to the final regression analysis.
<br>
This document details the complete analytical pipeline, structured into five key parts:
> **1. Calculating Technological Specializations** *(Section 1.1)* ⚙️<br>
><small> This section describes how raw patent data is processed to calculate **Revealed Technological Advantage (RTA)** for different countries and for AI as a whole. This is performed across three distinct time intervals to capture technological evolution.</small><br>
>**2. Constructing and Visualizing Technological Spaces** *(Sections 1.2 & 1.3)* 🌐<br>
><small> Here, we explain the creation of the **Global Technological Space (GTS)**, based on the co-occurrence of technological fields in patents, and the dynamic **AI-specific Technological Space (ATS)**. This section also covers the visualization of national trajectories within these spaces.</small><br>
>**3. Generating Supporting Figures** *(Section 2)* 📊<br>
><small> This part outlines the code used to create the descriptive figures presented in the paper, such as the growth of AI patents and the share of specialized fields.</small><br>
>**4. Robustness Checks & Permutation Analysis** *(Section 3)* 🎲<br>
><small> This section details the permutation-based robustness checks, which are used to ensure that our findings are statistically significant and not the result of random chance.</small><br>
>**5. Econometric Analysis** *(Section 4)* 📈<br>
><small> Finally, we present the regression models used to formally test our hypotheses, including the data setup, model specifications, and interpretation of the results.</small><br>
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE, error = FALSE)
```
# 1. Technological Spaces based on Technological field
## 1.1. Calculate Specializations for Different Time intervals
This section details the foundational step of our analysis: calculating the technological specializations of countries and of AI itself. We begin by loading extensive patent datasets and defining several custom functions to streamline the process. The end goal is to compute Revealed Technological Advantage (RTA) scores for three distinct time intervals, which will later serve as the basis for constructing our technological spaces.
**Data Loading and Initial Setup**
We start by loading the necessary R libraries and defining a set of custom functions that will be used repeatedly for data aggregation and weighting. The primary dataset is a large file containing patent applications and their associated inventor locations, which is loaded in manageable chunks to optimize memory usage.
```{r, include=FALSE}
#### Main Code
# Load required libraries
library(tidyverse)
library(magrittr)
library(tidygraph)
library(ggraph)
library(EconGeo)
library(data.table)
library(netrankr)
library(dplyr)
library(tidyr)
library(ggrepel)
library(scales)
library(patchwork)
library(RColorBrewer)
library(janitor)
library(ggforce)
library(stringr)
library(openxlsx)
library(kableExtra)
rm(list=ls())
#set the working directory to where you saved the R code:
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
#1.First part: Technological Spaces based on Technological field -----
#Create important functions
# This function groups data by application ID, and within each group, it calculates
# a field-specific weight equal to 1 divided by the number of records in that group.
group_by_applnID <- function(data){
data %>%
group_by(appln_id) %>%
mutate(field_weight = 1 / n()) %>%
ungroup()
}
# This function aggregates the weighted fields at the country-technology field level.
group_by_ctry_and_techn_field <- function(data){
data %<>%
group_by(ctry_code, techn_field_nr) %>%
summarise(n_tech_reg = sum(field_weight)) %>%
ungroup() %>%
drop_na()
}
# This function aggregates the weighted fields at the country-subclass level.
group_by_ctry_and_subclass <- function(data){
data %<>%
group_by(ctry_code, Subclass) %>%
summarise(n_tech_reg = sum(field_weight)) %>%
ungroup() %>%
drop_na()
}
# This function arranges multiple ggplots into one figure.
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
library(grid)
# Combine all plots into a single list
plots <- c(list(...), plotlist)
numPlots = length(plots)
# If no layout is provided, define one based on the specified number of columns
if (is.null(layout)) {
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
# If only one plot, just print it
if (numPlots==1) {
print(plots[[1]])
} else {
# Set up a new page for the layout
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
# Print each plot in the correct layout position
for (i in 1:numPlots) {
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}
# This function creates a sparse matrix from the given inputs.
# i.input and j.input represent row and column indices, respectively.
create_sparse_matrix <- function(i.input, j.input){
require(Matrix)
mat <- spMatrix(
nrow = i.input %>% n_distinct(),
ncol = j.input %>% n_distinct(),
i = i.input %>% factor() %>% as.numeric(),
j = j.input %>% factor() %>% as.numeric(),
x = rep(1, i.input %>% length())
)
row.names(mat) <- i.input %>% factor() %>% levels()
colnames(mat) <- j.input %>% factor() %>% levels()
return(mat)
}
rows_part2 <- 45182803 - 40000000
ipc_all_patents_part2_chunk1_df <- fread("large_files/All_patents_and_IPCs_Part2.csv",
header = FALSE, nrow = 20000000)
ipc_all_patents_part2_chunk2_df <- fread("large_files/All_patents_and_IPCs_Part2.csv",
header = FALSE, nrow = 20000000, skip = 20000000)
ipc_all_patents_part2_chunk3_df <- fread("large_files/All_patents_and_IPCs_Part2.csv",
header = FALSE, nrow = rows_part2, skip = 40000000)
ipc_all_patents_part2_df <- rbind(ipc_all_patents_part2_chunk1_df,
ipc_all_patents_part2_chunk2_df, ipc_all_patents_part2_chunk3_df)
rm(ipc_all_patents_part2_chunk1_df, ipc_all_patents_part2_chunk2_df, ipc_all_patents_part2_chunk3_df)
setnames(ipc_all_patents_part2_df, c("appln_id", "ctry_code", "techn_field_nr", "weight", "priority_year"))
ai_patents_df <- fread("other_files/IPCs_AI.csv", sep=";", dec=",", header=TRUE)
ai_patents_df$ctry_code <- "AI_pat"
# Load IPC technology names for labeling
ipc_names_df <- read.csv("other_files/ipc_technology.csv", sep = ";", header = TRUE) %>%
select(field_nr, sector, field_name) %>% distinct(field_nr, .keep_all = TRUE) %>%
mutate(techn_field_nr = field_nr) %>% arrange(techn_field_nr)
```
A preview of the primary patent data (`ipc_all_patents_part2_df`) is shown below. Each row represents a patent application (`appln_id`) linked to an inventor's country (`ctry_code`) and a technological field (`techn_field_nr`).
```{r}
kable(as.data.frame(ipc_all_patents_part2_df[1:6,]))
```
Note that the original `weight` column from PATSTAT is disregarded in our analysis. We recalculate a fractional weight internally to ensure that each patent application has a total weight of 1, distributed equally among its assigned technological fields.
Next, we load two supplementary datasets:
1. **AI Patent Data** (`other_files/IPCs_AI.csv`): A curated list of patent applications identified as being related to Artificial Intelligence.
2. **IPC Technology Names** (`other_files/ipc_technology.csv`): A reference file that maps technological field numbers to their descriptive names and sectors.
The *AI Patent Data* (`ai_patents_df`) looks like this:
```{r}
head(ai_patents_df)
```
The *IPC Technology Names* (`ipc_names_df`) looks like this:
```{r}
kable(as.data.frame(ipc_names_df[1:6,]))
```
**Calculating Specializations for Interval 1 (1974-1988)**
The core of this section involves calculating the specialization scores for our first time interval, 1974-1988. This process is repeated identically for the subsequent two intervals.
The first step is to filter the main patent dataset for the specified period. We then apply our custom functions, `group_by_applnID()` and `group_by_ctry_and_techn_field()`, to fractionally count patent activities.
```{r, include=FALSE}
####1.1.2.1. Interval 1 (1974 - 1988)
# Define Time Interval
start_year <- 1973
end_year <- 1989
###Filter Data for Interval 1
ipc_all_patents_first_period_df <- ipc_all_patents_part2_df[priority_year > start_year & priority_year < end_year]
ipc_all_patents_first_period_df <- ipc_all_patents_first_period_df[, .(appln_id, ctry_code, techn_field_nr)]
# Calculate General RCA for Interval 1
# Compute weighted values at country-technology field level
region_tech_fields_1_df <- group_by_applnID(ipc_all_patents_first_period_df)
```
The `group_by_applnID()` function assigns an equal weight to each technological field within a single patent. For instance, if a patent is classified under four fields, each field receives a weight of 0.25. The result is a weighted dataset:
```{r}
kable(as.data.frame(region_tech_fields_1_df[1:6,]))
```
Next, `group_by_ctry_and_techn_field()` aggregates these weights, summing them up for each country-technology pair. This yields the total fractional count of patents for each country in each technological field.
```{r, include=FALSE}
region_tech_fields_1_df <- group_by_ctry_and_techn_field(region_tech_fields_1_df)
```
```{r}
kable(as.data.frame(region_tech_fields_1_df[1:6,]))
```
This aggregated data, saved as `reg_tech_FirstPeriod.csv`, is transformed into a country-technology matrix. The matrix rows represent countries, columns represent technological fields, and the values are the fractional patent counts. It looks like this:
```{r, include=FALSE}
mat_reg_tech1 <- region_tech_fields_1_df %>% arrange(techn_field_nr, ctry_code) %>%
pivot_wider(names_from = techn_field_nr, values_from = n_tech_reg, values_fill = 0)
mat_reg_tech1 %<>% remove_rownames %>% column_to_rownames(var="ctry_code") %>%
as.matrix() %>% round()
```
```{r}
kable(as.matrix(mat_reg_tech1[1:20, 1:12]), caption = "Sample of the Country-technology matrix")
```
Finally, we use this matrix to calculate the **Revealed Technological Advantage (RTA)** for each country in each field. RTA is a non-binary index that measures whether a country has a greater share of patents in a specific technology compared to the global average. An RTA value greater than or equal to 1 indicates a specialization.
```{r, include=FALSE}
## Calculate RCA (relative comparative advantage) for general technologies
reg_RCA1_df <- mat_reg_tech1 %>% location_quotient(binary = FALSE) %>% as.data.frame() %>%
rownames_to_column("ctry_code") %>% as_tibble() %>% gather("techn_field_nr", "RCA", -ctry_code)
```
```{r}
kable(as.data.frame(reg_RCA1_df[1:6,]))
```
This entire process is then repeated, this time using only the AI-related patents from the first interval to calculate **AI-specific RTAs** for each country. For the AI patents, the RTAs look like this:
```{r, include=FALSE}
# Calculate AI-Specific RCA for Interval 1
ai_patents_period_1_df <- ai_patents_df[ai_patents_df$priority_year > start_year & ai_patents_df$priority_year < end_year,]
# Join technological fields info to AI patents
ai_patents_period_1_df <- distinct(ai_patents_period_1_df, appln_id, .keep_all = TRUE)[, c(1,3)]
ai_patents_period_1_df <- left_join(ai_patents_period_1_df, ipc_all_patents_first_period_df, by = "appln_id")
# Compute weighted fields for AI patents
region_tech_fields_1_ai_df <- group_by_applnID(ai_patents_period_1_df)
rm(ai_patents_period_1_df)
region_tech_fields_1_ai_df <- group_by_ctry_and_techn_field(region_tech_fields_1_ai_df)
mat_reg_tech1_AI <- region_tech_fields_1_ai_df %>%
arrange(techn_field_nr, ctry_code) %>%
pivot_wider(names_from = techn_field_nr, values_from = n_tech_reg, values_fill = list(n_tech_reg = 0))
mat_reg_tech1_AI %<>% remove_rownames %>% column_to_rownames(var="ctry_code") %>% as.matrix() %>% round()
reg_RCA1_AI_df <- mat_reg_tech1_AI %>% location_quotient(binary = FALSE) %>%
as.data.frame() %>% rownames_to_column("ctry_code") %>%
as_tibble() %>% gather(key = "techn_field_nr", value = "RCA", -ctry_code) %>% arrange(ctry_code, techn_field_nr)
```
```{r}
kable(as.data.frame(reg_RCA1_AI_df[1:12,]))
```
The general and AI-specific RTA dataframes are then merged. The resulting file for the first interval shows, for each country and technological field, both its general specialization (`RCA_Gen`) and its AI-specific specialization (`RCA_AI`), as highlighted below for the whole dataset, and for Japan as an example.
```{r, include=FALSE}
# Merge general and AI-specific RCA data for Interval 1
rca_data_period_1_df <- merge(reg_RCA1_df, reg_RCA1_AI_df, all = TRUE, by = c("ctry_code", "techn_field_nr"))
rca_data_period_1_df$Period <- "1974-1988"
names(rca_data_period_1_df) <- c("ctry_code", "techn_field_nr", "RCA_Gen", "RCA_AI", "Period")
```
```{r}
#Resulting file:
kable(as.data.frame(rca_data_period_1_df[1:6,]))
#Example Japan:
kable(as.data.frame(rca_data_period_1_df[rca_data_period_1_df$ctry_code == "JP",][1:6,]))
```
A key methodological step follows: we treat the entire corpus of AI patents as if it belonged to a single, hypothetical 'country' named `AI_pat`. This novel approach allows us to calculate the RTA for AI itself across all technological fields, providing a benchmark against which national specializations can be compared. The resulting data is saved for later use, and it looks like this:
```{r, include=FALSE}
# Regional Tech for AI - Interval 1
setDT(ai_patents_df)
setDT(ipc_all_patents_first_period_df)
ipc_all_patents_first_period_df[ai_patents_df, on = "appln_id", ctry_code := i.ctry_code]
region_tech_ai_1_df <- group_by_applnID(ipc_all_patents_first_period_df)
region_tech_ai_1_df <- group_by_ctry_and_techn_field(region_tech_ai_1_df)
```
```{r}
kable(as.data.frame(region_tech_ai_1_df[region_tech_ai_1_df$ctry_code == "AI_pat",][1:6,]))
```
**Consolidating Data Across All Intervals**
The calculation process detailed above is repeated for the remaining two intervals: **1989-2003** and **2004-2018**. After processing all periods, the three interval-specific RTA files are combined into a single, comprehensive dataset named `IPC_RCAs` that is saved for later usage (`Files_created_with_the_code/data/files_code_Fields_analysis/IPC_RCAs.csv`). This file contains the complete history of general and AI-specific specializations for all countries across the three time periods.
Using Japan again as an example, the file looks like this for this country across each interval:
```{r, include=FALSE}
# Clean up
IPC_RCAs <- read.csv("Files_created_with_the_code/data/files_code_Fields_analysis/IPC_RCAs.csv",
sep = ";", header = TRUE, dec=",")
```
```{r}
kable(as.data.frame(IPC_RCAs[IPC_RCAs$ctry_code == "JP" & IPC_RCAs$Period == "1974-1988",][1:6,]))
kable(as.data.frame(IPC_RCAs[IPC_RCAs$ctry_code == "JP" & IPC_RCAs$Period == "1989-2003",][1:6,]))
kable(as.data.frame(IPC_RCAs[IPC_RCAs$ctry_code == "JP" & IPC_RCAs$Period == "2004-2018",][1:6,]))
```
To facilitate further analysis and visualization, we create consolidated summary files for each interval. These files combine the RTA scores of the four focus countries (US, CN, KR, JP) and the `AI_pat` entity into a single, wide-format table. They are named, e.g., as `Files_created_with_the_code/data/files_code_Fields_analysis/Metrics_First_period.csv` (for the First interval, and the names change accordingly for the Second and Third intervals). The data for this example interval looks like this:
```{r, include=FALSE}
###1.1.2.4. Create a Specializations Summary
# First Interval Countries
# Load first interval data for countries
reg_tech1_countries <- read.csv("Files_created_with_the_code/data/files_code_Fields_analysis/reg_tech_FirstPeriod.csv",
sep = ";", header = TRUE, dec=",")
# Create a wide matrix of technology fields for the first interval
mat_reg_tech1_countries <- reg_tech1_countries %>% arrange(techn_field_nr, ctry_code) %>%
pivot_wider(names_from = techn_field_nr, values_from = n_tech_reg, values_fill = list(n_tech_reg = 0))
mat_reg_tech1_countries %<>% remove_rownames %>%
column_to_rownames(var="ctry_code") %>% as.matrix() %>% round()
# Compute RCA for the first interval (general)
reg_RCA1_countries <- mat_reg_tech1_countries %>% location_quotient(binary = FALSE) %>%
as.data.frame() %>% rownames_to_column("ctry_code") %>%
as_tibble() %>% gather(key = "techn_field_nr", value = "RCA", -ctry_code) %>% arrange(ctry_code, techn_field_nr)
# First Interval AI
# Load first interval data for AI-specific patents
reg_tech1_AI <- read.csv("Files_created_with_the_code/data/files_code_Fields_analysis/reg_techAI_FirstPeriod.csv",
sep = ";", header = TRUE, dec=",")
# Create a wide matrix of technology fields for AI patents in the first interval
mat_reg_tech1_AI <- reg_tech1_AI %>% arrange(techn_field_nr, ctry_code) %>%
pivot_wider(names_from = techn_field_nr, values_from = n_tech_reg, values_fill = list(n_tech_reg = 0))
mat_reg_tech1_AI %<>% remove_rownames %>% column_to_rownames(var="ctry_code") %>%
as.matrix() %>% round()
# Compute RCA for the first interval (AI-specific)
reg_RCA1_AI <- mat_reg_tech1_AI %>% location_quotient(binary = FALSE) %>%
as.data.frame() %>% rownames_to_column("ctry_code") %>%
as_tibble() %>% gather(key = "techn_field_nr", value = "RCA", -ctry_code) %>%
arrange(ctry_code, techn_field_nr)
# Load IPC names and metadata
IPC_names <- read.csv("other_files/ipc_technology.csv", sep = ";", header = TRUE)%>%
select(field_nr, sector, field_name) %>% distinct(field_nr, .keep_all = TRUE) %>%
mutate(techn_field_nr = field_nr) %>% arrange(techn_field_nr)
IPC_names <- IPC_names[, -1]
# Extract top countries and AI data for first interval
US_first_period <- reg_RCA1_countries[,2:3][reg_RCA1_countries$ctry_code == "US",]
CN_first_period <- reg_RCA1_countries[,2:3][reg_RCA1_countries$ctry_code == "CN",]
KR_first_period <- reg_RCA1_countries[,2:3][reg_RCA1_countries$ctry_code == "KR",]
JP_first_period <- reg_RCA1_countries[,2:3][reg_RCA1_countries$ctry_code == "JP",]
AI_first_period <- reg_RCA1_AI[,2:3][reg_RCA1_AI$ctry_code == "AI_pat",]
# Merge IPC names with the countries and AI RCAs for the first interval
First_period <- merge(merge(merge(merge(merge(IPC_names, US_first_period), CN_first_period, by = "techn_field_nr"),
KR_first_period, by = "techn_field_nr"), JP_first_period, by = "techn_field_nr"),
AI_first_period, by = "techn_field_nr")
names(First_period) <- c("techn_field_nr", "sector", "field_name", "RCA_US", "RCA_CN","RCA_KR","RCA_JP", "RCA_AI")
```
```{r}
kable(as.data.frame(First_period[1:6,]))
```
Finally, these three interval-specific summary files are merged into a master file named `All_periods`, shown below. This file includes additional labels for analytical purposes, though these are not central to the paper's main findings.
```{r, include=FALSE}
IPC_names <- read.csv("Files_created_with_the_code/data/files_code_Fields_analysis/Specializations_All_periods_IPC.csv",
sep = ";", header = TRUE)[,c(-1)]
```
```{r}
head(IPC_names)
```
In the last step of this sub-section, we use the `IPC_RCAs.csv` file to generate a summary table (`IPC_RCAs_Top4`). Here, the non-binary RTA values are binarized, where any RTA ≥ 1 is considered a specialization (value of 1) and any RTA < 1 is not (value of 0). We then sum these binary indicators to count the number of general specializations, AI-specific specializations, and coinciding specializations (where a country is specialized in both the general field and its AI-specific application) for each country and interval, resulting in the following dataset:
```{r, include=FALSE}
IPC_RCAs_Top4 <- read.csv("Files_created_with_the_code/data/files_code_Fields_analysis/RCA_4countries_detailed.csv",
sep = ";", header = TRUE, dec=",")
```
```{r}
kable(as.data.frame(IPC_RCAs_Top4[1:6,]))
```
## 1.2. Building the Global Technological Space (GTS)
The next step is to construct the backbone of our analysis: the **Global Technological Space (GTS)**. This space is a network where nodes represent technological fields, and the links between them signify their relatedness. We measure this relatedness based on the principle that technologies that frequently appear together within the same patent are likely to be related.
### 1.2.1. From Patents to a Co-occurrence Matrix
To quantify this relationship, we must first count how often every possible pair of technologies co-occurs across the entire patent dataset. We start by loading the complete patent database (which, due to its size, is again handled in chunks) and applying the `create_sparse_matrix` function. This function generates a very large matrix where rows are unique patents and columns are the 35 technological fields.
```{r, include=FALSE}
c <- 58841893 - 40000000
IPC_all_patents_Part1 <- fread("large_files/All_patents_and_IPCs_Part1.csv", header = FALSE, nrow = 20000000)
IPC_all_patents_Part2 <- fread("large_files/All_patents_and_IPCs_Part1.csv", header = FALSE, nrow = 20000000, skip = 20000000)
IPC_all_patents_Part3 <- fread("large_files/All_patents_and_IPCs_Part1.csv", header = FALSE, nrow = c, skip = 40000000)
names(IPC_all_patents_Part1) <- c("appln_id", "ctry_code", "techn_field_nr", "weight", "priority_year")
names(IPC_all_patents_Part2) <- c("appln_id", "ctry_code", "techn_field_nr", "weight", "priority_year")
names(IPC_all_patents_Part3) <- c("appln_id", "ctry_code", "techn_field_nr", "weight", "priority_year")
# Create sparse matrices and compute cross-products for the first big file
mat_tech_AI1 <- create_sparse_matrix(i = IPC_all_patents_Part1 %>% pull(appln_id),
j = IPC_all_patents_Part1 %>% pull(techn_field_nr)) #
```
The resulting sparse matrix, `mat_tech_AI1`, indicates the presence of a technology in a given patent, and it looks like this:
```{r}
kable(as.matrix(mat_tech_AI1[1:20, 1:12]), caption = "Sample of the Sparse AI matrix")
```
By calculating the cross-product of this matrix (`t(M) %*% M`), we transform it into a 35x35 square **co-occurrence matrix**. Each cell (`i, j`) in this new matrix contains a count of how many patents simultaneously list technology `i` and technology `j`. This square matrix looks like this:
```{r, include=FALSE}
mat_tech_AI1 %<>% crossprod() %>% as.matrix()
```
```{r}
kable(as.matrix(mat_tech_AI1[1:35, 1:35]), caption = "Sample of the co-occurrence matrix")
```
After processing all data chunks, the individual co-occurrence matrices are summed to create a final, comprehensive matrix, which is then saved as `Matrix_IPC.csv`. This file looks like this:
```{r, include=FALSE}
matrix2 <- read.csv("Files_created_with_the_code/data/files_code_Fields_analysis/Matrix_IPC.csv",
sep = ";", header = FALSE)
matrix2 <- matrix2 %>% row_to_names(row_number = 1)
matrix <- matrix2[,-1]
rownames(matrix) <- matrix2[,1]
matrix <- as.matrix(matrix)
mat_tech_AI_Final <- matrix
```
```{r}
kable(as.matrix(mat_tech_AI_Final[1:35, 1:35]))
```
### 1.2.2. Calculating Relatedness and Defining the Network
Raw co-occurrence counts can be misleading, as highly prevalent technologies will naturally co-occur more often with others, inflating their apparent relatedness. To correct for this, we normalize the matrix using the `relatedness()` function from the `EconGeo` package, which employs a cosine similarity index. The result is a relatedness matrix, where each value represents the strength of the relationship between two technologies. It looks like this:
```{r, include=FALSE}
# Calculate relatedness using cosine similarity
mat_tech_rel_AI <- mat_tech_AI_Final %>% relatedness(method = "cosine")
```
```{r}
kable(as.matrix(mat_tech_rel_AI[1:35, 1:35]))
```
With the relatedness matrix complete, we can now treat it as an adjacency matrix to build a network graph (`g_tech_AI`). The nodes' centrality (**Eigenvector centrality**) is calculated to determine their importance in the network. For visual clarity in later plots, links with below-average weight (relatedness) are filtered out. Finally, a Fruchterman-Reingold layout algorithm is applied to determine the spatial coordinates (`coords_tech_AI`) of each node for visualization, which results in the following coordinates:
```{r, include=FALSE}
# Load IPC names and categories
IPC_names <- read.csv("Files_created_with_the_code/data/files_code_Fields_analysis/Specializations_All_periods_IPC.csv",
sep = ";", header = TRUE)%>%
select(techn_field_nr, sector, field_name, Category) %>% distinct(techn_field_nr, .keep_all = TRUE) %>%
mutate(techn_field_nr = techn_field_nr) %>% arrange(techn_field_nr)
# Build a graph from the relatedness matrix
g_tech_AI <- mat_tech_rel_AI %>% as_tbl_graph(directed = FALSE) %N>%
left_join(IPC_names %>% mutate(techn_field_nr = as.character(techn_field_nr)),
by = c("name" = "techn_field_nr")) %>% mutate(dgr = centrality_eigen(weights = weight)) %E>%
filter(weight >= mean(weight))
# Layout for visualization (Fruchterman-Reingold)
coords_tech_AI <- g_tech_AI %>% igraph::layout.fruchterman.reingold() %>% as_tibble()
colnames(coords_tech_AI) <- c("x", "y")
# Alternatively, load predefined coordinates
coords_tech_AI <- read.csv("other_files/coords_tech_AI_layout1.csv", sep = ";", header = TRUE, dec=",")
```
```{r}
kable(as.data.frame(coords_tech_AI[1:10,]))
```
### 1.2.3. Preparing Data for Visualization
In the final step of this section, we prepare the specialization data (calculated in Section 1.1) for plotting onto the GTS. We load the summary file (`RCA_4countries_detailed.csv`) and create a new categorical variable (`Var1`) that classifies each country-technology pair into one of four states: no specialization (0), general specialization (1), AI-specific (break-through) specialization (2), or coinciding (break-in) specialization (3). This will allow us to map the countries' technological trajectories directly onto the GTS structure in the next section. The dataset looks like this:
```{r, include=FALSE}
# Load top 4 countries RCA details
IPC_RCAs_Top4 <- read.csv("Files_created_with_the_code/data/files_code_Fields_analysis/RCA_4countries_detailed.csv",
sep = ";", header = TRUE, dec=",")
IPC_RCAs_Top4$Total_RCA <- as.factor(IPC_RCAs_Top4$Total_RCA)
IPC_RCAs_Top4$Period_sim <- as.numeric(factor(IPC_RCAs_Top4$Period, levels=unique(IPC_RCAs_Top4$Period)))
IPC_RCAs_Top4$techn_field_nr <- as.character(IPC_RCAs_Top4$techn_field_nr)
# Load AI RCA data and merge with IPC_RCAs_Top4
AI_RCA <- read.csv("Files_created_with_the_code/data/files_code_Fields_analysis/Specializations_All_periods_IPC.csv",
sep = ";", header = TRUE, dec=",")
AI_RCA$Period_sim <- as.numeric(factor(AI_RCA$Period, levels=unique(AI_RCA$Period)))
AI_RCA <- AI_RCA[, c(2,9,13)]
AI_RCA$techn_field_nr <- as.character(AI_RCA$techn_field_nr)
names(AI_RCA) <- c("techn_field_nr", "RCA_AI_Period", "Period_sim")
IPC_RCAs_Top4 <- left_join(IPC_RCAs_Top4, AI_RCA, by = c("techn_field_nr", "Period_sim"))
# Adjust Total_RCA_2 to differentiate between general and AI specialization
IPC_RCAs_Top4$Total_RCA_2 <- IPC_RCAs_Top4$Round_general + 2*IPC_RCAs_Top4$Round_AI
# Summarize data by category of specialization
Newtable <- as.data.frame(table(IPC_RCAs_Top4$Total_RCA_2, IPC_RCAs_Top4$ctry_code, IPC_RCAs_Top4$Period))
Newtable$Var1 <- gsub("0", "No specialization", str_trim(Newtable$Var1))
Newtable$Var1 <- gsub("1", "General specialization", str_trim(Newtable$Var1))
Newtable$Var1 <- gsub("2", "AI-specific specialization", str_trim(Newtable$Var1))
Newtable$Var1 <- gsub("3", "Coinciding specialization", str_trim(Newtable$Var1))
```
```{r}
kable(as.data.frame(Newtable[1:10,]))
```
## 1.3. Plotting technological spaces
Now that the underlying data and network structures are in place, this section focuses on their visualization. We will generate the key plots presented in the paper, illustrating both the static, global structure of technology and the dynamic, evolving space of AI.
### 1.3.1. Global technological space (GTS)
We begin by plotting the fundamental structure of the Global Technological Space. This initial visualization is **geography-agnostic**, meaning it shows the inherent relatedness between technological fields without any country-specific data. The node size corresponds to its centrality (degree), and nodes are clustered and colored by their broader technological sector. This plot serves as the canvas upon which we will later map national trajectories.
```{r, fig.width=14, fig.height=10}
g_tech_AI %>% ggraph(layout = coords_tech_AI) +
geom_edge_link(aes(width = weight), alpha = 0.4, colour = "grey") +
geom_node_point(aes(fill = sector, size = 1000^dgr, shape= sector))+ #
scale_shape_manual(values=c(21, 22, 23, 24, 25)) + scale_size("Degree", range = c(2, 12)) +
geom_node_text(aes(label = paste0(field_name, "\n(", name, ")")), size = 4, repel = TRUE) + #field_name or name
theme_graph(base_family = "sans")+ ggtitle("Global technological space: IPC Technological fields") +
theme(legend.title = element_text(size = 14), legend.text = element_text(size = 10)) +
guides(colour = guide_legend(override.aes = list(size=10)))+
geom_mark_hull(aes(x = x, y=y, colour = sector, fill= sector,
linetype = sector), alpha = 0.15, expand = unit(2.5, "mm"), size = 1)
```
Next, we overlay the country-specific specialization data onto the static GTS canvas. This allows us to visualize the technological trajectory of each country over the three time intervals. The shape of each node indicates the type of specialization (general, break-through, or break-in), while hulls are drawn to highlight the clusters of specialization for each period. This composite visualization reveals how each nation's technological focus has evolved within the global structure. Additionally, an horizontal bar plot is also generated to summarize the main indicators based on the country-specific specializations. Picking China as an example, this overlaid visualization and its linked bar-plot look like this:
```{r, fig.width=14, fig.height=10}
#GTS with specialisations per country
country_select <- c("CN", "US", "JP", "KR")
### 1.2.3.3. Third Country
i=1
IPC_RCAs_wide_simplified <- IPC_RCAs_Top4 %>% pivot_wider(id_cols = c(ctry_code, techn_field_nr, Label),
names_from = Period_sim,
values_from = c(RCA_AI_Period, Total_RCA_2, RCA_Gen, RCA_AI, Round_general, Round_AI, Total_RCA),
names_glue = "{.value}_Period_{Period_sim}" )
g_tech_AI %N>% left_join(IPC_RCAs_wide_simplified %>%
filter(ctry_code == country_select[i]) %>%
select(-ctry_code), by = c("name" = "techn_field_nr")) %>%
mutate(Shape_Group_P1_Factor = factor(
ifelse(is.na(Total_RCA_2_Period_1), "NA_Value", as.character(Total_RCA_2_Period_1)),
levels = c("0", "1", "2", "3", "NA_Value"))) %>% ggraph(layout = coords_tech_AI) +
geom_edge_link(aes(width = weight), alpha = 0.2, colour = "#CCCCCC", show.legend = FALSE) +
geom_node_point(aes(shape = Shape_Group_P1_Factor,
size = 5, stroke = ifelse(Total_RCA_2_Period_1 == 3, 2.5, 1.3),
alpha = 1), color = "#FF3300", show.legend = c(shape=TRUE, size=FALSE, stroke=FALSE, alpha=FALSE, color=FALSE)) +
geom_node_point(aes(shape = factor(Total_RCA_2_Period_2),
size = 5.5, stroke = ifelse(Total_RCA_2_Period_2 == 3, 2.5, 1.3),
alpha = 1), color = "#3399FF", show.legend = FALSE) +
geom_node_point(aes(shape = factor(Total_RCA_2_Period_3),
size = 6.5,stroke = ifelse(Total_RCA_2_Period_3 == 3, 2.5, 1.3),
alpha = 1), color = "#009900", show.legend = FALSE) +
scale_shape_manual(name = "Type of specialisation",
values = c("0" = 4, "1" = 1, "2" = 5, "3" = 2, "NA_Value" = 16), breaks = c("0", "1", "2", "3"),
labels = c("0" = "No specialisation", "1" = "General specialisation",
"2" = "Break-through specialisation", "3" = "Break-in specialisation"),
na.translate = FALSE, drop = FALSE) + scale_size("Degree", range = c(7, 18))+
scale_alpha(guide = "none") +
#geom_node_label(aes(label = name), size = 2, repel = F) +
geom_mark_hull(aes(filter = Total_RCA_2_Period_1 > .99, x = x, y = y, fill = "Period 1", group = "Period 1"),
concavity = .1, alpha = .11, linetype = "dotted",expand = unit(2, "mm"), size = .5, color = "#FF3300") +
geom_mark_hull(aes(filter = Total_RCA_2_Period_2 > .99, x = x, y = y, fill = "Period 2", group = "Period 2"),
concavity = .1, alpha = .11, linetype = "longdash",expand = unit(2, "mm"), size = .5, color = "#3399FF") +
geom_mark_hull(aes(filter = Total_RCA_2_Period_3 > .99, x = x, y = y, fill = "Period 3", group = "Period 3"),
concavity = .1, alpha = .02, expand = unit(2, "mm"), size = 1, color = "#009900") +
scale_fill_manual(name = "Interval colour (same for \nboth nodes and cluster)", # New legend for fill
values = c("Period 1" = "#FF3300", "Period 2" = "#3399FF", "Period 3" = "#009900"),
labels = c("Interval 1 (1974-1988)", "Interval 2 (1989-2003)", "Interval 3 (2004-2018)")) +
theme_graph(base_family = "sans") + theme(legend.position = "bottom", #right
legend.box = "vertical", legend.title = element_text(size = 12, face = "bold"),
legend.text = element_text(size = 10), legend.key.size = unit(0.7, "cm") ) +
ggtitle("d) Global technological space: China (1974-2018)") +
geom_node_text(aes(label = name), size = 5, repel = TRUE) + #field_name or name
guides(shape = guide_legend(title.position = "top",
override.aes = list(size = 5, stroke = 1.5, color = "black") ),
colour = guide_legend(title.position = "top",
override.aes = list(linetype = c("solid", "longdash", "dotted"),
alpha = 1, size = 1, shape = NA) ))
bar_plot_China <- bar_plot_China <- IPC_RCAs_Top4[IPC_RCAs_Top4$ctry_code == country_select[i],] %>%
arrange(Label, Period) %>% group_by(Label) %>%
mutate( general = Total_RCA_2 == 1,
break_in = Total_RCA_2 == 2,
break_through = Total_RCA_2 == 3,
sustained_general = general & lag(general, 1, default = FALSE),
sustained_break_in = break_in & lag(break_in, 1, default = FALSE),
sustained_break_through = break_through & lag(break_through, 1, default = FALSE)) %>%
ungroup()
bar_plot_China <- bar_plot_China %>%
group_by(Period) %>% summarise(`General case` = sum(general, na.rm = TRUE),
`Break-through case` = sum(break_in, na.rm = TRUE),
`Break-in case` = sum(break_through, na.rm = TRUE),
`Sustained General case` = sum(sustained_general, na.rm = TRUE),
`Sustained break-through case` = sum(sustained_break_in, na.rm = TRUE),
`Sustained break-in case` = sum(sustained_break_through, na.rm = TRUE),
.groups = "drop") %>% arrange(Period)
plot_long_China <- bar_plot_China |> rename(Period = Period) |>
pivot_longer(cols= -Period,names_to= "Indicator",values_to = "Count")
#order labels
plot_long_China$Indicator <- factor(plot_long_China$Indicator, levels = rev(c("General case", "Break-through case", "Break-in case",
"Sustained General case", "Sustained break-through case", "Sustained break-in case")))
plot_long_China$Period <- factor(plot_long_China$Period, levels = c("2004-2018", "1989-2003", "1974-1988"))
legend_order <- c(
"General case", "Break-through case", "Break-in case",
"Sustained General case", "Sustained break-through case", "Sustained break-in case"
)
ggplot(plot_long_China, aes(x = factor(Period),y = Count, fill = Indicator)) +
geom_col(position = position_dodge(width = .8), width = .7) +
scale_fill_manual(values = c("General case" = "#FF3300",
"Sustained General case" = "#993333",
"Break-in case" = "#009900", #3399FF
"Sustained break-in case" = "#006633", #3333CC
"Break-through case" = "#3399FF", #009900
"Sustained break-through case" = "#3333CC"),
breaks = legend_order) + #006633
guides(fill = guide_legend(nrow = 2, byrow = TRUE)) +
labs(x = "Interval",y = "Number of cases", fill = NULL, title = NULL)+
ggtitle("Summary of specialisations China") +
theme_classic(base_size = 11) + theme(legend.position = "bottom")+ coord_flip()
```
The plotting code is structured to iterate through each of the four focus countries by changing the `i` variable. The resulting figures, each depicting a single country's trajectory over three periods alongside a summary bar chart, are then saved.
### 1.3.2. AI-specific technological space (ATS)
Unlike the static GTS, the **AI-specific Technological Space (ATS)** is dynamic. Its structure is recalculated for each time interval, reflecting the rapid evolution of AI technology. Here, the relatedness between fields is based only on their co-occurrence within AI patents for that specific period. This approach allows us to observe which technological fields form the core of AI innovation at different points in time.
```{r, include=FALSE}
rm(list = ls()[!sapply(ls(), function(x) is.function(get(x)))])
gc()
# ATS First interval
patents_AI_specific_1st <- read.csv("Files_created_with_the_code/data/files_code_Fields_analysis/AI_ALL_patents.csv", sep = ";", header = TRUE, dec=",")
a = 1973
b = 1989
patents_AI_specific_1st <- patents_AI_specific_1st[patents_AI_specific_1st$priority_year < b,]
patents_AI_specific_1st <- patents_AI_specific_1st[patents_AI_specific_1st$priority_year > a,]
length(unique(patents_AI_specific_1st$appln_id)) #436
patents_AI_specific_1st <- patents_AI_specific_1st[is.na(patents_AI_specific_1st$appln_id)==F,]
mat_tech_AI <- create_sparse_matrix(i = patents_AI_specific_1st %>% pull(appln_id),
j = patents_AI_specific_1st %>% pull(techn_field_nr))
mat_tech_AI %<>% crossprod() %>% as.matrix()
mat_tech_rel_AI <- mat_tech_AI %>% relatedness(method = "cosine")
IPC_names <- read.csv("Files_created_with_the_code/data/files_code_Fields_analysis/Specializations_All_periods_IPC.csv", sep = ";", header = TRUE)%>%
select(techn_field_nr, sector, field_name, Category) %>% distinct(techn_field_nr, .keep_all = TRUE) %>%
mutate(techn_field_nr = techn_field_nr) %>% arrange(techn_field_nr)
g_tech_AI <- mat_tech_rel_AI %>% as_tbl_graph(directed = FALSE) %N>%
left_join(IPC_names %>% mutate(techn_field_nr = techn_field_nr %>% as.character()), by = c("name" = "techn_field_nr")) %>%
mutate(dgr = centrality_eigen(weights = weight)) %E>% filter(weight >= mean(weight))
#Create the Coordinates
coords_tech_AI <- g_tech_AI %>% igraph::layout.fruchterman.reingold() %>% as_tibble()
colnames(coords_tech_AI) <- c("x", "y")
```
Starting with the first interval (1974-1988), the top 10 most central technological fields in the AI space are:
```{r}
g_tech_AI %N>% arrange(desc(dgr)) %>% as_tibble() %>% slice(1:10)
```
We use the previously calculated AI specialization data (`AI_RCA`) to highlight the core technologies in each period. A binary flag indicates whether AI has an RTA ≥ 1 in a given field (and Period_sim refers to each interval, going from 1 to 3), like this:
```{r, include=FALSE}
AI_RCA <- read.csv("Files_created_with_the_code/data/files_code_Fields_analysis/Specializations_All_periods_IPC.csv", sep = ";", header = TRUE, dec=",")
AI_RCA$Period_sim <- as.numeric(factor(AI_RCA$Period,levels=unique(AI_RCA$Period)))
AI_RCA <- AI_RCA[,c(2,9,13)]
AI_RCA$techn_field_nr <- as.character(AI_RCA$techn_field_nr)
names(AI_RCA) <- c("techn_field_nr", "RCA_AI_Period", "Period_sim")
AI_RCA$Binary <- ifelse(AI_RCA$RCA_AI_Period < 1, 0,1)
```
```{r}
kable(as.data.frame(AI_RCA[1:6,]))
```
The following code generates the ATS for the first interval (1974-1988). The nodes with labels are those where AI is specialized (RTA ≥ 1).
```{r, fig.width=14, fig.height=10}
AI_RCA1 <- AI_RCA[AI_RCA$Period_sim == 1,]
p=1
g_tech_AI %N>%
left_join(AI_RCA1 %>% filter(Period_sim == p), by = c("name" = "techn_field_nr")) %>%
ggraph(layout = coords_tech_AI) +
geom_edge_link(aes(width = weight), alpha = 0.2, colour = "#CCCCCC") +
geom_node_point(aes(fill = sector, size = 1000^dgr, shape= sector)) +
scale_shape_manual(values=c(21, 22, 23, 24, 25)) + labs(color = "RCA")+ scale_size("Degree", range = c(2, 12)) +
geom_node_text(aes(filter=Binary > .99, label = field_name), size = 6, repel = TRUE) +
theme_graph(base_family = "sans") + guides(colour = guide_legend(override.aes = list(size=5)))+
ggtitle("AI-specific technological space (1974-1988)") #
```
We do the same for the 2 other intervals, and combine the three figures again using the multiplot custom function. The resulting figure is saved at `Files_created_with_the_code/figures/Figure_2_ATS_and_AI_core_technologies_3_intervals.jpg`.
# 2. Generating Descriptive Figures
This section details the creation of the paper's descriptive figures. These visualizations illustrate key trends in AI patenting and the evolution of national specialization strategies that motivate our main analysis.
## 2.1. Share of Break-in specialisations (Fig 6 and 7)
Here, we generate the plots showing the share of 'break-in' specializations for each country over time. This metric is central to our paper's narrative and is calculated as the ratio of **coinciding specializations** (specialized in both the general field and its AI application) to the country's total number of **general specializations**. A higher share indicates that a larger portion of a country's established technological strengths is being integrated with AI. We first perform this analysis at the technological field level.
```{r, include=FALSE}
rm(list = ls()[!sapply(ls(), function(x) is.function(get(x)))])
gc()
IPC_RCAs_Top4 <- read.csv("Files_created_with_the_code/data/files_code_Fields_analysis/RCA_4countries_detailed.csv", sep = ";", header = TRUE, dec=",")
IPC_RCAs_Top4$Total_RCA <- as.factor(IPC_RCAs_Top4$Total_RCA)
IPC_RCAs_Top4$Period_sim <- as.numeric(factor(IPC_RCAs_Top4$Period,levels=unique(IPC_RCAs_Top4$Period)))
IPC_RCAs_Top4$techn_field_nr <- as.character(IPC_RCAs_Top4$techn_field_nr)
#replace names:
IPC_RCAs_Top4$ctry_code <- gsub("US", "USA", str_trim(IPC_RCAs_Top4$ctry_code))
IPC_RCAs_Top4$ctry_code <- gsub("CN", "China", str_trim(IPC_RCAs_Top4$ctry_code))
IPC_RCAs_Top4$ctry_code <- gsub("JP", "Japan", str_trim(IPC_RCAs_Top4$ctry_code))
IPC_RCAs_Top4$ctry_code <- gsub("KR", "South Korea", str_trim(IPC_RCAs_Top4$ctry_code))
AI_RCA <- read.csv("Files_created_with_the_code/data/files_code_Fields_analysis/Specializations_All_periods_IPC.csv", sep = ";", header = TRUE, dec=",")
AI_RCA$Period_sim <- as.numeric(factor(AI_RCA$Period,levels=unique(AI_RCA$Period)))
AI_RCA <- AI_RCA[,c(2,9,13)]
AI_RCA$techn_field_nr <- as.character(AI_RCA$techn_field_nr)
names(AI_RCA) <- c("techn_field_nr", "RCA_AI_Period", "Period_sim")
IPC_RCAs_Top4 <- left_join(IPC_RCAs_Top4, AI_RCA, by = c("techn_field_nr", "Period_sim"))
#fix Total_RCA:
IPC_RCAs_Top4$Total_RCA_2 <- IPC_RCAs_Top4$Round_general + 2*IPC_RCAs_Top4$Round_AI
rm(AI_RCA)
IPC_RCAs_Top4$Coiciding <- ifelse(IPC_RCAs_Top4$Total_RCA_2 ==3,1,0)
IPC_RCAs_Top4$justGeneral <- ifelse(IPC_RCAs_Top4$Total_RCA_2 ==1,1,0)
IPC_RCAs_Top4$OnlyAI <- ifelse(IPC_RCAs_Top4$Total_RCA_2 ==2,1,0)
#now, create a file per country per interval, where I sum over the 3 columns;
IPC_RCAs <- IPC_RCAs_Top4
IPC_RCAs %<>%
group_by(ctry_code,Period) %>% mutate(Share_coinciding = sum(Coiciding)/(sum(Coiciding)+sum(justGeneral))) %>%
mutate(Share_OnlyAI = sum(OnlyAI)/(sum(OnlyAI)+sum(Coiciding))) %>%
mutate(sum_coinciding = sum(Coiciding)) %>% mutate(sum_justGeneral = sum(justGeneral)) %>%
mutate(sum_OnlyAI = sum(OnlyAI)) %>% ungroup()
```
The data is processed to count the number of 'coinciding', 'general only', and 'AI only' specializations for each country and period. From these counts, the `Share_coinciding` is calculated. The resulting summary table is shown below.
```{r}
SummaryAllData<-distinct(IPC_RCAs, ctry_code, Period, .keep_all = TRUE)
colnames(SummaryAllData)[1] <- "Country"
head(SummaryAllData)
```
This summarized data is then used to plot the evolution of the break-in share for the four focus countries (Figure 6).
```{r, fig.width=12, fig.height=4}
ggplot(data=SummaryAllData, aes(x=Period, y=Share_coinciding, group=Country, shape = Country, color=Country)) +
geom_point(aes(fill = Country), size=8) + scale_shape_manual(values=c(21, 22, 24, 23)) +
xlab("Interval") + ylab("Share of break-in specialisations (%)") +
theme_classic() + geom_line(aes(color=Country), linetype = "dashed", size=1.5)+
scale_y_continuous(labels = scales::percent) +
scale_fill_manual(values = c("#1B9E77", "#D95F02", "#7570B3", "#E7298A")) +
scale_color_manual(values = c("#1B9E77", "#D95F02", "#7570B3", "#E7298A"))
```
To ensure the robustness of our findings, we repeat the analysis at a more granular level of technological classification: the **4-digit IPC subclass**. This serves as a check to confirm that the observed trends are not an artifact of the broader 35-field aggregation.
```{r, include=FALSE}
IPC_RCAs <- read.csv("Files_created_with_the_code/data/files_code_4-digits_analysis/IPC_RCAs_subclass.csv", sep = ";", header = TRUE, dec=",")
#Select the 4 countries we want
IPC_RCAs_Top4 <- IPC_RCAs[IPC_RCAs$ctry_code == "CN" | IPC_RCAs$ctry_code == "KR"|
IPC_RCAs$ctry_code == "US"|IPC_RCAs$ctry_code == "JP", ]
rm(IPC_RCAs)
#replace names:
IPC_RCAs_Top4$ctry_code <- gsub("US", "USA", str_trim(IPC_RCAs_Top4$ctry_code))
IPC_RCAs_Top4$ctry_code <- gsub("CN", "China", str_trim(IPC_RCAs_Top4$ctry_code))
IPC_RCAs_Top4$ctry_code <- gsub("JP", "Japan", str_trim(IPC_RCAs_Top4$ctry_code))
IPC_RCAs_Top4$ctry_code <- gsub("KR", "South Korea", str_trim(IPC_RCAs_Top4$ctry_code))
IPC_RCAs_Top4$Period_sim <- as.numeric(factor(IPC_RCAs_Top4$Period,levels=unique(IPC_RCAs_Top4$Period)))
#replace NAs by 0:
#replace NAs, so we don't have problems when summing:
IPC_RCAs_Top4[is.na(IPC_RCAs_Top4)] <- 0
#make the numbers binary
IPC_RCAs_Top4$RCA_Gen2 <- ifelse(IPC_RCAs_Top4$RCA_Gen >=1,1,0)
IPC_RCAs_Top4$RCA_AI2 <- ifelse(IPC_RCAs_Top4$RCA_AI >=1,1,0)
#fix Total_RCA:
IPC_RCAs_Top4$Total_RCA_2 <- IPC_RCAs_Top4$RCA_Gen2 + 2*IPC_RCAs_Top4$RCA_AI2
IPC_RCAs_Top4$Coiciding <- ifelse(IPC_RCAs_Top4$Total_RCA_2 ==3,1,0)
IPC_RCAs_Top4$justGeneral <- ifelse(IPC_RCAs_Top4$Total_RCA_2 ==1,1,0)
IPC_RCAs_Top4$OnlyAI <- ifelse(IPC_RCAs_Top4$Total_RCA_2 ==2,1,0)
#now, create a file per country per interval, where I sum over the 3 columns;
IPC_RCAs <- IPC_RCAs_Top4
IPC_RCAs %<>%
group_by(ctry_code,Period) %>%
mutate(Share_coinciding = sum(Coiciding)/(sum(Coiciding)+sum(justGeneral))) %>%
mutate(Share_OnlyAI = sum(OnlyAI)/(sum(OnlyAI)+sum(Coiciding))) %>%
mutate(sum_coinciding = sum(Coiciding)) %>%
mutate(sum_justGeneral = sum(justGeneral)) %>%
mutate(sum_OnlyAI = sum(OnlyAI)) %>%
ungroup()
SummaryAllData4dig<-distinct(IPC_RCAs, ctry_code, Period, .keep_all = TRUE)
colnames(SummaryAllData4dig)[1] <- "Country"
```
The resulting plot (Figure 7) confirms that the trends observed at the field level are consistent at the more detailed subclass level.
```{r, fig.width=12, fig.height=4}
ggplot(data=SummaryAllData4dig, aes(x=Period, y=Share_coinciding, group=Country, shape = Country, color=Country)) +
geom_point(aes(fill = Country), size=8) +
scale_shape_manual(values=c(21, 22, 24, 23)) +
xlab("Interval") +
ylab("Share of break-in specialisations (%)") +
theme_classic() +
geom_line(aes(color=Country), linetype = "dashed", size=1.5)+
scale_y_continuous(labels = scales::percent) +
scale_fill_manual(values = c("#1B9E77", "#D95F02", "#7570B3", "#E7298A")) +
scale_color_manual(values = c("#1B9E77", "#D95F02", "#7570B3", "#E7298A"))
```
## 2.2. Growth of AI Patents (Fig 1)
This section reproduces Figure 1 from the paper, which illustrates the dramatic growth in AI patenting since the 1970s. We use the raw AI patent data, aggregating the number of unique patent applications per country for each year. A **log-10** scale is used for the y-axis to accommodate the exponential increase in patent counts and allow for a clearer comparison of growth trajectories between the four focus countries, resulting in the figure seen below.
```{r, include=FALSE}
patents_AI_specific <- read.csv("other_files/IPCs_AI.csv", sep = ";", header = TRUE, dec=",")
patents_AI_specific <- patents_AI_specific[,c((1), (3:4))]
patents_AI_specific %<>% mutate(DistinctOwnerInf = !duplicated(appln_id)) %>% ungroup()
patents_AI_specific %<>% group_by(appln_id) %>%
mutate(DistinctpatentOffice = !duplicated(patent_office)) %>% ungroup()
patents_AI_specific %<>% group_by(appln_id) %>%
mutate(DistinctpatentOffice = n_distinct(patent_office, na.rm = T)) %>% ungroup()
table(patents_AI_specific$DistinctpatentOffice)
test<- patents_AI_specific[1,]
test$patent_office <- gsub("CN", "US", str_trim(test$patent_office))
patents_AI_specific2 <- rbind(patents_AI_specific, test)
patents_AI_specific2 %<>% group_by(appln_id) %>%
mutate(DistinctpatentOffice = n_distinct(patent_office, na.rm = T)) %>% ungroup()
patents_AI_specific2[patents_AI_specific2$appln_id == "475222998",]
table(patents_AI_specific2$DistinctpatentOffice)
#thus, there is no patent with inventors from distinct patent offices in our dataset;
patents_AI_specific_simplified <- patents_AI_specific[patents_AI_specific$DistinctOwnerInf == T,]
patents_AI_specific_simplified2 <- patents_AI_specific2[patents_AI_specific2$DistinctOwnerInf == T,]
patents_AI_specific_simplified_4<- patents_AI_specific_simplified[patents_AI_specific_simplified$patent_office == "CN" |
patents_AI_specific_simplified$patent_office == "US"|
patents_AI_specific_simplified$patent_office == "KR"|
patents_AI_specific_simplified$patent_office == "JP", ]
patents_AI_specific_simplified_4$patent_office <- gsub("US", "USA", str_trim(patents_AI_specific_simplified_4$patent_office))
patents_AI_specific_simplified_4$patent_office <- gsub("CN", "China", str_trim(patents_AI_specific_simplified_4$patent_office))
patents_AI_specific_simplified_4$patent_office <- gsub("JP", "Japan", str_trim(patents_AI_specific_simplified_4$patent_office))
patents_AI_specific_simplified_4$patent_office <- gsub("KR", "South Korea", str_trim(patents_AI_specific_simplified_4$patent_office))
table(patents_AI_specific_simplified_4$patent_office)
Data <- as.data.frame(table(patents_AI_specific_simplified_4$patent_office, patents_AI_specific_simplified_4$priority_year))
names(Data) <- c("Country", "Year", "Number_of_AI_patents")
Data$Year <- as.Date(paste(Data$Year, 1, 1, sep = "-")) # beginning of year
Data$Year <- as.Date(paste(Data$Year, 12, 31, sep = "-"))
Data$Year <- as.numeric(format(Data$Year, "%Y"))
Data$Period <- ifelse(Data$Year >= 1974 & Data$Year <= 1988, "First Period (1974-1988)",
ifelse(Data$Year > 1988 & Data$Year <= 2003, "Second Period (1989-2003)",
ifelse(Data$Year >= 2004 & Data$Year < 2019, "Third Period (2004-2018)", "No period")))
test <- Data[Data$Period != "No period",]
```
```{r, fig.width=12, fig.height=8}
ggplot(data=test, aes(x=Year, y=log10(Number_of_AI_patents), group=Country, colour=Country, shape=Country)) +
geom_line(size=1.2, aes(linetype=Country)) +
geom_point(size=4) + xlab("Year") + ylab("Number of new AI registers [Log10]") + theme_classic() +
scale_linetype_manual(values=c("twodash", "longdash", "solid", "solid")) +
scale_shape_manual(values=c(16, 15, 17, 18)) + theme(legend.position="bottom") +
theme(text = element_text(size = 15)) + scale_y_continuous(limits=c(0,4)) +
geom_vline(data=test, aes(xintercept=c(1988), colour=Period), linetype="dashed", size=1, color = "grey") +
geom_vline(data=test, aes(xintercept=c(2003), colour=Period), linetype="dashed", size=1, color = "grey") +
scale_x_continuous(breaks = c(1974, 1988, 2003, 2018), limits=c(1974, 2018)) + scale_color_brewer(palette="Dark2") +
annotate("rect", xmin = 1974, xmax=1988, ymin = 3.6, ymax = 4, alpha = .01, color = "black") +
annotate("text", x = 1981, y = 3.8, label = c("First Interval \n(1974-1988)"), size=4)+
annotate("rect", xmin = 1988, xmax=2003, ymin = 3.6, ymax = 4, alpha = .01, color = "black") +
annotate("text", x = 1996, y = 3.8, label = c("Second Interval \n(1989-2003)"), size=4) +
annotate("rect", xmin = 2003, xmax=2018, ymin = 3.6, ymax = 4, alpha = .01, color = "black") +
annotate("text", x = 2011, y = 3.8, label = c("Third Interval \n(2004-2018)"), size=4)
```
# 3. Robustness Checks: Permutation Analysis
To ensure that our findings are statistically robust and not merely the result of random chance, we conduct a permutation analysis. The core idea is to create a "null model" by generating thousands of randomized AI patent datasets. By comparing our actual results to the distribution of results from these random datasets, we can assess the statistical significance of our observations. This section details the creation of these permuted datasets and the subsequent recalculation of specialization metrics.
## 3.1. Permutate the AI dataset
The first step is to generate the randomized, or **permuted**, datasets. For each of the four focus countries and for each time interval, we follow a specific procedure:
1. Count the number of **actual** AI patents the country has in that interval.
2. Randomly select the **same number** of patents from that country's **entire pool** of patents (both AI and non-AI) for that interval.
3. Treat this random sample as the new, 'permuted' AI dataset for that country.
This process is repeated 1,000 times (note: `num_permutations` is set to 10 in this example for faster execution) to create 1,000 counterfactual scenarios where 'AI' patents are just random draws from a country's overall technological portfolio.
We begin by reloading the patent data for the first interval (1974-1988) to establish the pool from which random patents will be drawn.
```{r, include=FALSE}
#### Main Code
# Load required libraries
library(tidyverse)
library(magrittr)
library(tidygraph)
library(ggraph)
library(EconGeo)
library(data.table)
library(netrankr)
library(dplyr)
library(tidyr)
library(ggrepel)
library(scales)
library(patchwork)
library(RColorBrewer)
library(janitor) #also used in the clean_names() function
library(ggforce)
library(stringr)
library(openxlsx)
library(gridExtra) #for grid.arrange
library(readxl) #for reading the xlsx files
library(lmtest) #for LM analysis and robustness econometric test
library(sandwich)
library(stargazer) #for generating nice econometric tables
rm(list=ls())
#set the working directory to where you saved the R code:
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
#4.1.First part: Technological Spaces based on Technological field