-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpirates.R
376 lines (313 loc) · 12 KB
/
pirates.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
rm(list = ls()); gc(); gc()
options(bitmapType='cairo')
options(scipen = 999)
library(dplyr)
library(purrr)
library(tidyr)
library(readr)
library(stringr)
library(ggplot2)
library(rvest)
# Define your workspace: "X:/xxx/"
wd <- "c:/github/pirates/"
setwd(wd)
## retrieve wikipedia list of pirates page
#pirates.url <- "https://en.wikipedia.org/w/index.php?title=List_of_pirates&oldid=799791558"
pirates.url <- "C:/github/pirates/List of pirates - Wikipedia.htm"
## read the page into R
pirates.wiki <- read_html(pirates.url)
## get raw data
# 3: Rise of the English Sea Dogs and Dutch Corsairs: 1560-1650
# 4: Age of the Buccaneers: 1650-1690
# 5: Golden Age of Piracy: 1690-1730
pirates.raw <- pirates.wiki %>%
# grab all data tables; they all have the class "wikitable"
html_nodes(".wikitable") %>%
# extract all tables and put them into one dataframe
purrr::map_dfr(html_table, fill = TRUE) %>%
# clean column names
setNames(make.names(names(.))) %>%
mutate(
# stupid wiki has capital and non-capital letters in headers...
Years.active = if_else(is.na(Years.active), Years.Active, Years.active),
# empty table cells should be NA
Years.active = parse_character(Years.active),
Life = parse_character(Life),
# god, I hate windows... R on Win will chocke on ‒ and – characters, so replace them with ASCII
# http://unicode-search.net/unicode-namesearch.pl?term=dash
# U+2012 to U+2015 are dashes
Life = stringr::str_replace_all(Life, "(\u2012|\u2013)", "-"),
Years.active = stringr::str_replace_all(Years.active, "(\u2012|\u2013)", "-")
) %>%
# keep columns we want and re-order
select(Name, Life, Country.of.origin, Years.active, Comments)
#write.csv(pirates.raw, file = "pirates.csv", row.names = FALSE)
## functions used for cleanup
f.keep.last.four.digits <- function(x) {
gsub("[[:alpha:][:blank:][:punct:]]", "", x) %>%
str_sub(-4, -1)
}
f.activity.from.life <- function(b, d, return.b = TRUE) {
# four different NA-permutations:
# we assume that if
# - we only know birth, piracy was active at birth + 25 years
# - we only know death, piracy was active at death
# - we know both birth&death, piracy was active at his middle age: (b+d)/2
#
# we return the corresponding value, and make sure everything is integer()
case_when(
is.na(b)&is.na(d) ~ as.integer(NA),
is.na(b) ~ if(return.b){as.integer(NA)} else {d},
is.na(d) ~ if(return.b){as.integer(b+25L)} else {as.integer(NA)},
TRUE ~ if(return.b){as.integer((b+d)/2)} else {as.integer(NA)}
)
}
# pull top n countries as vector from pirates.dta
top.n.countries <- function(n = 10) {
pirates.dta %>%
group_by(Country.of.origin) %>%
tally(sort = TRUE) %>%
head(n) %>%
.$Country.of.origin
}
# hack some country colours, based off this scheme: http://colorbrewer2.org/?type=qualitative&scheme=Paired&n=11
my.colours <- c('#a6cee3', # China
'#33a02c', # Colonial America
'#e31a1c', # England
'#1f78b4', # France
'#fb9a99', # Germany
'#fdbf6f', # Netherlands
'#b2df8a', # other
'#cab2d6', # Spain
'#6a3d9a', # United States
'#ffff99', # Unknown
'#ff7f00') # Venezuela
##
## generate dataset to work with
##
## generate our pirating times dataset
pirates.dta <- pirates.raw %>%
mutate(
# born and died forced into the "yyyy-yyyy" schedule for later tidyr::separate()
Life.parsed = gsub("(b.|born) *([[:digit:]]+)", "\\2-", x = Life),
Life.parsed = gsub("(d.|died) *([[:digit:]]+)", "-\\2", x = Life.parsed),
Years.active = gsub("(to) *([[:digit:]]+)", "-\\2", x = Years.active),
# move the "flourished" dates to its own column
flourished = if_else(grepl("fl.", Life.parsed), gsub("fl. *", "", Life.parsed), as.character(NA)),
# remove "flourished" dates from life
Life.parsed = if_else(grepl("fl.", Life.parsed), as.character(NA), Life.parsed)
) %>%
# split the life, active, and flourished years into 2 columns (start/stop), each
separate(Life.parsed, c("born", "died"), sep = "-", extra = "merge", fill = "right") %>%
separate(Years.active, c("active.start", "active.stop"), sep = "-", extra = "merge", fill = "right", remove = FALSE) %>%
separate(flourished, c("fl.start", "fl.stop"), sep = "-", extra = "merge", fill = "right", remove = FALSE) %>%
#
mutate_at(
vars(born, died, active.start, active.stop, fl.start, fl.stop),
f.keep.last.four.digits
) %>%
mutate_at(
vars(born, died, active.start, active.stop, fl.start, fl.stop),
#as.integer
parse_integer
) %>%
# get the best guess of piracing time: active > flourished > estimated from life
mutate(
# take active if available, if not take flourished
piracing.start = if_else(is.na(active.start), fl.start, active.start),
piracing.stop = if_else(is.na(active.stop), fl.stop, active.stop),
# neither active, nor flourished date, estimate from life
piracing.start = if_else(is.na(piracing.start)&is.na(piracing.stop), f.activity.from.life(b = born, d = died), piracing.start),
piracing.stop = if_else(is.na(piracing.start)&is.na(piracing.stop), f.activity.from.life(b = born, d = died, return.b = FALSE), piracing.stop),
# finally, let's get single-year start/stops cleaned up
piracing.start = if_else(is.na(piracing.start), piracing.stop, piracing.start),
piracing.stop = if_else(is.na(piracing.stop), piracing.start, piracing.stop)
) %>%
# reorder
select(Name, Life, piracing.start, piracing.stop, Country.of.origin, everything())
## building the pirate activity, with each pirate having one row per decade in which he was active
pirate.activity <- pirates.dta %>%
mutate(
piracing.start = piracing.start %/%10L*10L,
piracing.stop = piracing.stop %/%10L*10L,
arrr = piracing.stop - piracing.start,
country = if_else(Country.of.origin %in% top.n.countries(10), Country.of.origin, "other")
) %>%
filter(piracing.start > 1000) %>%
rowwise() %>%
do(
data.frame(
Name = .$Name,
Life = .$Life,
piracing.start = .$piracing.start,
piracing.stop = .$piracing.stop,
piracing.years = seq(.$piracing.start, .$piracing.stop, by = 10L),
arrr = .$arrr,
Country.of.origin = .$Country.of.origin,
country = .$country,
stringsAsFactors = FALSE
)
)
##
## analysis/graphing
##
## golden age of piracy
pirates.dta %>%
filter(piracing.start > 1000) %>%
select(Name, Life, starts_with("piracing"), Country.of.origin) %>%
gather(pirating, year, starts_with("piracing")) %>%
ggplot()+
geom_density(aes(x=year, colour = pirating))+
#geom_text(aes(x = 1300, y = 0.002, label = "yarrr! \u2620"), size = 12)+
theme_bw()+
theme(plot.title = element_text(lineheight=.8, face="bold"))+
labs(
title = "Pirate tenure \n \u2620\u2620\u2620",
x = "",
y = "density"
)
# export to size that fits everything into graph, use golden ratio
ggsave(file="piracy-tenure.png", width = 30, height = 30/((1+sqrt(5))/2), units = "cm")
## piracy golden age by top n (15) countries
pirates.dta %>%
filter(piracing.start > 1000) %>%
filter(Country.of.origin %in% top.n.countries(15)) %>%
ggplot()+
geom_density_ridges(aes(x = piracing.start, y = Country.of.origin, fill = Country.of.origin))+
theme_bw()+
theme(plot.title = element_text(lineheight=.8, face="bold"))+
labs(
title = "Pirate activity by country\n \u2620\u2620\u2620",
x = "",
y = "country"
)
# export to size that fits everything into graph, use golden ratio
ggsave(file="piracy-by-country.png", width = 30, height = 30/((1+sqrt(5))/2), units = "cm")
## piracing tenure
pirates.dta %>%
filter(piracing.start > 1000) %>%
ggplot()+
geom_density(aes(x=piracing.stop-piracing.start))
ggsave(file="piracy-tenure.png", width = 30, height = 30/((1+sqrt(5))/2), units = "cm")
## pirates by country
pirates.raw %>%
group_by(Country.of.origin) %>%
tally(sort = TRUE)
## pirate activity per decade
pirate.activity %>%
ungroup() %>%
#ungroup.rowwise_df() %>%
group_by(piracing.years, country) %>%
tally() %>%
ggplot(aes(x=piracing.years, y=n))+
geom_bar(aes(fill=country, colour=country), stat = "identity", position = "stack")+
# scale_fill_brewer(type = "qual", palette = 3)+
# scale_colour_brewer(type = "qual", palette = 3)+
scale_fill_manual(values = my.colours)+
scale_colour_manual(values = my.colours)+
theme_bw()+
theme(plot.title = element_text(lineheight=.8, face="bold"))+
labs(
title = "Arrr!tivity of famous pirates per decade\n \u2620\u2620\u2620",
x = "",
y = "number of pirates active"
)
# export to size that fits everything into graph, use golden ratio
ggsave(file="piracy-country-time.png", width = 30, height = 30/((1+sqrt(5))/2), units = "cm")
##
## fit the negbin distribution
##
pirates.fit <- pirates.dta %>%
filter(piracing.start > 1500) %>%
filter(piracing.start < 1750) %>%
mutate(
arrr = piracing.stop - piracing.start
)
pirates.fit %>%
ggplot()+
geom_density(aes(x=piracing.start))
ggsave(file="piracy-goldenage.png", width = 30, height = 30/((1+sqrt(5))/2), units = "cm")
pirates.fit %>%
ggplot()+
geom_density(aes(x=arrr))
ggsave(file="piracy-goldenage-tenure.png", width = 30, height = 30/((1+sqrt(5))/2), units = "cm")
#library(fitdistrplus)
fitdistrplus::descdist(pirates.fit$arrr, boot = 1000)
# negative binomial
f1 <- fitdistrplus::fitdist(pirates.fit$arrr, "nbinom")
f1
fitdistrplus::plotdist(pirates.fit$arr, "nbinom", para = list(size=f1$estimate[1], mu=f1$estimate[2]))
## unsuccessful fitting of piracing.start
# f2 <- fitdistrplus::fitdist(pirates.fit$piracing.start, "sn")
# f2
# fitdistrplus::plotdist(pirates.fit$piracing.start, "nbinom", para = list(size = f1$estimate[1], mu = f1$estimate[2]))
# f3 <- fGarch::snormFit(pirates.fit$arrr)
# f3
# dsnorm(1:100, mean = 9.3899, sd = 7.0945, xi = 180.3135) %>% as.data.frame()
#
# library(actuar)
#
# my_data <- pirates.fit$arrr
#
# fit_ll <- fitdist(my_data, "llogis", start = list(shape = 1, scale = 500), lower = c(0,0))
# fit_P <- fitdist(my_data, "pareto", start = list(shape = 1, scale = 500))
# fit_B <- fitdist(my_data, "burr", start = list(shape1 = 0.3, shape2 = 1, rate = 1))
# cdfcomp(list(fit_ln, fit_ll, fit_P, fit_B), xlogscale = TRUE, ylogscale = TRUE,
# legendtext = c("lognormal", "loglogistic", "Pareto", "Burr"), lwd=2)
#
# fit_ll <- fitdist(my_data, "llogis")
#
# fit0.start <- fitdist(my_data, "truncnorm", fix.arg=list(a=0),
# start = as.list(0))
##
## build a pirate's story
##
## pirate name generator
# http://www.fantasynamegenerators.com/pirate-names.php
source("pirate-names.R")
f.gen.pirate.name <- function(female = FALSE) {
# generate a pirate name from a list of
# - first names (male/female)
# - nick names (male/female)
# - last names
if(female) {
paste(
base::sample(names.female, 1),
base::sample(names.nickf, 1),
base::sample(names.last, 1)
)
} else {
paste(
base::sample(names.male, 1),
base::sample(names.nickm, 1),
base::sample(names.last, 1)
)
}
}
f.gen.pirating.tenure <- function() {
# generate a tenure time from the estimated theoretical neg binomial distribution (pirates.fit$arrr)
rnbinom(1, size=f1$estimate[1], mu=f1$estimate[2])
}
f.gen.pirating.startyear <- function() {
# generate a startyear by pulling from the empirical distribution (pirates.fit$piracing.start)
base::sample(pirates.fit$piracing.start, 1)
}
f.pirate.story <- function(female = FALSE) {
# build the pirate story by joining name, startyear, and tenure time
yy <- f.gen.pirating.startyear()
rr <- paste0(
"Your name is ",
f.gen.pirate.name(female),
" and you roamed the caribbean from ",
yy, " to ", yy+f.gen.pirating.tenure(),
". Arrr! \u2620"
)
return(rr)
}
# test the story!
f.pirate.story()
f.pirate.story(female = TRUE)
# skull+crossbones
# U+2620
# https://emojipedia.org/emoji/%E2%98%A0/
# "\u2620"