-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_Data_Setup.Rmd
826 lines (588 loc) · 31 KB
/
01_Data_Setup.Rmd
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
---
title: "Data Preparation - Timing, Development and Diffusion of Work-Injury Laws Globally: Industrialization, Nation States and Institutions"
output: html_document
---
Nate Breznau
Felix Lanver
University of Bremen
We setup the data to analyze both the first law (often employer liability), and the first social insurance law (with the suffix *_socins*)
```{r setup}
# Clean up
rm(list = ls(all = T))
pacman::p_load('netdiffuseR','stargazer','tidyverse','dplyr','countrycode','sandwich','lmtest','Hmisc')
# stargazer 3 (define manually to make code faster)
# source("http://www.barkhof.uni-bremen.de/~mwindzio/modified_stargazer.R")
stargazer3 <- function(model, odds.ratios = F, origin_model = NULL, ...) {
if(!("list" %in% class(model))) model <- list(model)
if(!("list" %in% class(origin_model))) origin_model <- list(origin_model)
if (odds.ratios) {
coefOR2 <- lapply(model, function(x) exp(x[,1]))
#seOR2 <- lapply(model, function(x) exp(x[,1]) * exp(x[,2]))
p2 <- lapply(model, function(x) x[,4])
obs <- lapply(origin_model, function(x) nobs(x))
LL <- lapply(origin_model, function(x) round(logLik(x),3))
AIC <- lapply(origin_model, function(x) round(AIC(x),3))
stargazer(model, coef = coefOR2, p = p2,
star.char = c("+", "*", "**", "***"),
star.cutoffs = c(.1, .05, .01, .001),
notes = "+ p < 0.1; * p < 0.05; ** p < 0.01; *** p < 0.001",
notes.append = FALSE,
add.lines=list(c("Observations", unlist(obs)),
c("Log Likelihood", unlist(LL)),
c("Akaike Inf. Crit.", unlist(AIC))), ...)
} else {
stargazer(model, ...)
}
}
```
## Get Data
**Networkdata sources**
http://www.barkhof.uni-bremen.de/~mwindzio/edgelist_cultural_spheres.zip
http://www.barkhof.uni-bremen.de/~mwindzio/COW_trade_consolidated.zip
http://www.barkhof.uni-bremen.de/~mwindzio/trade_existence_dummy.csv
http://www.barkhof.uni-bremen.de/~mwindzio/capital_distance_edgelist.zip
http://www.barkhof.uni-bremen.de/~mwindzio/vdem_gdp_pc_INTERPOLATED_consolidated.csv
http://www.barkhof.uni-bremen.de/~mwindzio/vdem_regime__INTERPOLATED_consolidated.csv
This one stays zipped as GitHub has a 100 MB max filesize
http://www.barkhof.uni-bremen.de/~mwindzio/capital_distance_edgelist.zip
## 1. Network Data
```{r windzio_data, warning=F, message=F}
#--- 1. Networks here ---------------
#----- 1a) cultural spheres ---------
edgelist_cultural_spheres <- read.csv("data/edgelist_cultural_spheres.csv")
# head(edgelist_cultural_spheres)
dim(edgelist_cultural_spheres)
# - ids should be factor variables
edgelist_cultural_spheres$ego_id <- as.factor(edgelist_cultural_spheres$ego_id)
edgelist_cultural_spheres$alter_id <- as.factor(edgelist_cultural_spheres$alter_id)
# head(edgelist_cultural_spheres)
#----- 1b) trade network -----------------------------------
trade_dyadic_consolidated <- read.csv("data/COW_trade_consolidated.csv")
trade_dyadic <- trade_dyadic_consolidated
dim(trade_dyadic)
# head(trade_dyadic)
summary(trade_dyadic$smoothtotrade)
ddat <- trade_dyadic[is.na(trade_dyadic$smoothtotrade)==FALSE,]
summary(ddat$smoothtotrade)
d <- density(ddat$smoothtotrade)
plot(d)
polygon(d, col="red", border="blue")
names(trade_dyadic)[names(trade_dyadic)=="iso3_ego"] <- "ego_id"
names(trade_dyadic)[names(trade_dyadic)=="iso3_alter"] <- "alter_id"
# head(trade_dyadic)
# - ids should be factor variables
trade_dyadic$ego_id <- as.factor(trade_dyadic$ego_id)
trade_dyadic$alter_id <- as.factor(trade_dyadic$alter_id)
# - NA to 0 and log positive values
trade_dyadic$smoothtotrade[trade_dyadic$smoothtotrade < 0] <- 0
trade_dyadic$smoothtotrade[is.na(trade_dyadic$smoothtotrade)] <- 0
summary(trade_dyadic$smoothtotrade)
# head(trade_dyadic)
trade_dyadic$log_value <- ifelse(trade_dyadic$smoothtotrade > 0, log(trade_dyadic$smoothtotrade), 0)
summary(trade_dyadic$log_value)
# We realized that with only using a log function to smooth out the much skewed trade values we created negative values. That in itself would be not such a huge problem, however since we are dealing with network weights a negative weight does not only make no sense theoretically, it actually distorts the calculation of the exposure variable a lot. We end up with values outside the range of 0 – 1. To circumvent that we changed the script in line 109 in a way that we make sure to not end up with negative log_values:
trade_dyadic$log_value <- ifelse(trade_dyadic$smoothtotrade > 0, log(trade_dyadic$smoothtotrade + 1), 0)
summary(trade_dyadic$log_value)
# -- distribution is still extreme
d <- density(trade_dyadic$log_value)
plot(d)
polygon(d, col="red", border="blue")
trade_edgelist <- trade_dyadic
# -- we want this to be "independence" therefore, we use our own data to create this variable.
A02_workinj <- read.csv("data/A02_workinj.csv", header = TRUE)
# These data have missing cases, fix by using the empty frame
A02_workinj <- select(A02_workinj, country_name, cow_code, iso3, year, independence)
# Replace missing cow_codes
A02_workinj$cow2 <- countrycode(A02_workinj$iso3, 'iso3c','cown')
# Find mismatches
# A02_workinj2 <- subset(A02_workinj, A02_workinj$cow_code != A02_workinj$cow2)
# Fix
A02_workinj <- A02_workinj %>%
mutate(cow_code = ifelse(cow2==817, 816, cow_code),
cow_code = ifelse(is.na(cow_code), cow2, cow_code),
cow_code = ifelse(country_name == "Serbia", 345, cow_code),
iso3 = ifelse(cow_code == 816, "VNM", ifelse(cow_code == 345, "SRB", iso3)))
```
The GWIP presents us with a conundrum regarding treatment of the former Soveit Union states. Many were independent states on paper, but were under the centralized control of the Russian Communist party. Socialism is conceptually the same as social insurance when it comes to protections for workers. They do not pay into an insurance system; however, they are subjected to total state employment control. This means that their needs should be taken care of, as if they were in a social insurance system. Therefore, the first laws occur starting in 1922 for non-Russian Soviet states, as they form and/or join the USSR. However, the official independence of these states comes after the dissolution of the USSR in 1991. This means that we either recode the first laws to 1991 or record their independence dates to 1922, or before, depending on the case. We run our models with both versions of the recode but settle on the pre-USSR independence coding as these were national groups aiming to form states and joined the USSR as part of this process or through Russian force. This issue only applies to Belarus, Kyrgyzstan, Croatia and Slovenia.
```{r fixes, warning=F, message=F}
A02_workinj <- A02_workinj %>%
mutate(independence = ifelse(iso3 == "BLR" | iso3 == "KGZ", 1922, ifelse(iso3 == "HRV" | iso3 == "SVN", 1937, independence)))
existence <- select(A02_workinj, iso3, year, independence)
colnames(existence) <- c("iso3", "year", "existence")
# now make 0/1 for existence
existence_c <- existence %>%
group_by(iso3) %>%
dplyr::summarize(existence = max(existence, na.rm = T))
existence <- select(existence, -c(existence))
existence <- left_join(existence, existence_c, by = "iso3")
existence <- existence %>%
mutate(exist = ifelse(year < existence, 0, 1))
A02_workinj <- select(A02_workinj, country_name, cow_code, iso3, year)
```
Collier and Messick (1975) argue that percent labor force in agriculture is key to the introduction of work-injury law. This measure should be an indicator of industrialization/modernization. Therefore we import the Banks time-series data here. It is available for a smaller set of countries, but provides a robustness check.
```{r pct_ag}
pct_ag <- read_csv(file = "data/CNTSDATA_2020_Ag_only.csv")
pct_ag <- select(pct_ag, iso3, year, pct_ag_01)
# linear interpolate non-complete-missing series
pct_ag <- pct_ag %>%
group_by(iso3) %>%
filter(any(!is.na(pct_ag_01))) %>%
subset(iso3 != "ARE") %>% # have only one value
subset(iso3 != "BGD") %>%
subset(iso3 != "BWA") %>%
subset(iso3 != "CHN") %>%
subset(iso3 != "CMR") %>%
subset(iso3 != "FJI") %>%
subset(iso3 != "GAB") %>%
subset(iso3 != "GUY") %>%
subset(iso3 != "MWI") %>%
mutate(pct_ag_01_i = zoo::na.approx(pct_ag_01, na.rm = F),
pct_ag_01_i = zoo::na.fill(pct_ag_01_i, "extend")) %>%
ungroup()
# put together
A02_workinj <- left_join(A02_workinj, pct_ag, by = c("iso3", "year"))
```
```{r fixes2, warning=F, message=F}
#----- 1c) colonial ties from CEPI --
# - this is only a cross-sectional network !
# - we think about using a better one
# - if so, you will have to switch to "undirected=FALSE"
# - when creating the diffnet object. But don't care at the moment!
colonial_ties_edgelist <- read.csv("data/colonial_ties_edgelist.csv")
head(colonial_ties_edgelist)
names(colonial_ties_edgelist)[names(colonial_ties_edgelist)=="iso_o"] <- "ego_id"
names(colonial_ties_edgelist)[names(colonial_ties_edgelist)=="iso_d"] <- "alter_id"
# - ids should be factor variables
colonial_ties_edgelist$ego_id <- as.factor(colonial_ties_edgelist$ego_id)
colonial_ties_edgelist$alter_id <- as.factor(colonial_ties_edgelist$alter_id)
# head(colonial_ties_edgelist)
unique(colonial_ties_edgelist$ego_id)
unique(colonial_ties_edgelist$year)
table(colonial_ties_edgelist$colony)
# head(colonial_ties_edgelist)
#----------------------------------------------------------
#----- 1d) spatial distance/ prox. of capitals from CEPI --
#----------------------------------------------------------
# - this is only a cross-sectional network !
capital_distance_edgelist <- read.csv(unz("data/capital_distance_edgelist.zip", "capital_distance_edgelist.csv"))
# -- we use log of proximity
capital_distance_edgelist$log_dist <- log(capital_distance_edgelist$capdist)
log(7593.042) # => 8.934988, ok
capital_distance_edgelist$log_proximity <- 1/(capital_distance_edgelist$log_dist)
1/8.934988 # => 0.1119196, ok
# head(capital_distance_edgelist)
# capital_distance_edgelist <- subset(capital_distance_edgelist, select = -c(log_proximity) )
capital_distance_edgelist$proximity <- 1/(capital_distance_edgelist$capdist)
capital_distance_edgelist <- subset(capital_distance_edgelist, select=c(ego, alter, year,proximity))
names(capital_distance_edgelist)[names(capital_distance_edgelist)=="ego"] <- "ego_id"
names(capital_distance_edgelist)[names(capital_distance_edgelist)=="alter"] <- "alter_id"
# - ids should be factor variables
capital_distance_edgelist$ego_id <- as.factor(capital_distance_edgelist$ego_id)
capital_distance_edgelist$alter_id <- as.factor(capital_distance_edgelist$alter_id)
```
## 2. Explanatory Variables
```{r explanatory_vars}
#--- 2. explanatory variables -------
# --- we just use gdp and regime-type in this introduction
# GDP from vdem data
gdp <- read.csv("data/vdem_gdp_pc_INTERPOLATED_consolidated.csv", header = TRUE) # actor attributes
# head(gdp)
names(gdp)[names(gdp)=="filled_vdem_gdp_interpol"] <- "gdp" # it's just gdp
# regime type from vdem data
regime <- read.csv("data/vdem_regime__INTERPOLATED_consolidated.csv", header = TRUE) # actor attributes
# head(regime)
regime <- subset(regime, select=c(iso3, year,regime))
#-- merge here ----
covar_base <- subset(gdp, select=c(iso3, year,gdp))
# head(covar_base)
covar_base <- merge(covar_base, regime,by=c("iso3","year"))
# head(covar_base)
# table(covar_base$year) # - every year on board, for 164 countries
# table(covar_base$iso3) # - every country on board, for 131 years
```
## 3. Wor-Injury Policy
From GWIP ([Breznau and Lanver, 2020](https://doi.org/10.7910/DVN/IVKYIE))
Data are converted into longitudinal format (all years for all countries)
There were some discrepancies with the data so we update that which the team prepared for us
Fill in former Serbian republics at some point (basically as 'Serbia' in the GWIP), for now they need to be dropped
```{r sfb_vars}
# ----- Project A 02 -----------------
# Introduction of work regulations
# we already load this data above to create the 'existence' variable.
# A02_workinj <- read.csv("data/A02_workinj.csv", header = TRUE)
# unique(A02_workinj$cow_code) 165 OK!
# Make sure gwip data is up to date
gwip <- read.csv("data/gwip_v1.csv")
gwip <- select(gwip, -c(country_name))
A02_workinj <- left_join(A02_workinj, gwip, by = "cow_code")
# Drop missing countries
A02_workinj <- subset(A02_workinj, !is.na(labor_workinjury_firstlaw))
# Merge
A02_workinj <- A02_workinj %>%
mutate(labor_workinjury_firstlaw = ifelse(labor_workinjury_firstlaw < 1880, 1880, labor_workinjury_firstlaw), # Start with 1880 for FIRST LAW
labor_workinjury_first_socins = ifelse(labor_workinjury_first_socins < 1884, 1884, labor_workinjury_first_socins), # Start with 1884 for FIRST SOCINS
labor_workinjury_first_socins = ifelse(labor_workinjury_first_socins < labor_workinjury_firstlaw_bluecollar_fullcoverage, labor_workinjury_firstlaw_bluecollar_fullcoverage, labor_workinjury_first_socins), # ensure that the social insurance law being coded applies to all blue-collar sectors (measured with error, but best possible approximation combining information from these two variables)
labor_workinjury_first_socins = ifelse(labor_workinjury_first_socins > 2010, 2010, labor_workinjury_first_socins) # trim to end of series at 2010
)
covar_ <- data.frame(covar_base)
head(covar_)
A02_2 <- data.frame(subset( A02_workinj, select = c(iso3,labor_workinjury_firstlaw,labor_workinjury_first_socins,year,pct_ag_01_i) ))
# here we have to drop 13 countries because we do not have relevant work-injury data
covar_x <- merge(covar_, A02_2,by=c("iso3","year"))
# head(covar_)
# FIRST LAW
names(covar_)[names(covar_)=="labor_workinjury_firstlaw"] <- "toa_workinj_A02"
# FIRST SOC INSURANCE
names(covar_)[names(covar_)=="labor_workinjury_first_socins"] <- "toa_workinj_A02_socins"
# head(covar_)
covar_ <- merge(covar_, existence,by=c("iso3","year"))
# head(covar_)
covar <- covar_
dim(covar)
unique(covar$iso3)
unique(covar$year)
unique(edgelist_cultural_spheres$year)
# head(covar)
# - combined covar object for all explanatory variables
# - covar$gdp & covar$regime
```
## 4. netdiffuseR setup
keep in mind which networks we have
- >>edgelists<<:
- trade_edgelist => trade network
- edgelist_cultural_spheres => cultural spheres
- colonial_ties_edgelist => colonial ties
- capital_distance_edgelist => d = distance , prox. = 1/d
- make netdiffuse object: combine edgelist with covariate object
- begin with cultural spheres
### FIRST LAW
```{r 4_netdiff_culture, warning = F, message = F}
# cultural spheres --
dim(edgelist_cultural_spheres)
diffnet_culture <- edgelist_to_diffnet(
edgelist = edgelist_cultural_spheres[,1:2], # As usual, a two column dataset [,1:2]
w = edgelist_cultural_spheres$weight,
t0 = edgelist_cultural_spheres$year, # An integer vector with starting point of spell
t1 = edgelist_cultural_spheres$year, # An integer vector with the endpoint of spell
undirected = TRUE, # undirected network
dat = covar, # Attributes dataset
idvar = "iso3",
toavar = "toa_workinj_A02", # -- !! here just change time-of-adoption !! -- #
timevar = "year",
keep.isolates = TRUE # Keeping isolates (if there's any)
)
# diffnet_culture
# plot(diffnet_culture)
# summary(diffnet_culture)
# -------------------------------------------------------------------------
# -- 5. get the respective exposure variables and time-of-adoption (toa) f
# -- generate exposure and add to diffnet object
diffnet_culture[["lag_w_expo_culture"]] <- exposure(diffnet_culture, valued = T, lags = 1)#
diffnet_culture[["lag_expo_culture"]] <- exposure(diffnet_culture, valued = F, lags = 1)
diffnet_culture[["adopted"]] <- toa_mat(diffnet_culture)$cumadopt
table(data.frame(diffnet_culture)$adopted) # don't care about no. adopted here
summary(data.frame(diffnet_culture)$lag_w_expo_culture)
# head(data.frame(diffnet_culture)) # exposure is NA for 1st 164 cases => 1880
# tail(data.frame(diffnet_culture))
```
```{r 4_netdiff_colonial, warning = F, message = F}
# -------------------------
# - 4b) colonial ties -----
# -------------------------
# - here comes the core: creating the diffnet object
#head(colonial_ties_edgelist)
dim(colonial_ties_edgelist)
diffnet_colony <- edgelist_to_diffnet(
edgelist = colonial_ties_edgelist[,1:2], # As usual, a two column dataset [,1:2]
w = colonial_ties_edgelist$weight_decay_exp,
t0 = colonial_ties_edgelist$year, # An integer vector with starting point of spell
t1 = colonial_ties_edgelist$year, # An integer vector with the endpoint of spell
undirected = FALSE, # !!! change for >directed< network !!! => we provide it later
dat = covar, # Attributes dataset
idvar = "iso3",
toavar = "toa_workinj_A02", # -- !! here just change time-of-adoption !! -- #
timevar = "year",
keep.isolates = TRUE # Keeping isolates (if there's any)
)
# diffnet_colony
# plot(diffnet_colony)
# summary(diffnet_colony)
# -------------------------------------------------------------------------
# -- 5. get the respective exposure variables and time-of-adoption (toa)
# compute exposure: Netdiffuser automatically identifies whether the input is dynamic or not.
diffnet_colony[["w_expo_colony"]] <- exposure(diffnet_colony, valued = T)
diffnet_colony[["lag_expo_colony"]] <- exposure(diffnet_colony, valued = F)
diffnet_colony[["adopted"]] <- toa_mat(diffnet_colony)$cumadopt
diffnet_colony[["non_normalized_w_expo_colony"]] <- exposure(diffnet_colony, valued = T, normalized = FALSE)
# diffnet_colony
# summary(diffnet_colony)
summary(data.frame(diffnet_colony)$w_expo_colony)
```
```{r 4_netdiff_trade, warning = F, message = F}
# -------------------------
# - 4c) trade network -----
# -------------------------
# - here comes the core: creating the diffnet object
# head(trade_edgelist)
dim(trade_edgelist)
diffnet_trade <- edgelist_to_diffnet(
edgelist = trade_edgelist[,1:2], # As usual, a two column dataset [,1:2]
t0 = trade_edgelist$year, # An integer vector with starting point of spell
t1 = trade_edgelist$year, # An integer vector with the endpoint of spell
w = trade_edgelist$log_value,
undirected = TRUE, # undirected network
dat = covar, # Attributes dataset
idvar = "iso3",
toavar = "toa_workinj_A02", # -- !! here just change time-of-adoption !! -- #
timevar = "year",
keep.isolates = TRUE # Keeping isolates (if there's any)
)
# diffnet_trade
# plot(diffnet_trade)
# summary(diffnet_trade)
# -------------------------------------------------------------------------
# -- 5. get the respective exposure variables and time-of-adoption (toa)
# compute exposure: Netdiffuser automatically identifies whether the input is dynamic or not.
diffnet_trade[["lag_w_expo_trade"]] <- exposure(diffnet_trade, valued = T, lags = 1)
diffnet_trade[["lag_expo_trade"]] <- exposure(diffnet_trade, valued = F, lags = 1)
diffnet_trade[["adopted"]] <- toa_mat(diffnet_trade)$cumadopt
# head(data.frame(diffnet_trade))
# tail(data.frame(diffnet_trade))
summary(data.frame(diffnet_trade)$lag_w_expo_trade)
```
```{r 4_netdiff_spatial, warning = F, message = F}
# -----------------------------
# - 4d) spatial proximity -----
# -----------------------------
# - here comes the core: creating the diffnet object
# head(capital_distance_edgelist)
dim(capital_distance_edgelist)
diffnet_proximity <- edgelist_to_diffnet(
edgelist = capital_distance_edgelist[,1:2], # As usual, a two column dataset [,1:2]
t0 = capital_distance_edgelist$year, # An integer vector with starting point of spell
t1 = capital_distance_edgelist$year, # An integer vector with the endpoint of spell
w = capital_distance_edgelist$proximity,
undirected = TRUE, # undirected network
dat = covar, # Attributes dataset
idvar = "iso3",
toavar = "toa_workinj_A02", # -- !! here just change time-of-adoption !! -- #
timevar = "year",
keep.isolates = TRUE # Keeping isolates (if there's any)
)
# diffnet_proximity
# plot(diffnet_proximity)
# -----------------------------------------------------------------------
diffnet_proximity[["lag_w_expo_proximity"]] <- exposure(diffnet_proximity, valued = T, lags = 1)
diffnet_proximity[["lag_expo_proximity"]] <- exposure(diffnet_proximity, valued = F, lags = 1)
diffnet_proximity[["adopted"]] <- toa_mat(diffnet_proximity)$cumadopt
# diffnet_proximity
summary(data.frame(diffnet_proximity)$lag_expo_proximity)
# head(data.frame(diffnet_proximity))
```
```{r 4_culture_adoptiontable_socins, warning = F, message = F}
# - save first diffnet object as data as data frame
diff_data_culture <- as.data.frame(diffnet_culture)
diff_data_culture <- select(diff_data_culture, -c(toa_workinj_A02_socins))
table(diff_data_culture$adopted) # don't care about n. of cases
```
### SOCIAL INSURANCE
```{r 4_netdiff_culture_socins, warning = F, message = F}
# -------------------------
# - 4a) cultural spheres --
# -------------------------
diffnet_culture_socins <- edgelist_to_diffnet(
edgelist = edgelist_cultural_spheres[,1:2], # As usual, a two column dataset [,1:2]
w = edgelist_cultural_spheres$weight,
t0 = edgelist_cultural_spheres$year, # An integer vector with starting point of spell
t1 = edgelist_cultural_spheres$year, # An integer vector with the endpoint of spell
undirected = TRUE, # undirected network
dat = covar, # Attributes dataset
idvar = "iso3",
toavar = "toa_workinj_A02_socins",
timevar = "year",
keep.isolates = TRUE # Keeping isolates (if there's any)
)
# -------------------------------------------------------------------------
diffnet_culture_socins[["lag_w_expo_culture"]] <- exposure(diffnet_culture_socins, valued = T, lags = 1)#
diffnet_culture_socins[["lag_expo_culture"]] <- exposure(diffnet_culture_socins, valued = F, lags = 1)
diffnet_culture_socins[["adopted"]] <- toa_mat(diffnet_culture_socins)$cumadopt
table(data.frame(diffnet_culture_socins)$adopted) # don't care about no. adopted here
summary(data.frame(diffnet_culture_socins)$lag_w_expo_culture)
```
```{r 4_netdiff_colonial, warning = F, message = F}
# -------------------------
# - 4b) colonial ties -----
# -------------------------
diffnet_colony_socins <- edgelist_to_diffnet(
edgelist = colonial_ties_edgelist[,1:2], # As usual, a two column dataset [,1:2]
w = colonial_ties_edgelist$weight_decay_exp,
t0 = colonial_ties_edgelist$year, # An integer vector with starting point of spell
t1 = colonial_ties_edgelist$year, # An integer vector with the endpoint of spell
undirected = FALSE, # !!! change for >directed< network !!! => we provide it later
dat = covar, # Attributes dataset
idvar = "iso3",
toavar = "toa_workinj_A02_socins", # -- !! here just change time-of-adoption !! -- #
timevar = "year",
keep.isolates = TRUE # Keeping isolates (if there's any)
)
# -------------------------------------------------------------------------
diffnet_colony_socins[["w_expo_colony"]] <- exposure(diffnet_colony_socins, valued = T)
diffnet_colony_socins[["lag_expo_colony"]] <- exposure(diffnet_colony_socins, valued = F)
diffnet_colony_socins[["adopted"]] <- toa_mat(diffnet_colony_socins)$cumadopt
diffnet_colony_socins[["non_normalized_w_expo_colony"]] <- exposure(diffnet_colony_socins, valued = T, normalized = FALSE)
summary(data.frame(diffnet_colony_socins)$w_expo_colony)
```
```{r 4_netdiff_trade, warning = F, message = F}
# -------------------------
# - 4c) trade network -----
# -------------------------
diffnet_trade_socins <- edgelist_to_diffnet(
edgelist = trade_edgelist[,1:2], # As usual, a two column dataset [,1:2]
t0 = trade_edgelist$year, # An integer vector with starting point of spell
t1 = trade_edgelist$year, # An integer vector with the endpoint of spell
w = trade_edgelist$log_value,
undirected = TRUE, # undirected network
dat = covar, # Attributes dataset
idvar = "iso3",
toavar = "toa_workinj_A02_socins", # -- !! here just change time-of-adoption !! -- #
timevar = "year",
keep.isolates = TRUE # Keeping isolates (if there's any)
)
# -------------------------------------------------------------------------
diffnet_trade_socins[["lag_w_expo_trade"]] <- exposure(diffnet_trade_socins, valued = T, lags = 1)
diffnet_trade_socins[["lag_expo_trade"]] <- exposure(diffnet_trade_socins, valued = F, lags = 1)
diffnet_trade_socins[["adopted"]] <- toa_mat(diffnet_trade_socins)$cumadopt
summary(data.frame(diffnet_trade)$lag_w_expo_trade)
```
```{r 4_netdiff_spatial, warning = F, message = F}
# -----------------------------
# - 4d) spatial proximity -----
# -----------------------------
# - here comes the core: creating the diffnet object
# head(capital_distance_edgelist)
dim(capital_distance_edgelist)
diffnet_proximity_socins <- edgelist_to_diffnet(
edgelist = capital_distance_edgelist[,1:2], # As usual, a two column dataset [,1:2]
t0 = capital_distance_edgelist$year, # An integer vector with starting point of spell
t1 = capital_distance_edgelist$year, # An integer vector with the endpoint of spell
w = capital_distance_edgelist$proximity,
undirected = TRUE, # undirected network
dat = covar, # Attributes dataset
idvar = "iso3",
toavar = "toa_workinj_A02_socins", # -- !! here just change time-of-adoption !! -- #
timevar = "year",
keep.isolates = TRUE # Keeping isolates (if there's any)
)
# diffnet_proximity
# plot(diffnet_proximity)
# -----------------------------------------------------------------------
diffnet_proximity_socins[["lag_w_expo_proximity"]] <- exposure(diffnet_proximity_socins, valued = T, lags = 1)
diffnet_proximity_socins[["lag_expo_proximity"]] <- exposure(diffnet_proximity_socins, valued = F, lags = 1)
diffnet_proximity_socins[["adopted"]] <- toa_mat(diffnet_proximity_socins)$cumadopt
# diffnet_proximity
summary(data.frame(diffnet_proximity_socins)$lag_expo_proximity)
# head(data.frame(diffnet_proximity))
```
```{r 4_culture_adoptiontable_socins, warning = F, message = F}
# - save first diffnet object as data as data frame
diff_data_culture_socins <- as.data.frame(diffnet_culture_socins)
diff_data_culture_socins <- select(diff_data_culture_socins, -c(toa_workinj_A02))
table(diff_data_culture_socins$adopted) # don't care about n. of cases
```
## 5. Combine Data
### Time Dummies
```{r six}
# -----------------------------------------------------------------
# -----------------------------------------------------------------
# - 6. create process-time control, define the duration of episode
# -----------------------------------------------------------------
# FIRST LAW
diff_data_culture <- diff_data_culture %>%
mutate(t = per - 1880,
t0_22 = ifelse(t < 23, 1, 0), # stage one Europe, emergence (1880-1902)
t23_48 = ifelse(t >= 23 & t < 50, 1, 0), # 2 Russian/Soviet Revolution + WWI (1903-1928)
t49_74 = ifelse(t >= 50 & t < 75, 1, 0), # WW2 + Marshall Plan (1929-1954)
t75_99 = ifelse(t >= 75 & t < 100, 1, 0),
t100_130 = ifelse(t >= 100, 1, 0))
#SOC INS
diff_data_culture_socins <- diff_data_culture_socins %>%
mutate(t = per - 1880,
t0_22 = ifelse(t < 23, 1, 0), # stage one Europe, emergence (1880-1902)
t23_48 = ifelse(t >= 23 & t < 50, 1, 0), # 2 Russian/Soviet Revolution + WWI (1903-1928)
t49_74 = ifelse(t >= 50 & t < 75, 1, 0), # WW2 + Marshall Plan (1929-1955)
t75_99 = ifelse(t >= 75 & t < 100, 1, 0),
t100_130 = ifelse(t >= 100, 1, 0))
```
### Merge
```{r seven}
# -----------------------------------------------------------
diff_data <- diff_data_culture
diff_data_socins <- diff_data_culture_socins
diffnet_colony_df <- data.frame(diffnet_colony)
diffnet_trade_df <- data.frame(diffnet_trade)
diffnet_proximity_df <- data.frame(diffnet_proximity)
diffnet_culture_df <- data.frame(diffnet_culture)
diffnet_colony_df_socins <- data.frame(diffnet_colony_socins)
diffnet_trade_df_socins <- data.frame(diffnet_trade_socins)
diffnet_proximity_df_socins <- data.frame(diffnet_proximity_socins)
diffnet_culture_df_socins <- data.frame(diffnet_culture_socins)
# -------------------------------------------------------
# -------------- merge everything together ---------------
# -------------------------------------------------------
# --- merge id is now "id", automatically generated in netdiffuseR objects
# FIRST LAW
d <- subset( diffnet_colony_df, select = c(id,per,w_expo_colony, lag_expo_colony, non_normalized_w_expo_colony))
diff_data <- merge(diff_data, d,by=c("id","per"))
d <- subset( diffnet_trade_df, select = c(id,per,lag_w_expo_trade, lag_expo_trade) )
diff_data <- merge(diff_data, d,by=c("id","per"))
d <- subset( diffnet_proximity_df, select = c(id,per,lag_w_expo_proximity, lag_expo_proximity) )
diff_data <- merge(diff_data, d,by=c("id","per"))
# SOC INS
d <- subset( diffnet_colony_df_socins, select = c(id,per,w_expo_colony, lag_expo_colony, non_normalized_w_expo_colony) )
diff_data_socins <- merge(diff_data_socins, d,by=c("id","per"))
d <- subset( diffnet_trade_df_socins, select = c(id,per,lag_w_expo_trade, lag_expo_trade) )
diff_data_socins <- merge(diff_data_socins, d,by=c("id","per"))
d <- subset( diffnet_proximity_df_socins, select = c(id,per,lag_w_expo_proximity, lag_expo_proximity) )
diff_data_socins <- merge(diff_data_socins, d,by=c("id","per"))
# fix GDP
diff_data$gdp10000 <- diff_data$gdp/10000 # in 10,000 USD
diff_data_socins$gdp10000 <- diff_data_socins$gdp/10000 # in 10,000 USD
```
Original datafile source
http://www.barkhof.uni-bremen.de/~mwindzio/clusterID_n.csv
```{r cluster_id}
cluster_id <- read.csv("data/clusterID_n.csv", header = TRUE)
# head(cluster_id)
cluster_id <- cluster_id[,c(1,2,5)]
names(cluster_id) <- c("id", "per", "cluster_id")
diff_data <- merge(diff_data, cluster_id,by=c("id","per"))
diff_data_socins <- merge(diff_data_socins, cluster_id,by=c("id","per"))
```
## 6. Summary Stats
### Threshold Adoption comparison
```{r adoption plot, warning = F, message = F}
plot1 <- classify(diffnet_trade, include_censored = TRUE)
ftable(plot1)
plot2 <- classify(diffnet_colony, include_censored = TRUE)
ftable(plot2)
```
### Network Distributions
```{r summarydist}
# - distribution of network, range and "density/edge weight" => 164 NAs for 1880
summary(diff_data$lag_w_expo_culture)
#summary(diff_data$w_expo_colony)
#summary(diff_data$lag_w_expo_trade)
#summary(diff_data$lag_w_expo_proximity)
#summary(diff_data$adopted) # - create new for each project specific-toa
```
```{r summarydist_socins}
summary(diff_data_socins$lag_w_expo_culture)
```
## 7. Save Data
```{r savepoint}
# drop missing cases
diff_data <- diff_data[diff_data$existence != "-Inf",]
diff_data_socins <- diff_data_socins[diff_data_socins$existence != "-Inf",]
rm(trade_dyadic, trade_dyadic_consolidated, trade_edgelist, diff_data_culture, capital_distance_edgelist, d, ddat, edgelist_cultural_spheres, existence, gdp, regime, colonial_ties_edgelist)
save.image(file = "data/.Rdata")
```