-
Notifications
You must be signed in to change notification settings - Fork 0
/
F_data_and_fit.R
340 lines (264 loc) · 11.4 KB
/
F_data_and_fit.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
library(tidyverse)
library(data.table)
library(tidyfast)
library(tidytable)
library(latex2exp)
library(arrow)
library(readxl)
library(patchwork)
library(nlstools) # for fit
# functions
source(file = "functions.R")
# formula of fit
formula_F_asymetric <- as.formula(Fresult ~ ps * ( (N0_N^(1-alpha)) * (1-N0_N) + ( (1-N0_N)^(1+alpha) ) * N0_N ) +
(1 - ps) * pd * N0_N^(-alpha) * (1/N) )
#### System of cities ####
tradeve <- read_excel(path = "data/TRADEVE_UrbanAreas_Data.xlsx", sheet = "UrbanAreas_Data")
# creation of ranking by countries
dk_countries_tradeve <- tradeve %>%
group_by(Country) %>%
mutate(rang_1961 = rank(desc(Pop_1961)),
rang_1971 = rank(desc(Pop_1971)),
rang_1981 = rank(desc(Pop_1981)),
rang_1991 = rank(desc(Pop_1991)),
rang_2001 = rank(desc(Pop_2001)),
rang_2011 = rank(desc(Pop_2011)))
##### calculus of F ####
# list of countries studied
liste_pays <- c("DE", "CZ", "ES", "FR", "UK", "IT", "NL", "PL", "RO")
# diverse N0/N
liste_tible_result <- dk_countries_tradeve %>%
filter(Country %in% liste_pays) %>%
select(Country, rang_1961:rang_2011) %>%
mutate(rowid = row_number()) %>%
pivot_longer(cols = rang_1961:rang_2011, names_to = "periods", values_to = "rank") %>%
arrange(rank, Country, periods) %>%
group_by(Country) %>%
group_split(Country)
# calculus of f
liste_f_countries <- tibble()
for (country in 1:length(liste_tible_result)) {
# create vector of ranking of IDs of elements (here cities) for each country
tibble_of_ids_rank <- liste_tible_result[[country]] %>%
group_by(periods) %>%
select(rowid) %>%
group_split()
vector_of_ids_rank <- list()
for (k in 1:length(tibble_of_ids_rank)) {
vector_of_ids_rank[[k]] <- tibble_of_ids_rank[[k]]$rowid
}
# calculus of f
f_result_tibble_countries <- f_calculus_F_data(vector_entry = seq(1, length(vector_of_ids_rank[[k]]), 1),
times = length(vector_of_ids_rank),
list_entry = vector_of_ids_rank,
N = length(vector_of_ids_rank[[1]])) %>%
mutate(N = length(vector_of_ids_rank[[1]])) %>%
mutate(Country = liste_tible_result[[country]]$Country[1])
# output tibble bind
liste_f_countries <- liste_f_countries %>%
bind_rows(f_result_tibble_countries)
}
liste_f_countries <- liste_f_countries %>%
group_by(Country) %>%
mutate(Fresult = Fsum/sum(Fsum))
##### viz ####
liste_f_countries %>%
ggplot(aes(x=N0_N, y=Fresult, color = Country)) +
geom_line() +
ggthemes::scale_color_tableau(palette = 'Tableau 10') +
theme_bw() +
xlab(TeX(r"($N_{0}/N$)")) +
ylab(TeX(r"($F$)")) +
labs(title = "Cities")
ggsave(filename = "figures/F_cities.png", width = 15, height = 12, dpi = 300, units = 'cm')
##### fit ####
# test d'exemple avec l'Espagne
output <- liste_f_countries %>% filter(Country == 'ES')
# manual observation of potential fit
preview(formula_F_asymetric, data = output,
start = list(ps = 0.1, pd = 0, alpha=-0.3))
# fitting analysis with nls
fitting_results <- nls(formula = formula_F_asymetric, # formula made
data = output, # df for fit > ok for tibble
start = list(ps = 0.01, pd = 0, alpha = -1), # starting model in
lower = list(ps = 0, pd = 0, alpha = -10000000000000),
upper = list(ps = 1, pd = 1, alpha = 1), algorithm = "port", nls.control(maxiter = 10000))
fitting_results
summary(fitting_results) # pd error is big with small T ; less in large T
# visualization
plot(output$N0_N, output$Fresult, pch=19, cex=0.5, xlab='p=N0/N', ylab = 'F')
lines(output$N0_N, predict(fitting_results), col='red')
#### avec tous les pays
liste_outputs <- liste_f_countries %>%
group_by(Country) %>%
group_split()
fitting_liste_results <- list()
for (i in 1:length(liste_outputs)) {
fitting_liste_results[[i]] <- nls(formula = formula_F_asymetric, # formula made
data = liste_outputs[[i]], # df for fit > ok for tibble
start = list(ps = 0.00001, pd = 0, alpha = -1), # starting model in
lower = list(ps = 0, pd = 0, alpha = -10000000000000),
upper = list(ps = 1, pd = 1, alpha = 1),
algorithm = "port", nls.control(maxiter = 10000))
}
output_fit_results <- tibble()
for (i in 1:length(fitting_liste_results)) {
extractionparam <- fitting_liste_results[[i]]$m$getAllPars()
constructiontibble <- tibble(
Country = liste_outputs[[i]]$Country[1],
ps = round(extractionparam[1], 4),
pd = round(extractionparam[2], 4),
alpha = round(extractionparam[3],4),
rss = round(fitting_liste_results[[i]]$m$deviance(), 5)
)
output_fit_results <- output_fit_results %>%
bind_rows(constructiontibble)
}
### ploting multiple fits
par(mfrow= c(3,3))
for (i in 1:length(fitting_liste_results)) {
plot(liste_outputs[[i]]$N0_N, liste_outputs[[i]]$Fresult, pch=19, cex=0.5, xlab='N0/N', ylab = 'F',
main=output_fit_results[i,]$Country)
lines(liste_outputs[[i]]$N0_N, predict(fitting_liste_results[[i]]), col='red')
}
#### shanghai data ####
shangai <- read.csv2(file = "data/shanghai-world-university-ranking.csv") %>%
as_tibble()
shangai_ranked <- shangai %>%
# need renaming
mutate(University = case_when(
University %in% c("Texas A & M University", "Texas A&M University - College Station") ~ "Texas A&M University",
University %in% c("Rutgers, The State University of New Jersey - New Brunswick") ~ "Rutgers, The State University of New Jersey",
University %in% c("Pierre and Marie Curie University - Paris 6") ~ "Pierre and Marie Curie University - Paris 6",
University %in% c("University of California, Berkeley") ~ "University of California-Berkeley",
University %in% c("University of Michigan - Ann Arbor") ~ "University of Michigan-Ann Arbor",
University %in% c("University of Paris Sud (Paris 11)") ~ "University of Paris-Sud (Paris 11)",
University %in% c("University of Pittsburgh, Pittsburgh Campus", "University of Pittsburgh-Pittsburgh Campus") ~ "University of Pittsburgh",
University %in% c("Arizona State University - Tempe") ~ "Arizona State University",
TRUE ~ University
)) %>%
mutate(rank = as.numeric(World.rank)) %>%
filter(rank <= 100) %>%
select(University, Year, rank) %>%
pivot_wider(names_from = Year, values_from = rank) %>%
rowid_to_column()
shangai_ranked <- shangai_ranked %>%
pivot_longer(cols = `2012`:`2010`, names_to = "year", values_to = "rank") %>%
arrange(rowid, year) %>%
# if is NA <=> more than 100, remove
filter(!is.na(rank))
##### calculus of F ####
# diverse N0/N
liste_tible_result <- shangai_ranked %>%
arrange(year, rank) %>%
group_by(year) %>%
select(rowid) %>%
group_split()
# create vector of ranking of IDs of elements (here universities)
vector_of_ids_rank <- list()
for (k in 1:length(liste_tible_result)) {
vector_of_ids_rank[[k]] <- liste_tible_result[[k]]$rowid
}
# calculus of f
f_result_tibble_shanghai <- f_calculus_F_data(vector_entry = seq(1, length(vector_of_ids_rank[[k]]), 1),
times = length(vector_of_ids_rank),
list_entry = vector_of_ids_rank, N = 2500) %>%
mutate(N = 2500)
f_result_tibble_shanghai <- f_result_tibble_shanghai %>%
mutate(Fresult = Fsum/sum(Fsum))
##### viz ####
f_result_tibble_shanghai %>%
ggplot(aes(x=N0_N, y=Fresult)) +
geom_line() +
ggthemes::scale_color_tableau(palette = 'Tableau 10') +
theme_bw() +
xlab(TeX(r"($N_{0}/N$)")) +
ylab(TeX(r"($F$)")) +
labs(title = "Universities")
ggsave(filename = "figures/F_shanghai.png", width = 15, height = 12, dpi = 300, units = 'cm')
##### fit ####
output <- f_result_tibble_shanghai
# manual observation of potential fit
par(mfrow=c(1,1))
preview(formula_F_asymetric, data = output,
start = list(ps = 0.1, pd = 0, alpha=-1))
# fitting analysis with nls
fitting_results <- nls(formula = formula_F_asymetric, # formula made
data = output, # df for fit > ok for tibble
start = list(ps = 0.0001, pd = 0, alpha = -1), # starting model in
lower = list(ps = 0, pd = 0, alpha = -10000000000000),
upper = list(ps = 1, pd = 1, alpha = 1),
algorithm = "port", nls.control(maxiter = 10000))
fitting_results
summary(fitting_results) # pd error is big with small T ; less in large T
# visualization
plot(output$N0_N, output$Fresult, pch=19, cex=0.5, xlab='N0/N', ylab = 'F')
lines(output$N0_N, predict(fitting_results), col='red')
#### Chessplayers FIDE ####
chess_yearly <- read_parquet(file = "data/FIDE_standard_compilations_Dahiya.parquet",
as_data_frame = TRUE) %>%
as_tibble()
chess_yearly <- chess_yearly %>%
mutate(year = str_sub(string = Date, start = 1, end = 4),
month = str_sub(string = Date, start = 6, end = 7))
chess_yearly <- chess_yearly %>%
filter(month %in% c("01", '04', '07', '10')) # 4 times a year
#### note: N is very diverse through time (x10)
chess_yearly %>%
select(year, month, ID_Number) %>%
group_by(year, month) %>%
summarise(n=n()) %>%
view()
##### top 5000 2001-2019 ####
chess_yearly_select <- chess_yearly %>%
select(ID_Number, ranking, year, month) %>%
rename(rank = ranking, rowid = ID_Number) %>%
filter(rank < 10001) %>%
arrange(rowid, year)
##### calculus of F ####
# diverse N0/N
liste_tible_result <- chess_yearly_select %>%
arrange(year, month, rank) %>%
group_by(year, month) %>%
select(rowid) %>%
group_split()
# create vector of ranking of IDs of elements (here universities)
vector_of_ids_rank <- list()
for (k in 1:length(liste_tible_result)) {
vector_of_ids_rank[[k]] <- liste_tible_result[[k]]$rowid
}
# calculus of f
f_result_tibble_FIDE <- f_calculus_F_data(vector_entry = seq(1, length(vector_of_ids_rank[[k]]), 1),
times = length(vector_of_ids_rank),
list_entry = vector_of_ids_rank, N = 29273) %>%
mutate(N = 29273)
f_result_tibble_FIDE <- f_result_tibble_FIDE %>%
mutate(Fresult=Fsum/sum(Fsum))
##### viz ####
f_result_tibble_FIDE %>%
ggplot(aes(x=N0_N, y=Fresult)) +
geom_line() +
ggthemes::scale_color_tableau(palette = 'Tableau 10') +
theme_bw() +
xlab(TeX(r"($N_{0}/N$)")) +
ylab(TeX(r"($F$)")) +
labs(title = "Chessplayers")
ggsave(filename = "figures/F_chessplayers.png", width = 15, height = 12, dpi = 300, units = 'cm')
##### fit ####
output <- f_result_tibble_FIDE
# manual observation of potential fit
par(mfrow=c(1,1))
preview(formula_F_asymetric, data = output,
start = list(ps = 0.0001, pd = 0, alpha=-1))
# fitting analysis with nls
fitting_results <- nls(formula = formula_F_asymetric, # formula made
data = output, # df for fit > ok for tibble
start = list(ps = 0.0001, pd = 0, alpha = -1), # starting model in
lower = list(ps = 0, pd = 0, alpha = -10000000000000),
upper = list(ps = 1, pd = 1, alpha = 1),
algorithm = "port", nls.control(maxiter = 10000))
fitting_results
summary(fitting_results)
# visualization
plot(output$N0_N, output$Fresult, pch=19, cex=0.5, xlab='N0/N', ylab = 'F')
lines(output$N0_N, predict(fitting_results), col='red')