-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.R
284 lines (268 loc) · 9.33 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
library(tidyverse)
library(ggpubr)
library(DescTools)
library(scales)
library(ppcor)
# remove
library(brms)
library(tidybayes)
library(ggdag)
theme_set(theme_bw())
options(scipen=999) # preventing scientific notation for glue
save <- TRUE # whether to save figures or not
# Initializing the model
S <- 6000 # number of senses
N <- round(S * 0.507) # number of types (calculated with ratio from WordNet)
mu <- 0.01 # replacement rate
k <- 0.02 # reuse rate
p <- 100000 # number of tokens to be sampled
t <- 300 # number of time-steps
# Running the model (this should be uncommented if the reader wants
# to run the model by himself)
# NB: the N/S ratio is scalled, but we should prob. scale
# it down according to the size of the corpus
# system2('python3',
# args = c('src/NeutralModel.py',
# str_glue('--N={N}'),
# str_glue('--S={S}'),
# str_glue('--mu={mu}'),
# str_glue('--k={k}'),
# str_glue('--p={p}'),
# str_glue('--t={t}'))
# )
# Reading the data
data <- read.csv(str_glue('data/model-data/model-N{N}_S{S}_p{p}.csv')) %>%
filter(extinct != 1) %>% # remove extinct words
group_by(step) %>% # group by time-step
mutate(relative_frequency = count / sum(count)) # add relative frequency
# Figure 1: Rank-frequency distribution of words in the last time-step
# of the model
log_sc <- data %>%
filter(step == t-1) %>%
mutate(rank=row_number(desc(relative_frequency))) %>%
ggplot(aes(x=rank, y=log(relative_frequency)))+
geom_point(alpha=0.5)+
xlab('Frequency rank')+
ylab(' Relative frequency\n (log-odds)')+
ggtitle('Log scale')+
theme(text = element_text(size = 20))+
scale_x_continuous(trans = log2_trans())
lin_sc <- data %>%
filter(step == t-1) %>%
mutate(rank=row_number(desc(relative_frequency))) %>%
ggplot(aes(x=rank, y=relative_frequency))+
geom_point(alpha=0.5)+
xlab('Frequency rank')+
ylab('Relative frequency')+
ggtitle('Linear scale')+
theme(text = element_text(size = 20))
ggarrange(log_sc, lin_sc,
widths = c(1, 1),
ncol = 1,
nrow =2,
align = "hv")+
theme(text = element_text(size = 20))
if (isTRUE(save)){
ggsave('figures/Fig1.pdf', width = 10, height = 6)
}
# Figure 2: The evolution of number of types and senses.
## Sums for each times-step
sum_step <- data %>%
filter(extinct != 1) %>%
group_by(step) %>%
summarize(step = step,
sum_m = sum(meanings),
n = n(),
sum_c = sum(count),
share = n/sum_m)
## Plotting
sum_step %>%
ggplot(aes(x=step, y=sum_m))+
geom_path(aes(x=step, y=sum_m), color='grey', size=2)+
geom_hline(yintercept = S, color='grey', size=2, alpha=0.5)+
geom_path(aes(x=step, y=n), color='blue', size=2)+
geom_hline(yintercept = N, color='blue', size=2, alpha=0.5)+
xlab('Time-step (t)')+
ylab('Count')+
scale_color_manual(values = colors)+
theme(text = element_text(size = 20))
if (isTRUE(save)){
ggsave('figures/Fig2.pdf', width = 10, height = 4)
}
# Figure 3: Age ~ number of meanings vs. frequency ~ number of meanings
freq_meanings <- data %>%
filter(step == t-1 & extinct != 1) %>%
mutate(age=age)%>%
ggplot(aes(x=age, y=meanings))+
geom_point(aes(color=relative_frequency), alpha=0.5)+
stat_cor(method = "spearman",
aes(label = after_stat(r.label)))+
xlab('Longevity')+
ylab('Number of meanings')+
labs(color='Frequency')+
theme(text = element_text(size = 20),
axis.text = element_text(size=10))+
scale_colour_gradient(low = "blue", high = "red")
age_meanings <- data %>%
filter(step == t-1 & extinct != 1) %>%
mutate(log_f=relative_frequency) %>%
ggplot(aes(x=log_f, y=meanings))+
geom_point(aes(color=age), alpha=0.5)+
stat_cor(method = "spearman",
aes(label = after_stat(r.label)))+
xlab('Relative frequency')+
ylab('Number of meanings')+
labs(color='Longevity')+
theme(text = element_text(size = 20),
axis.text= element_text(size=10))+
scale_colour_gradient(low = "blue", high = "red")+
scale_x_continuous(trans = log2_trans(),
labels = function(x) format(round(x, 3), scientific = TRUE))
ggarrange(freq_meanings, age_meanings,
widths = c(1, 1),
ncol = 2,
nrow =1,
align = "hv",
labels = c("A", "B"))+
theme(text = element_text(size = 20))
if (isTRUE(save)){
ggsave('figures/Fig3.png', width = 10, height = 6)
}
# Correlation coefficients
last_step <- data %>%
filter(step == t-1 & extinct != 1) %>%
mutate(z_age = (age - mean(age))/sd(age),
z_freq = (relative_frequency -
mean(relative_frequency))/sd(relative_frequency),
z_meanings = (meanings - mean(meanings))/sd(meanings))
## age~meaning
cor.test(x=last_step$meanings,
y=last_step$age,
method = 'spearman')
## longevity~frequency
cor.test(x=last_step$relative_frequency,
y=last_step$age,
method = 'spearman')
## controlling for longevity
pcor.test(x=last_step$meanings,
y=last_step$relative_frequency,
z=last_step$age,
method = 'spearman')
## frequency~meaning
cor.test(x=last_step$meanings,
y=last_step$relative_frequency,
method = 'spearman')
## controlling for frequency
pcor.test(x=last_step$meanings,
y=last_step$age,
z=last_step$relative_frequency,
method = 'spearman')
# Figure 4
sample_bd <- data %>%
filter(step != 0) %>%
group_by(id) %>%
mutate(birth=min(step), death=max(step),
frequency=max(relative_frequency)) %>%
dplyr::select(birth, death, age) %>%
arrange(desc(age))
# Figure 4: plotting the intervals
sample_bd[sample.int(10000:10000*30, 40),] %>%
arrange(desc(age)) %>%
ggplot()+
geom_point(aes(y=id, x=birth), color='red')+
geom_point(aes(y=id, x=death), color='blue')+
geom_segment(aes(x=birth, xend=death, y=id, yend=id),colour = "grey50")+
theme(axis.text.y=element_blank(), #remove x axis labels
axis.ticks.y=element_blank())+
xlab('Step')+
ylab('')+
geom_text(aes(x=320, y=id, label=age))
if (isTRUE(save)){
ggsave('figures/Fig4_int.png', width = 10, height = 6)
}
# Bayesian poisson regression
## Meanings ~ frequency + age
mod1 <- brm(meanings ~ 1 + z_freq + z_age, data = last_step, family = poisson)
summary(mod1)
### plots
p_m1 <- posterior_samples(mod1) %>%
dplyr::select(b_z_age, b_z_freq) %>%
rename('Longevity' = 'b_z_age',
' Relative\n frequency' = 'b_z_freq') %>%
gather() %>%
ggplot(aes(x = value, y = reorder(key, value))) +
geom_vline(xintercept = 0, color = "firebrick4", alpha = 3/10) +
stat_pointinterval(point_interval = mode_hdi, .width = .95,
size = 3/4, color = "firebrick4") +
theme_bw() +
theme(panel.grid = element_blank(),
panel.grid.major.y = element_line(color = alpha("firebrick4", 1/4),
linetype = 3),
axis.text.y = element_text(hjust = 0),
axis.ticks.y = element_blank())+
xlab('beta coefficient')+
ylab('')+
xlim(-0.2, 1)
## Meanings ~ frequency
mod2 <- brm(meanings ~ 1 + z_freq, data = last_step, family = poisson)
summary(mod2)
p_m2 <- posterior_samples(mod2) %>%
dplyr::select(b_z_freq) %>%
rename(' Relative\n frequency' = 'b_z_freq') %>%
gather() %>%
ggplot(aes(x = value, y = reorder(key, value))) +
geom_vline(xintercept = 0, color = "firebrick4", alpha = 3/10) +
stat_pointinterval(point_interval = mode_hdi, .width = .95,
size = 3/4, color = "firebrick4") +
theme_bw() +
theme(panel.grid = element_blank(),
panel.grid.major.y = element_line(color = alpha("firebrick4", 1/4),
linetype = 3),
axis.text.y = element_text(hjust = 0),
axis.ticks.y = element_blank())+
xlab('beta coefficient')+
ylab('')+
xlim(-0.2, 1)
## Meanings ~ age
mod3 <- brm(meanings ~ 1 + z_age, data = last_step, family = poisson)
summary(mod3)
### Plot
p_m3 <- posterior_samples(mod3) %>%
dplyr::select(b_z_age) %>%
rename('Longevity' = 'b_z_age') %>%
gather() %>%
ggplot(aes(x = value, y = reorder(key, value))) +
geom_vline(xintercept = 0, color = "firebrick4", alpha = 3/10) +
stat_pointinterval(point_interval = mode_hdi, .width = .95,
size = 3/4, color = "firebrick4") +
theme_bw() +
theme(panel.grid = element_blank(),
panel.grid.major.y = element_line(color = alpha("firebrick4", 1/4),
linetype = 3),
axis.text.y = element_text(hjust = 0),
axis.ticks.y = element_blank())+
xlab('beta coefficient')+
ylab('')+
xlim(-0.2, 1)
ggarrange(p_m1, p_m2, p_m3,
widths = c(1, 1),
ncol = 1,
nrow = 3,
align = "hv",
labels = c("A", "B", "C"))
if (isTRUE(save)){
ggsave('figures/Fig1_p.pdf', width = 10, height = 6)
}
# # Drawing a DAG (frequency as a confound)
# meaning_dag <- dagify(polysemy ~ longevity,
# longevity ~ frequency,
# polysemy ~ frequency,
# labels = c(
# 'polysemy' = 'Polysemy',
# 'longevity' = 'Longevity',
# 'frequency' = 'Frequency'
# ))
# ggdag(meaning_dag,
# text = FALSE,
# use_labels = "label")+
# theme_void()