<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/talk-like-a-pirate-day-2017/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://opiateforthemass.es/articles/pirates/"> Opiate for the masses</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h1>International Talk Like A Pirate Day</h1>
<p><img alt="pirates arr, not err!" src="https://i0.wp.com/opiateforthemass.es/images/safferli/to-err-to-arr.jpg?w=456" data-recalc-dims="1"></p>
<blockquote>
<p>Q: What has 8 legs, 8 arms and 8 eyes?<br>
A: 8 pirates.</p>
</blockquote>
<p><span>A</span>vast, ye scurvy scum! Today be September 19, the <a rel="nofollow" href="https://en.wikipedia.org/wiki/International_Talk_Like_a_Pirate_Day" target="_blank">International Talk Like A Pirate Day</a>. While it’s a silly “holiday”, it’s a great chance to switch your Facebook to <a rel="nofollow" href="http://socmedianews.com/2009/05/facebook-fun-aaargchange-your-language-to-pirate-english/" target="_blank">Pirate English</a> (“Arr, this be pleasing to me eye!”). Also, it’s as good a reason as any to go sailing the Internet Main, scouring for booty (data)!</p>
<p>Well, any excuse, y’know…</p>
<p>What is today known (more or less) as “Pirate English” (the bit with all the “arrr!”, “ahoy, ye matey!”, etc) is in fact the accent employed by Robert Newton, the actor who played Long John Silver in the <a rel="nofollow" href="http://www.imdb.com/title/tt0043067/" target="_blank">first non-silent film (“talkie”) Treasure Island</a>. He speaks with a Bristol accent, which was in fact <a rel="nofollow" href="https://www.theguardian.com/theguardian/2010/mar/10/pirates-notes-and-queries" target="_blank">the main trading port with the West Indies</a> (Bristol, not the accent, of course). The accent we today associate with pirates could well have been historical!</p>
<p>More useless information on pirates (such as that there is no historical evidence of a pirate ever owning a parrot as a pet – again this stereotype evolved from the Treasure Island movie) is on the wonderful as always treasure trove of superfluous information, the <a rel="nofollow" href="http://qi.com/infocloud/pirates" target="_blank">QI page on pirates</a>.</p>
<h1>Data</h1>
<p>First, let’s get some data on pirates. I have some hazy memories on seeing data on historical shipping, and also piracy somewhere a while ago, but I cannot find it any more. I must be getting older. So, I’ll do what any self-respecting young whippersnapper would do and grab data from Wikipedia. There is a <a rel="nofollow" href="https://en.wikipedia.org/w/index.php?title=List_of_pirates" target="_blank">list of famous pirates</a>; it’s biased, of course. I see relatively few entries on Barbary pirates, for instance. No matter, the list covers most of the Golden Age of Piracy in the Caribbean, and I’ll use it to generate a dataset on these pirates.</p>
<p>We’ll grab the wiki page, linking to the static ID of the page for reproducability.</p>
<figure>
<pre><code data-lang="r"><span>## retrieve wikipedia list of pirates page
</span><span>pirates.url</span><span> </span><span>&lt;-</span><span> </span><span>&quot;https://en.wikipedia.org/w/index.php?title=List_of_pirates&amp;oldid=799791558&quot;</span><span>
</span><span>## read the page into R
</span><span>pirates.wiki</span><span> </span><span>&lt;-</span><span> </span><span>read_html</span><span>(</span><span>pirates.url</span><span>)</span></code></pre>
</figure>
<p>We then use <code>purrr::map_dfr</code> to grab all tables in the HTML, and push them into a single dataframe and do some basic cleaning. Man, I hate Windows; utf-8 and Windows <em>still in 2017</em> is a major pain in the stern.</p>
<figure>
<pre><code data-lang="r"><span>## grab all tables on the page; tables 3-5 are the ones of main interest
# 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
</span><span>pirates.raw</span><span> </span><span>&lt;-</span><span> </span><span>pirates.wiki</span><span> </span><span>%>%</span><span> </span><span># grab all data tables; they all have the class &quot;wikitable&quot;
</span><span> </span><span>html_nodes</span><span>(</span><span>&quot;.wikitable&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span># extract all tables and put them into one dataframe
</span><span> </span><span>purrr</span><span>::</span><span>map_dfr</span><span>(</span><span>html_table</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span>%>%</span><span> </span><span># clean column names
</span><span> </span><span>setNames</span><span>(</span><span>make.names</span><span>(</span><span>names</span><span>(</span><span>.</span><span>)))</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>
</span><span># stupid wiki has capital and non-capital letters in headers...
</span><span> </span><span>Years.active</span><span> </span><span>=</span><span> </span><span>if_else</span><span>(</span><span>is.na</span><span>(</span><span>Years.active</span><span>),</span><span> </span><span>Years.Active</span><span>,</span><span> </span><span>Years.active</span><span>),</span><span>
</span><span># empty table cells should be NA
</span><span> </span><span>Years.active</span><span> </span><span>=</span><span> </span><span>parse_character</span><span>(</span><span>Years.active</span><span>),</span><span>
</span><span>Life</span><span> </span><span>=</span><span> </span><span>parse_character</span><span>(</span><span>Life</span><span>),</span><span> </span><span># god, I hate windows... R on Win will chocke on ‒ and – characters, so replace them with ASCII
</span><span> </span><span># http://unicode-search.net/unicode-namesearch.pl?term=dash
</span><span> </span><span># U+2012 to U+2015 are dashes
</span><span> </span><span>Life</span><span> </span><span>=</span><span> </span><span>stringr</span><span>::</span><span>str_replace_all</span><span>(</span><span>Life</span><span>,</span><span> </span><span>&quot;(\u2012|\u2013)&quot;</span><span>,</span><span> </span><span>&quot;-&quot;</span><span>),</span><span>
</span><span>Years.active</span><span> </span><span>=</span><span> </span><span>stringr</span><span>::</span><span>str_replace_all</span><span>(</span><span>Years.active</span><span>,</span><span> </span><span>&quot;(\u2012|\u2013)&quot;</span><span>,</span><span> </span><span>&quot;-&quot;</span><span>)</span><span>
</span><span>)</span><span> </span><span>%>%</span><span> </span><span># keep columns we want and re-order
</span><span> </span><span>select</span><span>(</span><span>Name</span><span>,</span><span> </span><span>Life</span><span>,</span><span> </span><span>Country.of.origin</span><span>,</span><span> </span><span>Years.active</span><span>,</span><span> </span><span>Comments</span><span>)</span></code></pre>
</figure>
<p>With the raw data pulled, we now need to clean it a bit. Our goal is to generate a data set of the “pirating times” of each individual pirate. Wiki lists the active times, the “flourished” times, and/or the life times. We’ll split the age columns (usually yyyy-yyyy) into two, providing some padding if there is only one of the dates available. After this, we’ll clean up the columns into integer columns, ignoring the rows with did not provide accurate enough year data (e.g. “17th century”). This is a fun project, not a scientific one.</p>
<figure>
<pre><code data-lang="r"><span>## generate our pirating times dataset
</span><span>pirates.dta</span><span> </span><span>&lt;-</span><span> </span><span>pirates.raw</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>
</span><span># born and died forced into the &quot;yyyy-yyyy&quot; schedule for later tidyr::separate()
</span><span> </span><span>Life.parsed</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;(b.|born) *([[:digit:]]+)&quot;</span><span>,</span><span> </span><span>&quot;\\2-&quot;</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>Life</span><span>),</span><span>
</span><span>Life.parsed</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;(d.|died) *([[:digit:]]+)&quot;</span><span>,</span><span> </span><span>&quot;-\\2&quot;</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>Life.parsed</span><span>),</span><span>
</span><span>Years.active</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;(to) *([[:digit:]]+)&quot;</span><span>,</span><span> </span><span>&quot;-\\2&quot;</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>Years.active</span><span>),</span><span>
</span><span># move the &quot;flourished&quot; dates to its own column
</span><span> </span><span>flourished</span><span> </span><span>=</span><span> </span><span>if_else</span><span>(</span><span>grepl</span><span>(</span><span>&quot;fl.&quot;</span><span>,</span><span> </span><span>Life.parsed</span><span>),</span><span> </span><span>gsub</span><span>(</span><span>&quot;fl. *&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>Life.parsed</span><span>),</span><span> </span><span>as.character</span><span>(</span><span>NA</span><span>)),</span><span>
</span><span># remove &quot;flourished&quot; dates from life
</span><span> </span><span>Life.parsed</span><span> </span><span>=</span><span> </span><span>if_else</span><span>(</span><span>grepl</span><span>(</span><span>&quot;fl.&quot;</span><span>,</span><span> </span><span>Life.parsed</span><span>),</span><span> </span><span>as.character</span><span>(</span><span>NA</span><span>),</span><span> </span><span>Life.parsed</span><span>)</span><span>
</span><span>)</span><span> </span><span>%>%</span><span> </span><span># split the life, active, and flourished years into 2 columns (start/stop), each
</span><span> </span><span>separate</span><span>(</span><span>Life.parsed</span><span>,</span><span> </span><span>c</span><span>(</span><span>&quot;born&quot;</span><span>,</span><span> </span><span>&quot;died&quot;</span><span>),</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;-&quot;</span><span>,</span><span> </span><span>extra</span><span> </span><span>=</span><span> </span><span>&quot;merge&quot;</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;right&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>separate</span><span>(</span><span>Years.active</span><span>,</span><span> </span><span>c</span><span>(</span><span>&quot;active.start&quot;</span><span>,</span><span> </span><span>&quot;active.stop&quot;</span><span>),</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;-&quot;</span><span>,</span><span> </span><span>extra</span><span> </span><span>=</span><span> </span><span>&quot;merge&quot;</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;right&quot;</span><span>,</span><span> </span><span>remove</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>%>%</span><span> </span><span>separate</span><span>(</span><span>flourished</span><span>,</span><span> </span><span>c</span><span>(</span><span>&quot;fl.start&quot;</span><span>,</span><span> </span><span>&quot;fl.stop&quot;</span><span>),</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;-&quot;</span><span>,</span><span> </span><span>extra</span><span> </span><span>=</span><span> </span><span>&quot;merge&quot;</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;right&quot;</span><span>,</span><span> </span><span>remove</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>%>%</span><span> </span><span># </span><span> </span><span>mutate_at</span><span>(</span><span>
</span><span>vars</span><span>(</span><span>born</span><span>,</span><span> </span><span>died</span><span>,</span><span> </span><span>active.start</span><span>,</span><span> </span><span>active.stop</span><span>,</span><span> </span><span>fl.start</span><span>,</span><span> </span><span>fl.stop</span><span>),</span><span> </span><span>f.keep.last.four.digits</span><span>
</span><span>)</span><span> </span><span>%>%</span><span> </span><span>mutate_at</span><span>(</span><span>
</span><span>vars</span><span>(</span><span>born</span><span>,</span><span> </span><span>died</span><span>,</span><span> </span><span>active.start</span><span>,</span><span> </span><span>active.stop</span><span>,</span><span> </span><span>fl.start</span><span>,</span><span> </span><span>fl.stop</span><span>),</span><span>
</span><span>#as.integer
</span><span> </span><span>parse_integer</span><span>
</span><span>)</span><span> </span><span>%>%</span><span>
</span><span># get the best guess of piracing time: active > flourished > estimated from life
</span><span> </span><span>mutate</span><span>(</span><span>
</span><span># take active if available, if not take flourished
</span><span> </span><span>piracing.start</span><span> </span><span>=</span><span> </span><span>if_else</span><span>(</span><span>is.na</span><span>(</span><span>active.start</span><span>),</span><span> </span><span>fl.start</span><span>,</span><span> </span><span>active.start</span><span>),</span><span>
</span><span>piracing.stop</span><span> </span><span>=</span><span> </span><span>if_else</span><span>(</span><span>is.na</span><span>(</span><span>active.stop</span><span>),</span><span> </span><span>fl.stop</span><span>,</span><span> </span><span>active.stop</span><span>),</span><span>
</span><span># neither active, nor flourished date, estimate from life
</span><span> </span><span>piracing.start</span><span> </span><span>=</span><span> </span><span>if_else</span><span>(</span><span>is.na</span><span>(</span><span>piracing.start</span><span>)</span><span>&amp;</span><span>is.na</span><span>(</span><span>piracing.stop</span><span>),</span><span> </span><span>f.activity.from.life</span><span>(</span><span>b</span><span> </span><span>=</span><span> </span><span>born</span><span>,</span><span> </span><span>d</span><span> </span><span>=</span><span> </span><span>died</span><span>),</span><span> </span><span>piracing.start</span><span>),</span><span>
</span><span>piracing.stop</span><span> </span><span>=</span><span> </span><span>if_else</span><span>(</span><span>is.na</span><span>(</span><span>piracing.start</span><span>)</span><span>&amp;</span><span>is.na</span><span>(</span><span>piracing.stop</span><span>),</span><span> </span><span>f.activity.from.life</span><span>(</span><span>b</span><span> </span><span>=</span><span> </span><span>born</span><span>,</span><span> </span><span>d</span><span> </span><span>=</span><span> </span><span>died</span><span>,</span><span> </span><span>return.b</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>),</span><span> </span><span>piracing.stop</span><span>),</span><span>
</span><span># finally, let's get single-year start/stops cleaned up
</span><span> </span><span>piracing.start</span><span> </span><span>=</span><span> </span><span>if_else</span><span>(</span><span>is.na</span><span>(</span><span>piracing.start</span><span>),</span><span> </span><span>piracing.stop</span><span>,</span><span> </span><span>piracing.start</span><span>),</span><span> </span><span>piracing.stop</span><span> </span><span>=</span><span> </span><span>if_else</span><span>(</span><span>is.na</span><span>(</span><span>piracing.stop</span><span>),</span><span> </span><span>piracing.start</span><span>,</span><span> </span><span>piracing.stop</span><span>)</span><span>
</span><span>)</span><span> </span><span>%>%</span><span> </span><span># reorder
</span><span> </span><span>select</span><span>(</span><span>Name</span><span>,</span><span> </span><span>Life</span><span>,</span><span> </span><span>piracing.start</span><span>,</span><span> </span><span>piracing.stop</span><span>,</span><span> </span><span>Country.of.origin</span><span>,</span><span> </span><span>everything</span><span>())</span><span> </span></code></pre>
</figure>
<p>With the dataset now sufficiently cleared up, we will now change the dates to decades only – so change 1643 to 1640. We’ll graph our data by decade. We’ll use some <code>rowwise() %>% do()</code> magic to generate one row per pirate per activity decade (so two rows if he was active from 1648 to 1651). We will add up the pirates active in each decade to get to our visualisation of pirate activity, and some pirates were active for far longer than others!</p>
<figure>
<pre><code data-lang="r"><span>## building the pirate activity, with each pirate having one row per decade in which he was active
</span><span>pirate.activity</span><span> </span><span>&lt;-</span><span> </span><span>pirates.dta</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>
</span><span>piracing.start</span><span> </span><span>=</span><span> </span><span>piracing.start</span><span> </span><span>%/%</span><span>10L</span><span>*</span><span>10L</span><span>,</span><span>
</span><span>piracing.stop</span><span> </span><span>=</span><span> </span><span>piracing.stop</span><span> </span><span>%/%</span><span>10L</span><span>*</span><span>10L</span><span>,</span><span>
</span><span>arrr</span><span> </span><span>=</span><span> </span><span>piracing.stop</span><span> </span><span>-</span><span> </span><span>piracing.start</span><span>,</span><span>
</span><span>country</span><span> </span><span>=</span><span> </span><span>if_else</span><span>(</span><span>Country.of.origin</span><span> </span><span>%in%</span><span> </span><span>top.n.countries</span><span>(</span><span>10</span><span>),</span><span> </span><span>Country.of.origin</span><span>,</span><span> </span><span>&quot;other&quot;</span><span>)</span><span>
</span><span>)</span><span> </span><span>%>%</span><span> </span><span>filter</span><span>(</span><span>piracing.start</span><span> </span><span>></span><span> </span><span>1000</span><span>)</span><span> </span><span>%>%</span><span> </span><span>rowwise</span><span>()</span><span> </span><span>%>%</span><span> </span><span>do</span><span>(</span><span>
</span><span>data.frame</span><span>(</span><span>
</span><span>Name</span><span> </span><span>=</span><span> </span><span>.</span><span>$</span><span>Name</span><span>,</span><span>
</span><span>Life</span><span> </span><span>=</span><span> </span><span>.</span><span>$</span><span>Life</span><span>,</span><span> </span><span>piracing.start</span><span> </span><span>=</span><span> </span><span>.</span><span>$</span><span>piracing.start</span><span>,</span><span>
</span><span>piracing.stop</span><span> </span><span>=</span><span> </span><span>.</span><span>$</span><span>piracing.stop</span><span>,</span><span>
</span><span># need one row per decade active </span><span> </span><span>piracing.years</span><span> </span><span>=</span><span> </span><span>seq</span><span>(</span><span>.</span><span>$</span><span>piracing.start</span><span>,</span><span> </span><span>.</span><span>$</span><span>piracing.stop</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>10L</span><span>),</span><span>
</span><span>arrr</span><span> </span><span>=</span><span> </span><span>.</span><span>$</span><span>arrr</span><span>,</span><span>
</span><span>Country.of.origin</span><span> </span><span>=</span><span> </span><span>.</span><span>$</span><span>Country.of.origin</span><span>,</span><span>
</span><span>country</span><span> </span><span>=</span><span> </span><span>.</span><span>$</span><span>country</span><span>,</span><span> </span><span>stringsAsFactors</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>
</span><span>)</span><span>
</span><span>)</span></code></pre>
</figure>
<p>With the data now set up, we can set out to graph the pirate activity, and then also to build a random pirate generator!</p>
<h1>Pirates in Action</h1>
<p>Let’s take a look at the countries these famous pirates came from:</p>
<figure>
<pre><code data-lang="r"><span>pirates.raw</span><span> </span><span>%>%</span><span> </span><span>group_by</span><span>(</span><span>Country.of.origin</span><span>)</span><span> </span><span>%>%</span><span> </span><span>tally</span><span>(</span><span>sort</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span>%>%</span><span> </span><span>head</span><span>(</span><span>15</span><span>)</span></code></pre>
</figure>
<table>
<thead>
<tr>
<th>Country.of.origin</th>
<th>n</th>
</tr>
</thead>
<tbody>
<tr>
<td>England</td>
<td>108</td>
</tr>
<tr>
<td>Netherlands</td>
<td>47</td>
</tr>
<tr>
<td>France</td>
<td>39</td>
</tr>
<tr>
<td>Unknown</td>
<td>25</td>
</tr>
<tr>
<td>Colonial America</td>
<td>22</td>
</tr>
<tr>
<td>United States</td>
<td>19</td>
</tr>
<tr>
<td>Spain</td>
<td>10</td>
</tr>
<tr>
<td>China</td>
<td>9</td>
</tr>
<tr>
<td>Venezuela</td>
<td>8</td>
</tr>
<tr>
<td>Germany</td>
<td>7</td>
</tr>
<tr>
<td>Ireland</td>
<td>7</td>
</tr>
<tr>
<td>Wales</td>
<td>7</td>
</tr>
<tr>
<td>Scotland</td>
<td>6</td>
</tr>
<tr>
<td>Ottoman Empire</td>
<td>4</td>
</tr>
<tr>
<td>Portugal</td>
<td>4</td>
</tr>
</tbody>
</table>
<p>England is over-represented by quite a large margin, especially if you group Wales/Scotland/Ireland, and potentially Colonial America. The Netherlands and France come in second and third place, respectively. No big surprises here.</p>
<p>We’ll pull the data on pirate activity to build a graph of the overall pirate activity over the decades. This is a rather straightforward bar chart:</p>
<figure>
<pre><code data-lang="r"><span>## pirate activity per decade
</span><span>pirate.activity</span><span> </span><span>%>%</span><span> </span><span>ungroup</span><span>()</span><span> </span><span>%>%</span><span> </span><span>#ungroup.rowwise_df() %>% </span><span> </span><span>group_by</span><span>(</span><span>piracing.years</span><span>,</span><span> </span><span>country</span><span>)</span><span> </span><span>%>%</span><span> </span><span>tally</span><span>()</span><span> </span><span>%>%</span><span> </span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span>=</span><span>piracing.years</span><span>,</span><span> </span><span>y</span><span>=</span><span>n</span><span>))</span><span>+</span><span>
</span><span>geom_bar</span><span>(</span><span>aes</span><span>(</span><span>fill</span><span>=</span><span>country</span><span>,</span><span> </span><span>colour</span><span>=</span><span>country</span><span>),</span><span> </span><span>stat</span><span> </span><span>=</span><span> </span><span>&quot;identity&quot;</span><span>,</span><span> </span><span>position</span><span> </span><span>=</span><span> </span><span>&quot;stack&quot;</span><span>)</span><span>+</span><span>
</span><span># scale_fill_brewer(type = &quot;qual&quot;, palette = 3)+
</span><span> </span><span># scale_colour_brewer(type = &quot;qual&quot;, palette = 3)+
</span><span> </span><span>scale_fill_manual</span><span>(</span><span>values</span><span> </span><span>=</span><span> </span><span>my.colours</span><span>)</span><span>+</span><span>
</span><span>scale_colour_manual</span><span>(</span><span>values</span><span> </span><span>=</span><span> </span><span>my.colours</span><span>)</span><span>+</span><span>
</span><span>theme_bw</span><span>()</span><span>+</span><span>
</span><span>theme</span><span>(</span><span>plot.title</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>lineheight</span><span>=</span><span>.8</span><span>,</span><span> </span><span>face</span><span>=</span><span>&quot;bold&quot;</span><span>))</span><span>+</span><span>
</span><span>labs</span><span>(</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Arrr!tivity of famous pirates per decade\n \u2620\u2620\u2620&quot;</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;number of pirates active&quot;</span><span>
</span><span>)</span></code></pre>
</figure>
<p><img alt="pirate activity over the decades" src="https://i2.wp.com/opiateforthemass.es/images/safferli/piracy-country-time.png?w=456" data-recalc-dims="1"></p>
<p>One can nicely see the increase in pirate’s activity during the Golden Age (roughly 1560-1700). It drops shortly in 1700, building up again shortly thereafter. The middle of the 18th century is more or less free from pirates (at least those infamous enough to end up on Wikipedia), until piracy starts again around 1800 with the English-French wars. The Somalian pirates of the 2000s are the last on the graph!</p>
<p>This leads us to the next questions: Somalia was the heyday of pirates in the 2000s, but we cannot reasonably expect it to be in the 1600s. When was a country’s individual golden age of pirating?</p>
<p>To answer this, we’ll build a ridgeplot (formaly also known as a joyplot, from the Joy Division band t-shirt; the name is now <a rel="nofollow" href="https://en.wikipedia.org/wiki/House_of_Dolls" target="_blank">discouraged for obvious reasons</a>), showcasing the density plots of pirate activity per country.</p>
<figure>
<pre><code data-lang="r"><span>## piracy golden age by top n (15) countries
</span><span>pirates.dta</span><span> </span><span>%>%</span><span> </span><span>filter</span><span>(</span><span>piracing.start</span><span> </span><span>></span><span> </span><span>1000</span><span>)</span><span> </span><span>%>%</span><span> </span><span>filter</span><span>(</span><span>Country.of.origin</span><span> </span><span>%in%</span><span> </span><span>top.n.countries</span><span>(</span><span>15</span><span>))</span><span> </span><span>%>%</span><span> </span><span>ggplot</span><span>()</span><span>+</span><span>
</span><span>geom_density_ridges</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>piracing.start</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>Country.of.origin</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>Country.of.origin</span><span>))</span><span>+</span><span>
</span><span>theme_bw</span><span>()</span><span>+</span><span>
</span><span>theme</span><span>(</span><span>plot.title</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>lineheight</span><span>=</span><span>.8</span><span>,</span><span> </span><span>face</span><span>=</span><span>&quot;bold&quot;</span><span>))</span><span>+</span><span>
</span><span>labs</span><span>(</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Pirate activity by country\n \u2620\u2620\u2620&quot;</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;country&quot;</span><span>
</span><span>)</span></code></pre>
</figure>
<p><img alt="pirate activity over the decades; per country" src="https://i0.wp.com/opiateforthemass.es/images/safferli/piracy-by-country.png?w=456" data-recalc-dims="1"></p>
<p>The “usual suspects” add up nicely – England, the Netherlands, France. It’s interesting to see Germany show up as early (around 1350-1400). These are the pirates of the Hansa, mostly. Venezuela also had a “late blooming” pirate life in the 19th century, mostly. Interesting, I had not known about that!</p>
<h1>A Pirate’s Life For Me!</h1>
<p>With the historical data on pirates and their activity times, we can also try to generate a random pirate story! For this, we will restrict the data to the golden age of piracy, dropping all pirates which started their pirate life before 1500, and after 1750. We will want to create a random pirate with a statistically “correct” starting year of piracy, a statistically “correct” piracy tenure, and varnish him (or her) with a random pirate name!</p>
<h2>Golden Age Pirate: starting piracy year</h2>
<p>Let’s start with the pirating starting activity year.</p>
<figure>
<pre><code data-lang="r"><span>pirates.fit</span><span> </span><span>&lt;-</span><span> </span><span>pirates.dta</span><span> </span><span>%>%</span><span> </span><span>filter</span><span>(</span><span>piracing.start</span><span> </span><span>></span><span> </span><span>1500</span><span>)</span><span> </span><span>%>%</span><span> </span><span>filter</span><span>(</span><span>piracing.start</span><span> </span><span></span><span> </span><span>1750</span><span>)</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>
</span><span>arrr</span><span> </span><span>=</span><span> </span><span>piracing.stop</span><span> </span><span>-</span><span> </span><span>piracing.start</span><span>
</span><span>)</span><span>
</span><span>pirates.fit</span><span> </span><span>%>%</span><span> </span><span>ggplot</span><span>()</span><span>+</span><span>
</span><span>geom_density</span><span>(</span><span>aes</span><span>(</span><span>x</span><span>=</span><span>piracing.start</span><span>))</span></code></pre>
</figure>
<p><img alt="pirates' starting their trade" src="https://i1.wp.com/opiateforthemass.es/images/safferli/piracy-goldenage.png?w=456" data-recalc-dims="1"></p>
<p>Unfortunately, it’s a heavily left-skewed distribution, and after some hours of looking into distributions, I couldn’t find a good fit, and we’ll have to resort to pulling from the empirical distribution (ie, the data itself) for generating a random start year for a pirate:</p>
<figure>
<pre><code data-lang="r"><span>f.gen.pirating.startyear</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>()</span><span> </span><span>{</span><span>
</span><span># generate a startyear by pulling from the empirical distribution (pirates.fit$piracing.start)
</span><span> </span><span>base</span><span>::</span><span>sample</span><span>(</span><span>pirates.fit</span><span>$</span><span>piracing.start</span><span>,</span><span> </span><span>1</span><span>)</span><span>
</span><span>}</span></code></pre>
</figure>
<h2>Golden Age Pirate: piracing tenure</h2>
<p>We have better luck with the tenure of a pirate: the distribution follows a nice count-data distribution, I’m guessing a negative binomial.</p>
<figure>
<pre><code data-lang="r"><span>pirates.fit</span><span> </span><span>%>%</span><span> </span><span>ggplot</span><span>()</span><span>+</span><span>
</span><span>geom_density</span><span>(</span><span>aes</span><span>(</span><span>x</span><span>=</span><span>piracing.start</span><span>))</span></code></pre>
</figure>
<p><img alt="pirates' tenure" src="https://i0.wp.com/opiateforthemass.es/images/safferli/piracy-goldenage-tenure.png?w=456" data-recalc-dims="1"></p>
<p>As it’s discrete data, we’ll use the negative binomial distribution to estimate the theoretical distribution of a pirate’s tenure using <code>library(fitdistrplus)</code>.</p>
<figure>
<pre><code data-lang="r"><span>#library(fitdistrplus)
# check which distribution would be a good fit: #fitdistrplus::descdist(pirates.fit$arrr, boot = 1000)
</span><span>
</span><span># negative binomial
</span><span>f</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>fitdistrplus</span><span>::</span><span>fitdist</span><span>(</span><span>pirates.fit</span><span>$</span><span>arrr</span><span>,</span><span> </span><span>&quot;nbinom&quot;</span><span>)</span><span>
</span><span>fitdistrplus</span><span>::</span><span>plotdist</span><span>(</span><span>pirates.fit</span><span>$</span><span>arr</span><span>,</span><span> </span><span>&quot;nbinom&quot;</span><span>,</span><span> </span><span>para</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>size</span><span>=</span><span>f</span><span>1</span><span>$</span><span>estimate</span><span>[</span><span>1</span><span>],</span><span> </span><span>mu</span><span>=</span><span>f</span><span>1</span><span>$</span><span>estimate</span><span>[</span><span>2</span><span>]))</span></code></pre>
</figure>
<p>The results are excellent: we can clearly see the oversampling of “round” years (10, in particular), which comes from the fact that with uncertain dates, Wikipedia noted a pirate’s activity in decades, rather than actual years. But still, our distribution of <code>rnbinom(x, size = 0.3876683, mu = 6.395578)</code> works nicely.</p>
<p><img alt="pirates' tenure estimation" src="https://i1.wp.com/opiateforthemass.es/images/safferli/pirate-tenure-negbin.png?w=456" data-recalc-dims="1"></p>
<p>We can thus draw a random tenure for a pirate by using this function:</p>
<figure>
<pre><code data-lang="r"><span>f.gen.pirating.tenure</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>()</span><span> </span><span>{</span><span>
</span><span># generate a tenure time from the estimated theoretical neg binomial distribution (pirates.fit$arrr)
</span><span> </span><span>rnbinom</span><span>(</span><span>1</span><span>,</span><span> </span><span>size</span><span>=</span><span>f</span><span>1</span><span>$</span><span>estimate</span><span>[</span><span>1</span><span>],</span><span> </span><span>mu</span><span>=</span><span>f</span><span>1</span><span>$</span><span>estimate</span><span>[</span><span>2</span><span>])</span><span>
</span><span>}</span></code></pre>
</figure>
<h2>Golden Age Pirate: silly name generator</h2>
<p>All that is missing now is a random pirate name generator. <a rel="nofollow" href="http://www.fantasynamegenerators.com/pirate-names.php" target="_blank">Fantasy name generator</a> have a nice collection of name generators, and a pirate one is included! They allow the use of the names “to name anything in any of your own works”, so let’s use them here!</p>
<p>We source the names into R and create a simple function pulling a random name, allowing you to specify male or female.</p>
<figure>
<pre><code data-lang="r"><span>source</span><span>(</span><span>&quot;pirate-names.R&quot;</span><span>)</span><span>
</span><span>f.gen.pirate.name</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>female</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>{</span><span>
</span><span># generate a pirate name from a list of </span><span> </span><span># - first names (male/female)
</span><span> </span><span># - nick names (male/female)
</span><span> </span><span># - last names
</span><span> </span><span>if</span><span>(</span><span>female</span><span>)</span><span> </span><span>{</span><span>
</span><span>paste</span><span>(</span><span>
</span><span>base</span><span>::</span><span>sample</span><span>(</span><span>names.female</span><span>,</span><span> </span><span>1</span><span>),</span><span> </span><span>base</span><span>::</span><span>sample</span><span>(</span><span>names.nickf</span><span>,</span><span> </span><span>1</span><span>),</span><span> </span><span>base</span><span>::</span><span>sample</span><span>(</span><span>names.last</span><span>,</span><span> </span><span>1</span><span>)</span><span>
</span><span>)</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span>paste</span><span>(</span><span>
</span><span>base</span><span>::</span><span>sample</span><span>(</span><span>names.male</span><span>,</span><span> </span><span>1</span><span>),</span><span> </span><span>base</span><span>::</span><span>sample</span><span>(</span><span>names.nickm</span><span>,</span><span> </span><span>1</span><span>),</span><span> </span><span>base</span><span>::</span><span>sample</span><span>(</span><span>names.last</span><span>,</span><span> </span><span>1</span><span>)</span><span>
</span><span>)</span><span>
</span><span>}</span><span>
</span><span>}</span></code></pre>
</figure>
<h2>Golden Age Pirate: a pirate’s story!</h2>
<p>With all the pieces set up, we can now generate a random pirate:</p>
<figure>
<pre><code data-lang="r"><span>f.pirate.story</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>female</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>{</span><span>
</span><span># build the pirate story by joining name, startyear, and tenure time
</span><span> </span><span>yy</span><span> </span><span>&lt;-</span><span> </span><span>f.gen.pirating.startyear</span><span>()</span><span>
</span><span>rr</span><span> </span><span>&lt;-</span><span> </span><span>paste0</span><span>(</span><span>
</span><span>&quot;Your name is &quot;</span><span>,</span><span> </span><span>f.gen.pirate.name</span><span>(</span><span>female</span><span>),</span><span> </span><span>&quot; and you roamed the Caribbean from &quot;</span><span>,</span><span> </span><span>yy</span><span>,</span><span> </span><span>&quot; to &quot;</span><span>,</span><span> </span><span>yy</span><span>+</span><span>f.gen.pirating.tenure</span><span>(),</span><span>
</span><span>&quot;. Arrr! \u2620&quot;</span><span>
</span><span>)</span><span>
</span><span>return</span><span>(</span><span>rr</span><span>)</span><span>
</span><span>}</span></code></pre>
</figure>
<p><code>\u2620</code> is the utf-8 code for the <a rel="nofollow" href="https://emojipedia.org/emoji/%E2%98%A0/" target="_blank">skull-and-crossbones emoji</a>. Calling the function several times then results in:</p>
<table>
<thead>
<tr>
<th>female</th>
<th>story</th>
</tr>
</thead>
<tbody>
<tr>
<td>FALSE</td>
<td>Your name is Gerard ‘The Fierce’ Eastoft and you roamed the Caribbean from 1603 to 1605. Arrr! ☠</td>
</tr>
<tr>
<td>FALSE</td>
<td>Your name is Orton ‘Grommet’ Drachen and you roamed the Caribbean from 1657 to 1657. Arrr! ☠</td>
</tr>
<tr>
<td>FALSE</td>
<td>Your name is Ascot ‘Shadow’ Artemis and you roamed the Caribbean from 1650 to 1652. Arrr! ☠”</td>
</tr>
<tr>
<td>TRUE</td>
<td>Your name is Ethyl ‘Temptress’ Mitchell and you roamed the Caribbean from 1610 to 1625. Arrr! ☠</td>
</tr>
<tr>
<td>TRUE</td>
<td>Your name is Christine ‘Daffy’ Ward and you roamed the Caribbean from 1657 to 1657. Arrr! ☠</td>
</tr>
<tr>
<td>TRUE</td>
<td>Your name is Huldah ‘Twitching’ Vome and you roamed the Caribbean from 1718 to 1718. Arrr! ☠</td>
</tr>
</tbody>
</table>
<h1>Concluding remarks</h1>
<p>The code is available on <a rel="nofollow" href="https://github.com/safferli/pirates" target="_blank">github</a>, as always.</p>
<p>I wish I could find those historical shipping and piracy data again – any hints or pointers are very much appreciated!</p>
<p><a rel="nofollow" href="http://opiateforthemass.es/articles/pirates/" target="_blank">Talk like a pirate day 2017</a> was originally published by Kirill Pomogajko at <a rel="nofollow" href="http://opiateforthemass.es/" target="_blank">Opiate for the masses</a> on September 19, 2017.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://opiateforthemass.es/articles/pirates/"> Opiate for the masses</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/OnbFTtVTJZA" height="1"></a></div>