-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.Rhistory
512 lines (512 loc) · 32.5 KB
/
.Rhistory
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
View(trade.dist.matrix)
trade.dist.matrix <- trade.dist.matrix[, -1]
View(trade.dist.matrix)
trade.dist.matrix <- t(trade.dist.matrix)
View(trade.dist.matrix)
trade.dist.matrix$country_codes <- rownames(trade.dist.matrix)
View(ruhlen2)
# Get geograhic data for each country (longitude and latitude)
geo <- read.csv("./input_data/geo.csv")
# Get 2008 levels of income for each country
mad08 <- read.csv("./input_data/maddison_subset_2008gdp.csv")
if (add.trade) {
# Trade distance matrix - direct input method (i.e. data is already cleaned up in Excel)
trade.dist.matrix <- read.csv("./input_data/trade_distance_matrix.csv")
colnames(trade.dist.matrix)[1] <- "country_code"
}
# Harmonize all datasets by selecting country rows that they all have
full <- merge(ruhlen2, mad, by = "country_code")
full <- merge(full, geo, by = "country_code")
if (add.trade) {
full <- merge(full, trade.dist.matrix, by = "country_code") ############ Trade
}
full <- select(full, country_code)
ruhlen2 <- merge(full, ruhlen2, by = 'country_code')
mad2 <- merge(full, mad, by = 'country_code')
geo2 <- merge(full, geo, by = 'country_code')
mad08 <- merge(full, mad08, by = 'country_code')
if (add.trade) {
trade.dist.matrix <- merge(full, trade.dist.matrix, by = 'country_code') ############ Trade
}
identical(as.vector(mad2$country_code), as.vector(ruhlen2$country_code)) # Check to make sure country codes match up exactly
identical(as.vector(geo2$country_code), as.vector(ruhlen2$country_code)) # Check to make sure country codes match up exactly
if (add.trade) {
identical(as.vector(trade.dist.matrix$country_code), as.vector(ruhlen2$country_code)) # Check to make sure country codes match up exactly
}
rownames(trade.dist.matrix) <- trade.dist.matrix[, 1]
trade.dist.matrix <- trade.dist.matrix[, -1]
trade.dist.matrix <- as.data.frame(t(trade.dist.matrix))
View(trade.dist.matrix)
trade.dist.matrix$country_codes <- rownames(trade.dist.matrix)
View(trade.dist.matrix)
trade.dist.matrix <- merge(full, trade.dist.matrix, by = 'country_code')
trade.dist.matrix$country_codes
# Get geograhic data for each country (longitude and latitude)
geo <- read.csv("./input_data/geo.csv")
# Get 2008 levels of income for each country
mad08 <- read.csv("./input_data/maddison_subset_2008gdp.csv")
if (add.trade) {
# Trade distance matrix - direct input method (i.e. data is already cleaned up in Excel)
trade.dist.matrix <- read.csv("./input_data/trade_distance_matrix.csv")
colnames(trade.dist.matrix)[1] <- "country_code"
}
# Harmonize all datasets by selecting country rows that they all have
full <- merge(ruhlen2, mad, by = "country_code")
full <- merge(full, geo, by = "country_code")
if (add.trade) {
full <- merge(full, trade.dist.matrix, by = "country_code") ############ Trade
}
full <- select(full, country_code)
ruhlen2 <- merge(full, ruhlen2, by = 'country_code')
mad2 <- merge(full, mad, by = 'country_code')
geo2 <- merge(full, geo, by = 'country_code')
mad08 <- merge(full, mad08, by = 'country_code')
if (add.trade) {
trade.dist.matrix <- merge(full, trade.dist.matrix, by = 'country_code') ############ Trade
}
identical(as.vector(mad2$country_code), as.vector(ruhlen2$country_code)) # Check to make sure country codes match up exactly
identical(as.vector(geo2$country_code), as.vector(ruhlen2$country_code)) # Check to make sure country codes match up exactly
if (add.trade) {
identical(as.vector(trade.dist.matrix$country_code), as.vector(ruhlen2$country_code)) # Check to make sure country codes match up exactly
}
rownames(trade.dist.matrix) <- trade.dist.matrix[, 1]
trade.dist.matrix <- trade.dist.matrix[, -1]
trade.dist.matrix <- as.data.frame(t(trade.dist.matrix))
trade.dist.matrix$country_code <- rownames(trade.dist.matrix)
trade.dist.matrix <- merge(full, trade.dist.matrix, by = 'country_code')
View(trade.dist.matrix)
rownames(trade.dist.matrix) <- trade.dist.matrix[, 1]
trade.dist.matrix <- trade.dist.matrix[, -1]
View(trade.dist.matrix)
isSymmetric(trade.dist.matrix)
isSymmetric(as.matrix(trade.dist.matrix))
class(trade.dist.matrix[5, 6])
class(trade.dist.matrix[5, 60])
mode(trade.dist.matrix) <- "numeric"
diag(trade.dist.matrix)
trade.dist.matrix[5, 5]
class(trade.dist.matrix[5, 5])
diag(trade.dist.matrix)
class(trade.dist.matrix)
View(trade.dist.matrix)
z <- as.dist(trade.dist.matrix)
trade.dist.matrix <- as.dist(trade.dist.matrix)
as.matrix(trade.dist.matrix)
diag(as.matrix(trade.dist.matrix))
View(as.matrix(trade.dist.matrix))
class(trade.dist.matrix)
ncf::mantel.test(as.matrix(lang.dist.matrix), as.matrix(trade.dist.matrix), resamp = 9999)
# Create growth distance matrix based on Maddison income data using Euclidean distance
mad3 <- mad2[, 3:ncol(mad2)] # Extract the growth numbers
rownames(mad3) <- mad2$country_code # Rename rows using country codes
mad3 <- t(scale(t(mad3))) # Standardize growth rates within each country so that mean = 0, SD = 1
growth.dist.matrix <- vegdist(mad3, "euclidean")
# Create language distance matrix for Ruhlen linguistic data using Jaccard distance
ruhlen3 <- ruhlen2[, 12:ncol(ruhlen2)] # Extract phoneme presence/absence for all 728 phonemes
rownames(ruhlen3) <- ruhlen2$country_code # Rename rows to ISO country codes
lang.dist.matrix <- vegdist(ruhlen3, "jaccard", binary = TRUE)
# Create geographic distance matrix for geographic data using great circle distance
geo3 <- geo2[ , 2:3]
geo.dist.matrix <- rdist.earth(geo3, geo3)
rownames(geo.dist.matrix) <- geo2[, 1]
colnames(geo.dist.matrix) <- geo2[, 1]
geo.dist.matrix <- log(as.dist(geo.dist.matrix)) # Log distance
# Create distance matrix for 2008 levels of income
mad08_2 <- as.matrix(mad08$X2008) # Extract the income numbers
rownames(mad08_2) <- mad08$country_code # Rename rows using country codes
mad08.dist.matrix <- log(vegdist(mad08_2, "euclidean")) # Log distance
ncf::mantel.test(as.matrix(lang.dist.matrix), as.matrix(trade.dist.matrix), resamp = 9999)
create.dist.matrices <- function (mad, add.trade=FALSE) {
# Get linguistic data from Ruhlen
ruhlen <- read.csv("./input_data/ruhlen.csv")
ruhlen <- rename(ruhlen, country_code = Country..ISO.Alpha.3.Code.) # Clean up column name for country code
# Get most spoken language for each country
ruhlen2 <- ruhlen %>%
group_by(country_code) %>%
arrange(desc(Population)) %>%
slice(1) %>% # In case there's more than one max language, just take the first one
ungroup
## Dealing with missing languages
# The real dominant languages of certain countries are missing
# E.g. For Australia, there's no Australian English and so the dominant language is (incorrectly) a small aboriginal language
# In this section, I manually substitute the phonemic data for some countries' languages with others that make sense
# E.g. Substitute Australia with the phonemic data from Britain's English
ruhlen2[which(ruhlen2$country_code == "AFG"), 12:739] <- ruhlen[which(ruhlen$ISO_A3 == "pbt-AFG"), 12:739] # Pashto (not Farsi) is the largest language in Afghanistan
ruhlen2[which(ruhlen2$country_code == "AUS"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "GBR"), 12:739] # Australia <- Britain
ruhlen2[which(ruhlen2$country_code == "ARG"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "COL"), 12:739] # Argentina <- Colombia
ruhlen2[which(ruhlen2$country_code == "BOL"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "COL"), 12:739] # Bolivia <- Colombia
ruhlen2[which(ruhlen2$country_code == "BWA"), 12:739] <- ruhlen[which(ruhlen$ISO_A3 == "tsn-ZAF"), 12:739] # Tswana is main language of Botswana (but categorized as a language of South Africa)
ruhlen2[which(ruhlen2$country_code == "CHE"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "DEU"), 12:739] # Switzerland <- Germany
ruhlen2[which(ruhlen2$country_code == "CHL"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "COL"), 12:739] # Chile <- Colombia
ruhlen2[which(ruhlen2$country_code == "GTM"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "CRI"), 12:739] # Guatemala <- Costa Rica
ruhlen2[which(ruhlen2$country_code == "HND"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "CRI"), 12:739] # Honduras <- Costa Rica
ruhlen2[which(ruhlen2$country_code == "NIC"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "CRI"), 12:739] # Nicaragua <- Costa Rica
ruhlen2[which(ruhlen2$country_code == "NZL"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "GBR"), 12:739] # New Zealand <- Britain
ruhlen2[which(ruhlen2$country_code == "PER"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "COL"), 12:739] # Peru <- Colombia
ruhlen2[which(ruhlen2$country_code == "PRY"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "COL"), 12:739] # Paraguay <- Colombia
ruhlen2[which(ruhlen2$country_code == "SDN"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "EGY"), 12:739] # Sudan <- Egypt
ruhlen2[which(ruhlen2$country_code == "SLV"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "CRI"), 12:739] # El Salvador <- Costa Rica
ruhlen2[which(ruhlen2$country_code == "VEN"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "COL"), 12:739] # Venezuela <- Colombia
# Get geograhic data for each country (longitude and latitude)
geo <- read.csv("./input_data/geo.csv")
# Get 2008 levels of income for each country
mad08 <- read.csv("./input_data/maddison_subset_2008gdp.csv")
if (add.trade) {
# Trade distance matrix - direct input method (i.e. data is already cleaned up in Excel)
trade.dist.matrix <- read.csv("./input_data/trade_distance_matrix.csv")
colnames(trade.dist.matrix)[1] <- "country_code"
}
# Harmonize all datasets by selecting country rows that they all have
full <- merge(ruhlen2, mad, by = "country_code")
full <- merge(full, geo, by = "country_code")
if (add.trade) {
full <- merge(full, trade.dist.matrix, by = "country_code") ############ Trade
}
full <- select(full, country_code)
ruhlen2 <- merge(full, ruhlen2, by = 'country_code')
mad2 <- merge(full, mad, by = 'country_code')
geo2 <- merge(full, geo, by = 'country_code')
mad08 <- merge(full, mad08, by = 'country_code')
if (add.trade) {
trade.dist.matrix <- merge(full, trade.dist.matrix, by = 'country_code') ############ Trade
}
identical(as.vector(mad2$country_code), as.vector(ruhlen2$country_code)) # Check to make sure country codes match up exactly
identical(as.vector(geo2$country_code), as.vector(ruhlen2$country_code)) # Check to make sure country codes match up exactly
if (add.trade) {
identical(as.vector(trade.dist.matrix$country_code), as.vector(ruhlen2$country_code)) # Check to make sure country codes match up exactly
}
if (add.trade) {
rownames(trade.dist.matrix) <- trade.dist.matrix[, 1]
trade.dist.matrix <- trade.dist.matrix[, -1]
trade.dist.matrix <- as.data.frame(t(trade.dist.matrix))
trade.dist.matrix$country_code <- rownames(trade.dist.matrix)
trade.dist.matrix <- merge(full, trade.dist.matrix, by = 'country_code')
rownames(trade.dist.matrix) <- trade.dist.matrix[, 1]
trade.dist.matrix <- trade.dist.matrix[, -1]
trade.dist.matrix <- as.dist(trade.dist.matrix)
}
# Create growth distance matrix based on Maddison income data using Euclidean distance
mad3 <- mad2[, 3:ncol(mad2)] # Extract the growth numbers
rownames(mad3) <- mad2$country_code # Rename rows using country codes
mad3 <- t(scale(t(mad3))) # Standardize growth rates within each country so that mean = 0, SD = 1
growth.dist.matrix <- vegdist(mad3, "euclidean")
# Create language distance matrix for Ruhlen linguistic data using Jaccard distance
ruhlen3 <- ruhlen2[, 12:ncol(ruhlen2)] # Extract phoneme presence/absence for all 728 phonemes
rownames(ruhlen3) <- ruhlen2$country_code # Rename rows to ISO country codes
lang.dist.matrix <- vegdist(ruhlen3, "jaccard", binary = TRUE)
# Create geographic distance matrix for geographic data using great circle distance
geo3 <- geo2[ , 2:3]
geo.dist.matrix <- rdist.earth(geo3, geo3)
rownames(geo.dist.matrix) <- geo2[, 1]
colnames(geo.dist.matrix) <- geo2[, 1]
geo.dist.matrix <- log(as.dist(geo.dist.matrix)) # Log distance
# Create distance matrix for 2008 levels of income
mad08_2 <- as.matrix(mad08$X2008) # Extract the income numbers
rownames(mad08_2) <- mad08$country_code # Rename rows using country codes
mad08.dist.matrix <- log(vegdist(mad08_2, "euclidean")) # Log distance
output <- list(growth.dist.matrix, lang.dist.matrix, geo.dist.matrix, mad08.dist.matrix)
return(output)
}
matrices <- create.dist.matrices(mad_subset5, add.trade=TRUE)
mad_subset5 <- read.csv("./input_data/maddison_subset5.csv") # 1950-2008, 137 countries, 5-year CAGR ----- PRIMARY OPTION
# These are for checking only
mad <- read.csv("./input_data/maddison_subset4a.csv") # 1950-2000, 138 countries, 10-year CAGR
mad <- read.csv("./input_data/maddison_subset6.csv") # random (check on Mantel test)
mad <- read.csv("./input_data/maddison_subset7.csv") # first half of countries are randomized (check on Mantel test)
# By region
mad_subset_europe <- read.csv("./input_data/maddison_subset_europe.csv") # 1950-2008, 23 countries, 5-year CAGR, Europe only
mad_subset_africa <- read.csv("./input_data/maddison_subset_africa.csv") # 1950-2008, 48 countries, 5-year CAGR, Sub-Saharan Africa only
mad_subset_eastasia <- read.csv("./input_data/maddison_subset_eastasia.csv") # 1950-2008, 15 countries, 5-year CAGR, East Asia only
mad_subset_latam <- read.csv("./input_data/maddison_subset_latam.csv") # 1950-2008, 23 countries, 5-year CAGR, Latin America only
mad_subset_anglo <- read.csv("./input_data/maddison_subset_anglo.csv") # 1950-2008, 6 countries, 5-year CAGR, Anglo only
mad_subset_oecd <- read.csv("./input_data/maddison_subset_oecd.csv") # 1950-2008, 29 countries, 5-year CAGR, OECD members only
mad_subset_opec <- read.csv("./input_data/maddison_subset_opec.csv") # 1950-2008, 13 countries, 5-year CAGR, OPEC members only
# By excluded region
mad_subset_exeurope <- read.csv("./input_data/maddison_subset_exeurope.csv") # 1950-2008, 115 countries, 5-year CAGR, excluding Europe
mad_subset_exafrica <- read.csv("./input_data/maddison_subset_exafrica.csv") # 1950-2008, 89 countries, 5-year CAGR, excluding Sub-Saharan Africa
mad_subset_exeastasia <- read.csv("./input_data/maddison_subset_exeastasia.csv") # 1950-2008, 122 countries, 5-year CAGR, excluding East Asia
mad_subset_exlatam <- read.csv("./input_data/maddison_subset_exlatam.csv") # 1950-2008, 114 countries, 5-year CAGR, excluding Latin America
mad_subset_exanglo <- read.csv("./input_data/maddison_subset_exanglo.csv") # 1950-2008, 131 countries, 5-year CAGR, excluding Anglophone countries (USA, UK, Australia, Ireland, Canada)
mad_subset_exoecd <- read.csv("./input_data/maddison_subset_exoecd.csv") # 1950-2008, 108 countries, 5-year CAGR, excluding OECD members
mad_subset_exopec <- read.csv("./input_data/maddison_subset_exopec.csv") # 1950-2008, 124 countries, 5-year CAGR, excluding OPEC members
matrices <- create.dist.matrices(mad_subset5, add.trade=TRUE)
create.dist.matrices <- function (mad, add.trade=FALSE) {
# Get linguistic data from Ruhlen
ruhlen <- read.csv("./input_data/ruhlen.csv")
ruhlen <- rename(ruhlen, country_code = Country..ISO.Alpha.3.Code.) # Clean up column name for country code
# Get most spoken language for each country
ruhlen2 <- ruhlen %>%
group_by(country_code) %>%
arrange(desc(Population)) %>%
slice(1) %>% # In case there's more than one max language, just take the first one
ungroup
## Dealing with missing languages
# The real dominant languages of certain countries are missing
# E.g. For Australia, there's no Australian English and so the dominant language is (incorrectly) a small aboriginal language
# In this section, I manually substitute the phonemic data for some countries' languages with others that make sense
# E.g. Substitute Australia with the phonemic data from Britain's English
ruhlen2[which(ruhlen2$country_code == "AFG"), 12:739] <- ruhlen[which(ruhlen$ISO_A3 == "pbt-AFG"), 12:739] # Pashto (not Farsi) is the largest language in Afghanistan
ruhlen2[which(ruhlen2$country_code == "AUS"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "GBR"), 12:739] # Australia <- Britain
ruhlen2[which(ruhlen2$country_code == "ARG"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "COL"), 12:739] # Argentina <- Colombia
ruhlen2[which(ruhlen2$country_code == "BOL"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "COL"), 12:739] # Bolivia <- Colombia
ruhlen2[which(ruhlen2$country_code == "BWA"), 12:739] <- ruhlen[which(ruhlen$ISO_A3 == "tsn-ZAF"), 12:739] # Tswana is main language of Botswana (but categorized as a language of South Africa)
ruhlen2[which(ruhlen2$country_code == "CHE"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "DEU"), 12:739] # Switzerland <- Germany
ruhlen2[which(ruhlen2$country_code == "CHL"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "COL"), 12:739] # Chile <- Colombia
ruhlen2[which(ruhlen2$country_code == "GTM"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "CRI"), 12:739] # Guatemala <- Costa Rica
ruhlen2[which(ruhlen2$country_code == "HND"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "CRI"), 12:739] # Honduras <- Costa Rica
ruhlen2[which(ruhlen2$country_code == "NIC"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "CRI"), 12:739] # Nicaragua <- Costa Rica
ruhlen2[which(ruhlen2$country_code == "NZL"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "GBR"), 12:739] # New Zealand <- Britain
ruhlen2[which(ruhlen2$country_code == "PER"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "COL"), 12:739] # Peru <- Colombia
ruhlen2[which(ruhlen2$country_code == "PRY"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "COL"), 12:739] # Paraguay <- Colombia
ruhlen2[which(ruhlen2$country_code == "SDN"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "EGY"), 12:739] # Sudan <- Egypt
ruhlen2[which(ruhlen2$country_code == "SLV"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "CRI"), 12:739] # El Salvador <- Costa Rica
ruhlen2[which(ruhlen2$country_code == "VEN"), 12:739] <- ruhlen2[which(ruhlen2$country_code == "COL"), 12:739] # Venezuela <- Colombia
# Get geograhic data for each country (longitude and latitude)
geo <- read.csv("./input_data/geo.csv")
# Get 2008 levels of income for each country
mad08 <- read.csv("./input_data/maddison_subset_2008gdp.csv")
if (add.trade) {
# Trade distance matrix - direct input method (i.e. data is already cleaned up in Excel)
trade.dist.matrix <- read.csv("./input_data/trade_distance_matrix.csv")
colnames(trade.dist.matrix)[1] <- "country_code"
}
# Harmonize all datasets by selecting country rows that they all have
full <- merge(ruhlen2, mad, by = "country_code")
full <- merge(full, geo, by = "country_code")
if (add.trade) {
full <- merge(full, trade.dist.matrix, by = "country_code") ############ Trade
}
full <- select(full, country_code)
ruhlen2 <- merge(full, ruhlen2, by = 'country_code')
mad2 <- merge(full, mad, by = 'country_code')
geo2 <- merge(full, geo, by = 'country_code')
mad08 <- merge(full, mad08, by = 'country_code')
if (add.trade) {
trade.dist.matrix <- merge(full, trade.dist.matrix, by = 'country_code') ############ Trade
}
identical(as.vector(mad2$country_code), as.vector(ruhlen2$country_code)) # Check to make sure country codes match up exactly
identical(as.vector(geo2$country_code), as.vector(ruhlen2$country_code)) # Check to make sure country codes match up exactly
if (add.trade) {
identical(as.vector(trade.dist.matrix$country_code), as.vector(ruhlen2$country_code)) # Check to make sure country codes match up exactly
}
if (add.trade) {
rownames(trade.dist.matrix) <- trade.dist.matrix[, 1]
trade.dist.matrix <- trade.dist.matrix[, -1]
trade.dist.matrix <- as.data.frame(t(trade.dist.matrix))
trade.dist.matrix$country_code <- rownames(trade.dist.matrix)
trade.dist.matrix <- merge(full, trade.dist.matrix, by = 'country_code')
rownames(trade.dist.matrix) <- trade.dist.matrix[, 1]
trade.dist.matrix <- trade.dist.matrix[, -1]
trade.dist.matrix <- as.dist(trade.dist.matrix)
}
# Create growth distance matrix based on Maddison income data using Euclidean distance
mad3 <- mad2[, 3:ncol(mad2)] # Extract the growth numbers
rownames(mad3) <- mad2$country_code # Rename rows using country codes
mad3 <- t(scale(t(mad3))) # Standardize growth rates within each country so that mean = 0, SD = 1
growth.dist.matrix <- vegdist(mad3, "euclidean")
# Create language distance matrix for Ruhlen linguistic data using Jaccard distance
ruhlen3 <- ruhlen2[, 12:ncol(ruhlen2)] # Extract phoneme presence/absence for all 728 phonemes
rownames(ruhlen3) <- ruhlen2$country_code # Rename rows to ISO country codes
lang.dist.matrix <- vegdist(ruhlen3, "jaccard", binary = TRUE)
# Create geographic distance matrix for geographic data using great circle distance
geo3 <- geo2[ , 2:3]
geo.dist.matrix <- rdist.earth(geo3, geo3)
rownames(geo.dist.matrix) <- geo2[, 1]
colnames(geo.dist.matrix) <- geo2[, 1]
geo.dist.matrix <- log(as.dist(geo.dist.matrix)) # Log distance
# Create distance matrix for 2008 levels of income
mad08_2 <- as.matrix(mad08$X2008) # Extract the income numbers
rownames(mad08_2) <- mad08$country_code # Rename rows using country codes
mad08.dist.matrix <- log(vegdist(mad08_2, "euclidean")) # Log distance
if (add.trade) {
output <- list(growth.dist.matrix, lang.dist.matrix, geo.dist.matrix, mad08.dist.matrix, trade.dist.matrix)
} else {
output <- list(growth.dist.matrix, lang.dist.matrix, geo.dist.matrix, mad08.dist.matrix)
}
return(output)
}
matrices <- create.dist.matrices(mad_subset5, add.trade=TRUE)
matrices.w.trade <- create.dist.matrices(mad_subset5, add.trade=TRUE)
ncf::mantel.test(as.matrix(matrices.w.trade[[1]]), as.matrix(matrices.w.trade[[5]]), resamp = 9999)
ncf::partial.mantel.test(as.matrix(matrices.w.trade[[1]]), as.matrix(matrices.w.trade[[2]], as.matrix(matrices.w.trade[[5]]), resamp = 9999)
ncf::partial.mantel.test(as.matrix(matrices.w.trade[[1]]), as.matrix(matrices.w.trade[[2]]), as.matrix(matrices.w.trade[[5]]), resamp = 9999)
ncf::partial.mantel.test(as.matrix(matrices.w.trade[[1]]), as.matrix(matrices.w.trade[[2]]), as.matrix(matrices.w.trade[[5]]), resamp = 9999)
matrices.w.trade <- create.dist.matrices(mad_subset5, add.trade=TRUE)
ncf::partial.mantel.test(as.matrix(matrices.w.trade[[1]]), as.matrix(matrices.w.trade[[2]]), as.matrix(matrices.w.trade[[5]]), resamp = 9999)
ncf::partial.mantel.test(as.matrix(matrices.w.trade[[1]]), as.matrix(matrices.w.trade[[2]]), as.matrix(matrices.w.trade[[3]]), as.matrix(matrices.w.trade[[4]]), as.matrix(matrices.w.trade[[5]]), resamp = 9999)
View(as.matrix(matrices.w.trade[[2]]))
complete.cases(as.matrix(matrices.w.trade[[2]]))
complete.cases(as.matrix(matrices.w.trade[[5]]))
which(is.na(as.matrix(matrices.w.trade[[5]])))
?n()
n(which(is.na(as.matrix(matrices.w.trade[[5]]))))
length(which(is.na(as.matrix(matrices.w.trade[[5]]))))
nrow(as.matrix(matrices.w.trade[[5]]))
ncol(as.matrix(matrices.w.trade[[5]]))
sapply(as.matrix(matrices.w.trade[[5]])), is.na)
sapply(as.matrix(matrices.w.trade[[5]]), is.na)
length(sapply(as.matrix(matrices.w.trade[[5]]), is.na))
length(as.matrix(matrices.w.trade[[5]]))
sumif(sapply(as.matrix(matrices.w.trade[[5]]), is.na), TRUE)
sum(sapply(as.matrix(matrices.w.trade[[5]]), is.na))
trade.modified <- as.matrix(matrices.w.trade[[5]])
upper.tri(trade.modified)
trade.modified[upper.tri(trade.modified)] <- NA
length(sapply(trade.modified, is.na))
sum(sapply(trade.modified, is.na))
View(trade.modified)
trade.modified <- as.matrix(matrices.w.trade[[5]])
trade.modified[upper.tri(trade.modified, diag=TRUE)] <- NA
View(trade.modified)
sum(sapply(trade.modified, is.na))
sum(sapply(trade.modified, !is.na))
which( !is.na(trade.modified), arr.ind=TRUE)
n(which( !is.na(trade.modified), arr.ind=TRUE))
length(which( !is.na(trade.modified), arr.ind=TRUE))
sum(sapply(trade.modified, is.na))
write.csv(as.matrix(matrices.w.trade[[5]]), "./output_tables/trade_dist_matrix2.csv")
trade.modified <- as.matrix(matrices.w.trade[[5]])
trade.modified[upper.tri(trade.modified, diag=TRUE)] <- NA
write.csv(trade.modified), "./output_tables/trade_dist_matrix2.csv")
write.csv(trade.modified), "./output_tables/trade_dist_matrix2.csv")
trade.modified <- as.matrix(matrices.w.trade[[5]])
trade.modified[upper.tri(trade.modified, diag=TRUE)] <- NA
View(trade.modified)
write.csv(trade.modified), "./output_tables/trade_dist_matrix2.csv")
write.csv(as.matrix(trade.modified)), "./output_tables/trade_dist_matrix2.csv")
write.csv(as.matrix(trade.modified), "./output_tables/trade_dist_matrix2.csv")
ncf::partial.mantel.test(as.matrix(matrices.w.trade[[1]]), as.matrix(matrices.w.trade[[2]]), as.matrix(matrices.w.trade[[5]]), resamp = 99999)
ncf.output <- data.frame(matrix(nrow=4))
ncf.output <- ncf::partial.mantel.test(as.matrix(matrices.w.trade[[1]]), as.matrix(matrices.w.trade[[2]]), as.matrix(matrices.w.trade[[5]]), resamp = 9999)
ncf.output$MantelR
ncf.output$MantelR[1]
ncf.output$p
ncf.output$p[1]
ncf1 <- paste0(formatC(round(ncf.output$MantelR[1], digits = 3), digits = 3, format = "f"), " (",format(ncf.output$p[1], scientific=TRUE, digits = 2),")")
ncf1
ncf1 <- paste0(formatC(round(ncf.output$MantelR[1], digits = 3), digits = 3, format = "f"), " (",format(ncf.output$p[1], scientific=TRUE, digits = 2),")")
ncf2 <- paste0(formatC(round(ncf.output$MantelR[2], digits = 3), digits = 3, format = "f"), " (",format(ncf.output$p[2], scientific=TRUE, digits = 2),")")
ncf3 <- paste0(formatC(round(ncf.output$MantelR[3], digits = 3), digits = 3, format = "f"), " (",format(ncf.output$p[3], scientific=TRUE, digits = 2),")")
ncf4 <- paste0(formatC(round(ncf.output$MantelR[4], digits = 3), digits = 3, format = "f"), " (",format(ncf.output$p[4], scientific=TRUE, digits = 2),")")
ncf.formatted <- data.frame(ncf1, ncf2, ncf3, ncf4)
View(ncf.formatted)
colnames(ncf.formatted) <- c("Growth ~ Linguistic", "Growth ~ Trade", "Linguistic ~ Trade", "Growth ~ Linguistic | Trade")
View(trade.modified)
View(ncf.formatted)
xtable(ncf.formatted)
print(xtable(ncf.formatted), include.rownames=FALSE)
colnames(ncf.formatted) <- c("Growth-Linguistic", "Growth-Trade", "Linguistic-Trade", "Growth-Linguistic (Control: Trade)")
print(xtable(ncf.formatted), include.rownames=FALSE)
print(xtable(ncf.formatted), include.rownames=FALSE, align = c(rep("p{2.3cm}", 4)), type='latex', sanitize.text.function=identity)
print(xtable(ncf.formatted, align = c(rep("p{2.3cm}", 4)), type='latex', sanitize.text.function=identity), include.rownames=FALSE)
print(xtable(ncf.formatted, align = c(rep("p{2.3cm}", 5)), type='latex', sanitize.text.function=identity), include.rownames=FALSE)
print(xtable(ncf.formatted, align = c(rep("p{3cm}", 5)), type='latex', sanitize.text.function=identity), include.rownames=FALSE)
ncf.output <- ncf::partial.mantel.test(as.matrix(matrices.w.trade[[1]]), as.matrix(matrices.w.trade[[2]]), as.matrix(matrices.w.trade[[5]]), resamp = 99999)
ncf1 <- paste0(formatC(round(ncf.output$MantelR[1], digits = 3), digits = 3, format = "f"), " (",format(ncf.output$p[1], scientific=TRUE, digits = 2),")")
ncf2 <- paste0(formatC(round(ncf.output$MantelR[2], digits = 3), digits = 3, format = "f"), " (",format(ncf.output$p[2], scientific=TRUE, digits = 2),")")
ncf3 <- paste0(formatC(round(ncf.output$MantelR[3], digits = 3), digits = 3, format = "f"), " (",format(ncf.output$p[3], scientific=TRUE, digits = 2),")")
ncf4 <- paste0(formatC(round(ncf.output$MantelR[4], digits = 3), digits = 3, format = "f"), " (",format(ncf.output$p[4], scientific=TRUE, digits = 2),")")
ncf.formatted <- data.frame(ncf1, ncf2, ncf3, ncf4)
colnames(ncf.formatted) <- c("Growth-Linguistic", "Growth-Trade", "Linguistic-Trade", "Growth-Linguistic \\hspace{20 mm} (Control: Trade)")
print(xtable(ncf.formatted, align = c(rep("p{3cm}", 5)), type='latex', sanitize.text.function=identity), include.rownames=FALSE)
ncf1 <- paste0(formatC(round(ncf.output$MantelR[1], digits = 3), digits = 3, format = "f"), " (",format(ncf.output$p[1], scientific=TRUE, digits = 2),")")
ncf2 <- paste0(formatC(round(ncf.output$MantelR[2], digits = 3), digits = 3, format = "f"), " (",format(ncf.output$p[2], scientific=TRUE, digits = 2),")")
ncf3 <- paste0(formatC(round(ncf.output$MantelR[3], digits = 3), digits = 3, format = "f"), " (",format(ncf.output$p[3], scientific=TRUE, digits = 2),")")
ncf4 <- paste0(formatC(round(ncf.output$MantelR[4], digits = 3), digits = 3, format = "f"), " (",format(ncf.output$p[4], scientific=TRUE, digits = 2),")")
ncf.formatted <- data.frame(ncf1, ncf2, ncf3, ncf4)
View(ncf.formatted)
colnames(ncf.formatted) <- c("Growth-Linguistic", "Growth-Trade", "Linguistic-Trade", "Growth-Linguistic \\hspace{20 mm} (Control: Trade)")
View(ncf.formatted)
print(xtable(ncf.formatted, align = c(rep("p{3cm}", 5)), type='latex', sanitize.text.function=identity), include.rownames=FALSE)
colnames(ncf.formatted) <- c("Growth-Linguistic", "Growth-Trade", "Linguistic-Trade", "Growth-Linguistic (Control: Trade)")
print(xtable(ncf.formatted, align = c(rep("p{3cm}", 5)), type='latex', sanitize.text.function=identity), include.rownames=FALSE)
colnames(ncf.formatted) <- c("(1) Growth-Linguistic", "(2) Growth-Trade", "(3) Linguistic-Trade", "(4) Growth-Linguistic (Control: Trade)")
print(xtable(ncf.formatted, align = c(rep("p{3cm}", 5)), type='latex', sanitize.text.function=identity), include.rownames=FALSE)
print(xtable(ncf.formatted, align = c(rep("p{3.1cm}", 5)), type='latex', sanitize.text.function=identity), include.rownames=FALSE)
print(xtable(ncf.formatted, align = c(rep("p{3.3cm}", 5)), type='latex', sanitize.text.function=identity), include.rownames=FALSE)
print(xtable(ncf.formatted, align = c(rep("p{3.5cm}", 5)), type='latex', sanitize.text.function=identity), include.rownames=FALSE)
matrices <- create.dist.matrices(mad_subset5)
correlogram1 <- mgram(matrices[[1]], matrices[[3]])
plot(correlogram1)
correlogram1 <- mgram(matrices[[1]], matrices[[3]], breaks=5)
plot(correlogram1)
correlogram1 <- mgram(matrices[[1]], matrices[[3]], nclass=5)
plot(correlogram1)
correlogram1 <- mgram(matrices[[1]], matrices[[3]], nclass=10)
plot(correlogram1)
View(matrices[[1]])
View(as.matrix(matrices[[1]]))
correlogram1
correlogram1 <- mgram(matrices[[1]], matrices[[3]], nclass=20)
plot(correlogram1)
correlogram1
max(as.matrixmatrices[[3]]))
max(as.matrixmatrices[[3]])
max(as.matrix(matrices[[3]]))
correlogram1 <- mgram(matrices[[1]], matrices[[3]], breaks=c(5, 6, 7, 8, 9))
plot(correlogram1)
correlogram1
correlogram1 <- mgram(matrices[[1]], matrices[[3]], breaks=c(1:9))
plot(correlogram1)
matrices <- create.dist.matrices(mad_subset5)
growth.dist.matrix <- matrices[[1]]
lang.dist.matrix <- matrices[[2]]
geo.dist.matrix <- matrices[[3]]
mad08.dist.matrix <- matrices[[4]]
growth.pairs <- convert.dist.matrix(growth.dist.matrix)
geo.pairs <- convert.dist.matrix(geo.dist.matrix)
growth.pairs2 <- growth.pairs
growth.pairs2 <- transmute(growth.pairs2, pairs = paste(as.character(country1), as.character(country2)), value)
geo.pairs2 <- geo.pairs
geo.pairs2 <- transmute(geo.pairs2, pairs = paste(as.character(country1), as.character(country2)), value)
merged.pairs <- merge(growth.pairs2, geo.pairs2, by = "pairs")
colnames(merged.pairs) <- c("pair", "growth", "geo")
ggplot(merged.pairs, aes(growth, geo)) +
geom_text(aes(label=pair)) +
geom_smooth(method = "lm", se = FALSE)
convert.dist.matrix <- function(dist.matrix) {
dist.matrix <- as.matrix(dist.matrix)
dist.matrix[upper.tri(dist.matrix, diag=TRUE)] <- NA
df <- melt(dist.matrix, varnames = c("country1", "country2"))
df <- na.omit(df)
return(df)
}
growth.pairs <- convert.dist.matrix(growth.dist.matrix)
geo.pairs <- convert.dist.matrix(geo.dist.matrix)
growth.pairs2 <- growth.pairs
growth.pairs2 <- transmute(growth.pairs2, pairs = paste(as.character(country1), as.character(country2)), value)
geo.pairs2 <- geo.pairs
geo.pairs2 <- transmute(geo.pairs2, pairs = paste(as.character(country1), as.character(country2)), value)
merged.pairs <- merge(growth.pairs2, geo.pairs2, by = "pairs")
colnames(merged.pairs) <- c("pair", "growth", "geo")
ggplot(merged.pairs, aes(growth, geo)) +
geom_text(aes(label=pair)) +
geom_smooth(method = "lm", se = FALSE)
View(merged.pairs)
View(arrange(merged.pairs, growth))
growth.pairs <- convert.dist.matrix(growth.dist.matrix)
lang.pairs <- convert.dist.matrix(lang.dist.matrix)
geo.pairs <- convert.dist.matrix(geo.dist.matrix)
growth.pairs2 <- growth.pairs
growth.dist.matrix <- matrices[[1]]
lang.dist.matrix <- matrices[[2]]
geo.dist.matrix <- matrices[[3]]
mad08.dist.matrix <- matrices[[4]]
# Compare growth and geo
growth.pairs <- convert.dist.matrix(growth.dist.matrix)
lang.pairs <- convert.dist.matrix(lang.dist.matrix)
geo.pairs <- convert.dist.matrix(geo.dist.matrix)
growth.pairs2 <- growth.pairs
growth.pairs2 <- transmute(growth.pairs2, pairs = paste(as.character(country1), as.character(country2)), value)
lang.pairs2 <- lang.pairs
lang.pairs2 <- transmute(lang.pairs2, pairs = paste(as.character(country1), as.character(country2)), value)
geo.pairs2 <- geo.pairs
geo.pairs2 <- transmute(geo.pairs2, pairs = paste(as.character(country1), as.character(country2)), value)
merged.pairs <- merge(growth.pairs2, lang.pairs2, geo.pairs2, by = "pairs")
colnames(merged.pairs) <- c("pair", "growth", "lang", "geo")
View(arrange(merged.pairs, growth))
View(lang.pairs2)
merged.pairs <- merge(growth.pairs2, lang.pairs2, by = "pairs")
merged.pairs <- merge(merged.pairs, geo.pairs2, by = "pairs")
colnames(merged.pairs) <- c("pair", "growth", "lang", "geo")
View(arrange(merged.pairs, growth))