-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.R
436 lines (417 loc) · 14.9 KB
/
analysis.R
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
library(modelr)
library(ggplot2)
library(dplyr)
library(lme4)
library(lmerTest)
library(sjPlot)
library(effects)
library(showtext)
library(ggeffects)
library(ggpubr)
library(lingtypology)
library(ggrepel)
library(MuMIn)
library(Cairo)
library(sysfonts)
library(broom.mixed)
library(sf)
library(viridis)
library(ggridges)
library(tibble)
library(modelbased)
# Setting the theme for ggplot
theme_set(theme_bw())
# Data loading
# !!!
# data <- read.csv('preprocessing/final_.csv')
data <- read.csv('data/final_correction.csv')
nrow(data)
# !!!
data <- data[data$ISO_language != 'heb',]
## Log-transforming relative frequency
data$Relative_frequency_l <- log(data$Relative_frequency)
## Correlation test
cor.test(data$Perimetric_complexity,
data$Compression,
method = "pearson")
nrow(data)
## SI table
data %>%
group_by(ISO_script) %>%
summarise(LengthScript = n(), across()) %>%
distinct(ISO_script, .keep_all = TRUE) %>%
mutate(Family = stringr::str_replace(Family, 'Mainland SE Asia',
'Mainland Southeast Asia')) %>%
arrange(ISO_language) %>%
select(ISO_language, ISO_script, Family, Sum_count, LengthScript, Source) %>%
write.csv(.,file = 'data/script_table.csv')
# Figure 1
dist <- data %>%
distinct(ISO_script, .keep_all = TRUE) %>%
mutate(Family = stringr::str_replace(Family, 'Mainland SE Asia',
'Mainland Southeast Asia'))
## Setting coordinates that could not be retrieved using lingtypology
dist$Language <- lang.iso(dist$ISO_language)
dist$lat <- lat.lang(dist$Language)
dist$lon <- long.lang(dist$Language)
dist[dist$ISO_language == 'chr',]$lat <- 35.8513
dist[dist$ISO_language == 'chr',]$lon <- -94.9878
dist[dist$ISO_language == 'cre',]$lat <- 66.578227
dist[dist$ISO_language == 'cre',]$lon <- -93.270105
dist %>%
ggplot(aes(x=lon, y=lat))+
coord_sf(xlim = c(-100, 120),ylim = c(0, 75), expand = TRUE)+
borders("world", colour=alpha("gray50", .2), fill=alpha("gray50", 0))+
geom_point(aes(color=Family))+
geom_text_repel(aes(label=ISO_script))+
theme_void()+
theme(legend.position = 'bottom')+
scale_color_viridis(discrete=TRUE)+
theme(text = element_text(size = 12))
ggsave('figures/fig1.pdf', dpi=300, width = 8, height = 6)
knitr::plot_crop('figures/fig1.pdf')
# Mixed-effects linear regression
## Perimetric complexity models (full and null)
model_full_p <- lmer(formula = Perimetric_complexity ~ 1 + Relative_frequency_l
+ (1 + Relative_frequency_l|ISO_script), data=data)
model_null_p <- lmer(formula =Perimetric_complexity ~ 1 + (1|ISO_script),
data=data)
## Comparing the AIC of the full model
abs(AIC(model_full_p) - AIC(model_null_p))
if (abs(AIC(model_full_p) - AIC(model_null_p)) > 2){
'Δ AIC > 2'
} else {
'Δ AIC < 2'
}
## Summary
summary(model_full_p)
## Confidence intervals
confint(model_full_p)
## Algorithmic complexity models (full and null)
model_full_c <- lmer(formula = Compression ~ 1 + Relative_frequency_l +
(1 + Relative_frequency_l|ISO_script), data=data)
model_null_c <- lmer(formula =Compression ~ 1 + (1|ISO_script), data=data)
## AIC comparison
abs(AIC(model_full_c) - AIC(model_null_c))
if (abs(AIC(model_full_c) - AIC(model_null_c)) > 2){
'Δ AIC > 2'
} else {
'Δ AIC < 2'
}
## Summary
summary(model_full_c)
## Confidence intervals
confint(model_full_c)
## Controlling for family: algorithmic complexity model
model_null_c_f <- lmer(formula =Compression ~ 1 + (1|Family:ISO_script), data=data)
model_full_c_f <- lmer(formula = Compression ~ 1 + Relative_frequency_l + (1 + Relative_frequency_l|Family:ISO_script), data=data)
## AIC comparison
abs(AIC(model_full_c_f) - AIC(model_null_c_f))
if (abs(AIC(model_full_c_f) - AIC(model_null_c_f)) > 2){
'Δ AIC > 2'
} else {
'Δ AIC < 2'
}
## Summary
summary(model_full_c_f)
## Confidence intervals
confint(model_full_c_f)
## Controlling for family: perimetric complexity model
model_null_p_f <- lmer(formula =Perimetric_complexity ~ 1 + (1|Family: ISO_script), data=data)
model_full_p_f <- lmer(formula = Perimetric_complexity ~ 1 + Relative_frequency_l + (1 + Relative_frequency_l|Family:ISO_script), data=data)
## AIC comparison
if (abs(AIC(model_full_p_f) - AIC(model_null_p_f)) > 2){
'Δ AIC > 2'
} else {
'Δ AIC < 2'
}
## Summary
confint(model_full_p_f)
## Confidencce intervals
summary(model_full_p)
# Figure 2
## Extracting model confidence interval (perimetric complexity)
p_sim <- data.frame(Relative_frequency_l = seq(min(data$Relative_frequency_l), max(data$Relative_frequency_l), .01)
)
fit.mat <- model.matrix(~ Relative_frequency_l, p_sim)
cis <- diag(fit.mat %*% tcrossprod(vcov(model_full_p), fit.mat))
p_sim$Perimetric_complexity_ <- predict(model_full_p, p_sim, re.form = NA)
p_sim$lwr <- p_sim$Perimetric_complexity-1.96*sqrt(cis)
p_sim$upr <- p_sim$Perimetric_complexity+1.96*sqrt(cis)
p_sim <- p_sim %>%
rename(Relative_frequency_l_ = Relative_frequency_l)
## Panel A with Thai examples
script <- 'Thai'
## Correct the character's encoding
thai <- data %>%
filter(ISO_script == script) %>%
mutate(textfile = as.character(textfile))
Encoding(stringi::stri_enc_toutf8(thai$textfile[1]))
## Plot the panel
p1 <- ggplot()+
theme(text=element_text(family="Arial Unicode MS"))+
geom_text_repel(data=thai, aes(x=Relative_frequency_l,
y=Perimetric_complexity,
label=stringi::stri_enc_toutf8(textfile)),
family="Arial Unicode MS",
nudge_x = 0.25,
nudge_y = 0.25,
size=7
)+
geom_point(data=data,
aes(x=Relative_frequency_l,
y=Perimetric_complexity),
alpha=0.1)+
geom_point(aes(x=Relative_frequency_l, y=Perimetric_complexity), data=thai)+
geom_ribbon(data=p_sim,
aes(x=Relative_frequency_l_,
y=Perimetric_complexity_,
ymin = lwr,
ymax = upr),
alpha = .3,
fill = "red")+
geom_line(data=p_sim,
aes(x=Relative_frequency_l_,
y=Perimetric_complexity_),
size = 1, color = "red")+
scale_y_continuous(breaks = seq(0, max(data$Perimetric_complexity),
by = 20))+
scale_x_continuous(breaks = seq(-15, 0,
by = 3))+
xlab('Frequency (log-transformed)')+
ylab('Perimetric complexity')+
theme(text = element_text(size = 20))
## Extracting model confidence interval (algorithmic complexity)
p_sim <- data.frame(Relative_frequency_l= seq(min(data$Relative_frequency_l),
max(data$Relative_frequency_l), .01)
)
fit.mat <- model.matrix(~ Relative_frequency_l, p_sim)
cis <- diag(fit.mat %*% tcrossprod(vcov(model_full_c), fit.mat))
p_sim$Compression <- predict(model_full_c, p_sim, re.form = NA)
p_sim$lwr <- p_sim$Compression-1.96*sqrt(cis)
p_sim$upr <- p_sim$Compression+1.96*sqrt(cis)
## Panel B
script <- 'Mlym'
mlym <- data %>%
filter(ISO_script == script) %>%
mutate(textfile = as.character(textfile))
p2 <- ggplot()+
theme(text=element_text(family="Arial Unicode MS"))+
geom_text_repel(data=mlym, aes(x=Relative_frequency_l,
y=Compression,
label=stringi::stri_enc_toutf8(textfile)),
family="Arial Unicode MS",
nudge_x = 0.25,
nudge_y = 0.25,
size=7
)+
geom_point(data=data,
aes(x=Relative_frequency_l,
y=Compression),
alpha=0.1)+
geom_point(aes(x=Relative_frequency_l, y=Compression), data=mlym)+
geom_ribbon(data=p_sim,
aes(x=Relative_frequency_l,
y=Compression,
ymin = lwr,
ymax = upr),
alpha = .3,
fill = "blue")+
geom_line(data=p_sim,
aes(x=Relative_frequency_l,
y=Compression),
size = 1, color = "blue")+
scale_y_continuous(breaks = seq(0, max(data$Compression),
by = 200))+
scale_x_continuous(breaks = seq(-15, 0,
by = 3))+
xlab('Frequency (log-transformed)')+
ylab('Algorithmic complexity')+
theme(text = element_text(size = 20))
## Combining two panels
ggarrange(p1, p2,
labels = c("A", "B"),
ncol = 2, nrow = 1)
ggsave('figures/fig2.pdf',
width = 16,
height = 8,
device = cairo_pdf)
# Figure 3
## Function to plot individual predictions for perimetric complexity
plot_individ_pred_p <- function(script){
data %>%
filter(ISO_script == script) %>%
ggplot(aes(x=exp(Relative_frequency_l), y=Perimetric_complexity))+
geom_line(data=predicted_values_p[predicted_values_p$ISO_script == script,],
aes(x=exp(Relative_frequency_l),
y=pred))+
theme(text=element_text(family="Arial Unicode MS"))+
geom_point(alpha=0.5)+
geom_text_repel(
aes(label=stringi::stri_enc_toutf8(textfile)),
family = 'Arial Unicode MS',
# nudge_x = 0.25, nudge_y = 0.25,
# check_overlap = T,
size=6
)
}
## Same function but for algorithmic complexity
plot_individ_pred_c <- function(script){
data %>%
filter(ISO_script == script) %>%
ggplot(aes(x=exp(Relative_frequency_l), y=Compression))+
geom_line(data=predicted_values_c[predicted_values_c$ISO_script == script,],
aes(x=exp(Relative_frequency_l),
y=pred))+
theme(text=element_text(family="Arial Unicode MS"))+
geom_point(alpha=0.5)+
geom_text_repel(
aes(label=stringi::stri_enc_toutf8(textfile)),
family = 'Arial Unicode MS',
# nudge_x = 0.25, nudge_y = 0.25,
# check_overlap = T,
size=6
)
}
## Panel A
p_1_ <- plot_individ_pred_p('Guru')+
theme(text = element_text(size = 14),
axis.text = element_text(size=14),
plot.margin = margin(l = 18, b=5, t=5, r=5))+
xlab('Frequency (log-scale)')+
ylab('Perimetric complexity')+
scale_x_log10(labels = scales::comma)
p_2_ <- predicted_values_p %>%
ggplot(aes(Relative_frequency_l, pred))+
geom_point(data=data,
aes(x=Relative_frequency_l,
y=Perimetric_complexity,
color=Family), alpha=0.7)+
geom_line()+
theme(legend.position = "none")+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank())+
facet_wrap(~ISO_script, ncol=5)+
scale_color_viridis(discrete=TRUE)+
theme(text = element_text(size = 20))+
theme(legend.position="none",
strip.background=element_rect(colour="black",
fill=NA))
c_fig3 <- ggarrange(p_1_, p_2_, heights = c(0.5, 1), ncol = 1, nrow = 2)
## Panel B
p_1_c <- plot_individ_pred_c('Guru')+
theme(text = element_text(size = 14),
axis.text = element_text(size=14),
plot.margin = margin(l = 18, b=5, t=5, r=5))+
xlab('Frequency (log-scale)')+
ylab('Algorithmic complexity')+
scale_x_log10(labels = scales::comma)
p_2_c <- predicted_values_c %>%
ggplot(aes(Relative_frequency_l, pred))+
geom_point(data=data,
aes(x=Relative_frequency_l,
y=Compression,
color=Family), alpha=0.7)+
geom_line()+
theme(legend.position = "none")+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank())+
facet_wrap(~ISO_script, ncol=5)+
scale_color_viridis(discrete=TRUE)+
theme(text = element_text(size = 20))+
theme(legend.position="none",
strip.background=element_rect(colour="black",
fill=NA))
p_fig3 <- ggarrange(p_1_c, p_2_c, heights = c(0.5, 1), ncol = 1, nrow = 2)
ggarrange(c_fig3, p_fig3,
ncol = 2,
nrow = 1,
labels = c("A", "B"))
ggsave('figures/fig3.pdf',
width = 16,
height = 8,
device = cairo_pdf)
# Figure 4
## Initial model
### Perimetric complexity
res_perim <- data.frame(coef(model_full_p)$ISO_script) %>%
rownames_to_column(var='ISO_script')
re_perim <- merge(unique(data[,c('Family', 'ISO_script')]), res_perim, by='ISO_script') %>%
ggplot(aes(y=ISO_script, x=Relative_frequency_l, color=Family))+
geom_point()+
geom_vline(xintercept = 0,
linetype="dashed",
color = "red",
size=0.5)+
xlim(-5, 1)+
xlab('Random slope values')+
ylab('Script name (ISO 15924 code)')+
theme(legend.position="none",
strip.background=element_rect(colour="black",
fill=NA))+
scale_color_viridis(discrete=TRUE)
### Algorithmic complexity
res_compr <- data.frame(coef(model_full_c)$ISO_script) %>%
rownames_to_column(var='ISO_script')
re_compr <- merge(unique(data[,c('Family', 'ISO_script')]), res_compr, by='ISO_script') %>%
ggplot(aes(y=ISO_script, x=Relative_frequency_l, color=Family))+
geom_point()+
geom_vline(xintercept = 0,
linetype="dashed",
color = "red",
size=0.5)+
xlim(-50, 10)+
xlab('Random slope values')+
ylab('Script name (ISO 15924 code)')+
theme(legend.position="none",
strip.background=element_rect(colour="black",
fill=NA))+
scale_color_viridis(discrete=TRUE)
ggarrange(re_perim, re_compr, ncol=2, nrow=1, labels = c("A", "B"))
ggsave('figures/fig4.pdf',
width = 10,
height = 5)
## Models with nesting
### Get coefficients from the nested model
nested_rs_p <- data.frame(coef(model_full_p_f)$`Family:ISO_script`) %>%
rownames_to_column(var='Family_ISO_script') %>%
tidyr::separate(Family_ISO_script, c("Family","ISO_script"),
sep = ":")
nested_rs_c <- data.frame(coef(model_full_c_f)$`Family:ISO_script`) %>%
rownames_to_column(var='Family_ISO_script') %>%
tidyr::separate(Family_ISO_script, c("Family","ISO_script"),
sep = ":")
### Plotting
re_perim_n <- nested_rs_p %>%
ggplot(aes(y=ISO_script, x=Relative_frequency_l))+
geom_point()+
geom_vline(xintercept = 0,
linetype="dashed",
color = "red",
size=0.5)+
xlim(-5, 1)+
xlab('Random slope values')+
ylab('Script name (ISO 15924 code)')
re_compr_n <- nested_rs_c %>%
ggplot(aes(y=ISO_script, x=Relative_frequency_l))+
geom_point()+
geom_vline(xintercept = 0,
linetype="dashed",
color = "red",
size=0.5)+
xlim(-50, 10)+
xlab('Random slope values')+
ylab('Script name (ISO 15924 code)')
ggarrange(re_perim_n, re_compr_n, ncol=2, nrow=1, labels = c("A", "B"))
ggsave('figures/fig4_n.pdf',
width = 10,
height = 5,
device = cairo_pdf)