<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/sri-lanka-visitor-arrivals-by-ellis2013nz/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://freerangestats.info/blog/2018/09/26/sri-lanka-arrivals"> free range statistics - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img src="https://i0.wp.com/freerangestats.io/img/0134-P7220028.JPG?w=180" align="right" title="Colombo skyline from the Red Cinnamon hotel. Source: Peter Ellis." data-recalc-dims="1"></p>
<p>So I’ve been holidaying in Sri Lanka for a while now, and thought I would do a brief blog post using some public tourism data. Tourism has grown rapidly in Sri Lanka since the <a rel="nofollow" href="https://en.wikipedia.org/wiki/Sri_Lankan_Civil_War" target="_blank">civil war</a> finished in 2009. Travelling around, it’s easy to see why. Sri Lanka excels in the <em>range</em> of attractions it has to offer visitors. These include beaches, history and ruins (from ancient times right down to facilities from the civil war), forests, wildlife, and an interesting contemporary cultural and religious mix around the island.</p>
<p>Sri Lanka has <a rel="nofollow" href="https://en.wikipedia.org/wiki/World_Heritage_Sites_of_Sri_Lanka" target="_blank">eight UNESCO world heritage sites</a>, mostly relating to history and culture, but with some interesting biosphere / environmental locations too.</p>
<h2>Arrivals data basics</h2>
<p>The Sri Lanka Tourism Development Authority publishes <a rel="nofollow" href="http://www.sltda.lk/statistics" target="_blank">a range of statistical and research products</a>.</p>
<p><img src="https://i1.wp.com/freerangestats.io/img/0134-P9220209.JPG?w=250" align="left" title="Sigiriya rock fortress, including tourists climbing to the top.  Source: Peter Ellis." data-recalc-dims="1"></p>
<p>As with other national tourism statistical systems in a controlled-border situation, the base of the tourism data comes from counting visitors crossing the border. All people entering Sri Lanka complete a physical arrival card with basic information. Aggregate results are published promptly in <a rel="nofollow" href="http://www.sltda.lk/node/765" target="_blank">monthly reports</a>.</p>
<p>Past years’ detailed data are available only in awkward PDF documents that I wasn’t able to efficiently scrape in the time available (I’m on holiday, ok?). However, detailed data from January 2018 is being published in Excel, and from 2014 to 2017 the summary data is easy enough to <a rel="nofollow" href="https://github.com/ellisp/blog-source/blob/master/_working/sl-tourism/monthly-totals.xlsx" target="_blank">copy into a spreadsheet</a>.</p>
<p>Data on “days spent in the country” (combining arrivals and departures, sometimes known as “stay days” or “total nights”) would be a better measure but are harder to deliver. They aren’t readily available.</p>
<p>Here’s what the main story looks like up to August 2018, including a forecast out for another 18 months:</p>
<p><img width="100%" src="http://freerangestats.io/img/0134-forecast.svg"></p>
<p>That forecast is done with <code>auto.arima</code> from Hyndman’s <code>forecast</code> R package, using a logarithm transform because the variance increases roughly proportional to the average number of visitors. The code for today’s post, which is all unremarkable, is in one chunk at the bottom of the post.</p>
<p>Here’s a different look at the seasonality and growth, putting month of the year on the X axis and giving a separate line to each year’s arrivals figures:</p>
<p><img width="100%" src="http://freerangestats.io/img/0134-seasonality.svg"></p>
<p>This visualization is better at showing the two peak periods of tourism per year. These two seasons are partly driven by the weather – Sri Lanka experiences <a rel="nofollow" href="http://www.meteo.gov.lk/index.php?option=com_content&view=article&id=94&Itemid=310&lang=en" target="_blank">two monsoons per year which impact differently on different parts of the island</a> – and partly by holidays and other seasonal features in the origin market countries.</p>
<p>These images also suggest (to me) a slowing in arrivals growth since 2015, but without a longer time series (at least back to the end of the war) it’s difficult to know how substantive that would be.</p>
<h2>Seasonality and country of origin</h2>
<p>I was interested to see which were the main market countries for Sri Lankan tourism, and if seasonality differed by country of residence. For this, the only data convenient for me was the Excel workbook with just the first eight months of 2018’s arrival numbers by country. The missing last four months of the year create an obvious limitation, but we’ve at least got a starting point.</p>
<p>Here’s a chart showing the top 15 countries:</p>
<p><img width="100%" src="http://freerangestats.io/img/0134-country-barchart.svg"></p>
<p>Unsurprisingly, neighbouring India is both the largest single source of visitors (which includes purposes of visit other than holiday making, such as visiting family, medical or religious purposes), and has a slightly different seasonal mix from other markets. Indian visitors spiked in May 2018 for a reason I don’t know but would doubtless be clear to someone more familiar with the situation. However, May is a low period for other key countries such as the UK and Australia, as well as the “other” category that holds all unlisted countries.</p>
<p>Here’s a line chart that converts each country’s monthly arrivals into a percentage of the total arrivals from that country in the first eight months of 2018:</p>
<p><img width="100%" src="http://freerangestats.io/img/0134-country-seasonality.svg"></p>
<p>The story is the same, but perhaps a little clearer particularly for the countries towards the bottom of the chart. We see very different seasonality patterns for visitors from Saudi Arabia compared to Russia and Ukraine, for example. A good reminder that climate in the country of origin can matter as much as in the destination country!</p>
<p>Finally, here’s a more complex graphic that tries to show the results of principal component analysis, seeking to summarise similarities or differences between the various source countries. The principal components analysis behind this biplot uses as source data the proportions calculated in the step above as the basis for a matrix with country of residence as rows, and months as columns.</p>
<p><img width="100%" src="http://freerangestats.io/img/0134-country-seasonality-pc.svg"></p>
<p>This chart shows, for example, that (based on just eight months’ data) visitor arrivals from Saudi Arabia and Australia have very different seasonal patterns, whereas Ukraine and Russia are very similar. This is useful for analytical and exploratory purposes, but my track record in being able to explain such images to non-statistical audiences (or anyone, in fact) is challenged so I wouldn’t necessarily recommend it for broad use.</p>
<h2>R code</h2>
<p>Here’s the code behind today’s post:</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>openxlsx</span><span>)</span><span>
</span><span>library</span><span>(</span><span>lubridate</span><span>)</span><span>
</span><span>library</span><span>(</span><span>forecast</span><span>)</span><span>
</span><span>#==============download data=================
</span><span>
</span><span>dir.create</span><span>(</span><span>&quot;sl-tourism&quot;</span><span>)</span><span>
</span><span># From the actual website for the detailed data by country, just for 2018:
</span><span>download.file</span><span>(</span><span>&quot;http://www.sltda.gov.lk/sites/default/files/monthly-international-tourist-arrivals-2018.xlsx&quot;</span><span>,</span><span>
</span><span>destfile</span><span> </span><span>=</span><span> </span><span>&quot;sl-tourism/monthly-international-tourist-arrivals-2018.xlsx&quot;</span><span>,</span><span>
</span><span>mode</span><span> </span><span>=</span><span> </span><span>&quot;wb&quot;</span><span>)</span><span>
</span><span># From my home-made hack for the past years, totals only:
</span><span>download.file</span><span>(</span><span>&quot;https://github.com/ellisp/blog-source/raw/master/_working/sl-tourism/monthly-totals.xlsx&quot;</span><span>,</span><span>
</span><span>destfile</span><span> </span><span>=</span><span> </span><span>&quot;sl-tourism/monthly-totals.xlsx&quot;</span><span>,</span><span>
</span><span>mode</span><span> </span><span>=</span><span> </span><span>&quot;wb&quot;</span><span>)</span><span>
</span><span># There are PDFs of detail by country by month (one PDF for each year-month combination) but I wasn't able to # systematically scrape them all in in the time available.
</span><span>
</span><span>#==============total arrivals by month========================
</span><span>
</span><span># file name:
</span><span>fn</span><span> </span><span>&lt;-</span><span> </span><span>&quot;sl-tourism/monthly-totals.xlsx&quot;</span><span>
</span><span># sheet names:
</span><span>sns</span><span> </span><span>&lt;-</span><span> </span><span>2018</span><span>:</span><span>2015</span><span>
</span><span># handy vector of month titles in full for later use:
</span><span>mons</span><span> </span><span>&lt;-</span><span> </span><span>month</span><span>(</span><span>1</span><span>:</span><span>12</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>abbr</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span># Import all sheets
</span><span>all_sheets</span><span> </span><span>&lt;-</span><span> </span><span>lapply</span><span>(</span><span>sns</span><span>,</span><span> </span><span>function</span><span>(</span><span>x</span><span>){</span><span>
</span><span>tmp</span><span> </span><span>&lt;-</span><span> </span><span>read.xlsx</span><span>(</span><span>fn</span><span>,</span><span> </span><span>sheet</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>x</span><span>))[,</span><span> </span><span>1</span><span>:</span><span>3</span><span>]</span><span>
</span><span>tmp</span><span> </span><span>&lt;-</span><span> </span><span>tmp</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>year_orig</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>-</span><span>Month</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>year</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>str_sub</span><span>(</span><span>year_orig</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>4</span><span>)),</span><span>
</span><span>status</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>grepl</span><span>(</span><span>&quot;\\*$&quot;</span><span>,</span><span> </span><span>year_orig</span><span>),</span><span> </span><span>&quot;Provisional&quot;</span><span>,</span><span> </span><span>&quot;Final&quot;</span><span>),</span><span>
</span><span>Month</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>Month</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>levels</span><span>(</span><span>mons</span><span>)),</span><span>
</span><span>value</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>value</span><span>))</span><span> </span><span>return</span><span>(</span><span>tmp</span><span>)</span><span>
</span><span>})</span><span>
</span><span># Combine into a single data frame and tidy up a bit eg remove duplicates:
</span><span>monthly</span><span> </span><span>&lt;-</span><span> </span><span>do.call</span><span>(</span><span>&quot;rbind&quot;</span><span>,</span><span> </span><span>all_sheets</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>yrmon</span><span> </span><span>=</span><span> </span><span>year</span><span> </span><span>+</span><span> </span><span>(</span><span>as.numeric</span><span>(</span><span>Month</span><span>)</span><span> </span><span>-</span><span> </span><span>0.5</span><span>)</span><span> </span><span>/</span><span> </span><span>12</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>status</span><span> </span><span>==</span><span> </span><span>&quot;Final&quot;</span><span> </span><span>|</span><span> </span><span>year</span><span> </span><span>==</span><span> </span><span>2018</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>yrmon</span><span>)</span><span>
</span><span>#-------------forecast-------------------
# put into a time series object
</span><span>monthly_ts</span><span> </span><span>&lt;-</span><span> </span><span>ts</span><span>(</span><span>monthly</span><span>$</span><span>value</span><span>,</span><span> </span><span>frequency</span><span> </span><span>=</span><span> </span><span>12</span><span>,</span><span> </span><span>start</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>2014</span><span>,</span><span> </span><span>1</span><span>))</span><span>
</span><span># check what sort of transformation makes sense. Recommends &lt; 0, so will use 0 (log) for simplicity:
</span><span>BoxCox.lambda</span><span>(</span><span>monthly_ts</span><span>)</span><span>
</span><span># fit model:
</span><span>mod</span><span> </span><span>&lt;-</span><span> </span><span>auto.arima</span><span>(</span><span>monthly_ts</span><span>,</span><span> </span><span>lambda</span><span> </span><span>=</span><span> </span><span>0</span><span>)</span><span>
</span><span># draw graph with forecast:
</span><span>autoplot</span><span>(</span><span>forecast</span><span>(</span><span>mod</span><span>,</span><span> </span><span>h</span><span> </span><span>=</span><span> </span><span>18</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>&quot;Visitor arrivals to Sri Lanka per month\n(Shaded blue area shows forecasts and uncertainty)&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>comma</span><span>)</span><span> </span><span>+</span><span>
</span><span>coord_cartesian</span><span>(</span><span>ylim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>80000</span><span>,</span><span> </span><span>300000</span><span>))</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Growth has slowed slightly since 2015&quot;</span><span>,</span><span>
</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;Prediction interval level of confidence (%):&quot;</span><span>)</span><span>
</span><span>#--------------more detailed seasonality plot----------------
</span><span>monthly</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>Month</span><span>),</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>value</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>as.ordered</span><span>(</span><span>year</span><span>)))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>&quot;&quot;</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>12</span><span>,</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>month</span><span>(</span><span>1</span><span>:</span><span>12</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>abbr</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>),</span><span> </span><span>minor_breaks</span><span> </span><span>=</span><span> </span><span>NULL</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_color_brewer</span><span>(</span><span>&quot;Year&quot;</span><span>,</span><span> </span><span>palette</span><span> </span><span>=</span><span> </span><span>&quot;Oranges&quot;</span><span>,</span><span> </span><span>guide</span><span> </span><span>=</span><span> </span><span>guide_legend</span><span>(</span><span>reverse</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>&quot;Arrivals per month&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>comma</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;right&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Visitor arrivals to Sri Lanka by month&quot;</span><span>,</span><span>
</span><span>&quot;Two peak seasons (Dec-Mar and Jul-Aug) are clearly visible, as is the strong growth in recent years.&quot;</span><span>)</span><span>
</span><span>#================country detail==================
# Import and tidy the detailed country data for first months of 2018:
</span><span>detail</span><span> </span><span>&lt;-</span><span> </span><span>read.xlsx</span><span>(</span><span>&quot;sl-tourism/monthly-international-tourist-arrivals-2018.xlsx&quot;</span><span>,</span><span>
</span><span>startRow</span><span> </span><span>=</span><span> </span><span>4</span><span>)</span><span> </span><span>%>%</span><span>
</span><span># get rid of the total:
</span><span> </span><span>filter</span><span>(</span><span>X</span><span>1</span><span> </span><span>!=</span><span> </span><span>&quot;TOTAL&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span># only keep the COUNTRY and the columns that are months:
</span><span> </span><span>select</span><span>(</span><span>COUNTRY</span><span>,</span><span> </span><span>one_of</span><span>(</span><span>toupper</span><span>(</span><span>mons</span><span>)))</span><span> </span><span>%>%</span><span>
</span><span># tidy:
</span><span> </span><span>rename</span><span>(</span><span>country</span><span> </span><span>=</span><span> </span><span>COUNTRY</span><span>)</span><span> </span><span>%>%</span><span> </span><span>gather</span><span>(</span><span>month</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>-</span><span>country</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>value</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>country2</span><span> </span><span>=</span><span> </span><span>str_to_title</span><span>(</span><span>fct_lump</span><span>(</span><span>country</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>15</span><span>,</span><span> </span><span>w</span><span> </span><span>=</span><span> </span><span>value</span><span>)),</span><span>
</span><span>country2</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot; Of&quot;</span><span>,</span><span> </span><span>&quot; of&quot;</span><span>,</span><span> </span><span>country2</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>country2</span><span>,</span><span> </span><span>month</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>value</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>value</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>(</span><span>month</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>month</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>str_to_title</span><span>(</span><span>month</span><span>),</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>levels</span><span>(</span><span>mons</span><span>)),</span><span>
</span><span>country2</span><span> </span><span>=</span><span> </span><span>fct_reorder</span><span>(</span><span>country2</span><span>,</span><span> </span><span>-</span><span>value</span><span>))</span><span> </span><span># Straight barchart of numbers:
</span><span>detail</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>month</span><span>),</span><span> </span><span>weight</span><span> </span><span>=</span><span> </span><span>value</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_bar</span><span>()</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>country2</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>&quot;&quot;</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>12</span><span>,</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>month</span><span>(</span><span>1</span><span>:</span><span>12</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>abbr</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>),</span><span> </span><span>minor_breaks</span><span> </span><span>=</span><span> </span><span>NULL</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>&quot;Visitor arrivals per month&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>comma</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Visitor arrivals to Sri Lanka by country and month&quot;</span><span>,</span><span> </span><span>&quot;Top 15 countries, first eight months of 2018. Differing seasonal patterns for different market countries are clear.&quot;</span><span>)</span><span>
</span><span># Calculate proportions so can see different countries on the same scale:
</span><span>detail2</span><span> </span><span>&lt;-</span><span> </span><span>detail</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>country2</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>prop</span><span> </span><span>=</span><span> </span><span>value</span><span> </span><span>/</span><span> </span><span>sum</span><span>(</span><span>value</span><span>))</span><span>
</span><span>detail2</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>month</span><span>),</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>prop</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>&quot;&quot;</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>12</span><span>,</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>month</span><span>(</span><span>1</span><span>:</span><span>12</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>abbr</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>),</span><span> </span><span>minor_breaks</span><span> </span><span>=</span><span> </span><span>NULL</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>&quot;Percentage of first eight months' visitor arrivals&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>country2</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Different peak seasons for different countries&quot;</span><span>)</span><span>
</span><span>#------------------Principal components analysis-------
</span><span>
</span><span># make wide version of the data (one column per month):
</span><span>detail3</span><span> </span><span>&lt;-</span><span> </span><span>detail2</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>country2</span><span>,</span><span> </span><span>month</span><span>,</span><span> </span><span>prop</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>spread</span><span>(</span><span>month</span><span>,</span><span> </span><span>prop</span><span>)</span><span>
</span><span># convert to a matrix and fit principal components to it:
</span><span>detail_m</span><span> </span><span>&lt;-</span><span> </span><span>as.matrix</span><span>(</span><span>detail3</span><span>[</span><span> </span><span>,</span><span> </span><span>-1</span><span>])</span><span>
</span><span>row.names</span><span>(</span><span>detail_m</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>detail3</span><span>$</span><span>country2</span><span>
</span><span>detail_pc</span><span> </span><span>&lt;-</span><span> </span><span>prcomp</span><span>(</span><span>detail_m</span><span>)</span><span>
</span><span># visualize:
</span><span>par</span><span>(</span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;Roboto&quot;</span><span>,</span><span> </span><span>font.main</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span>par</span><span>(</span><span>bg</span><span> </span><span>=</span><span> </span><span>&quot;grey99&quot;</span><span>)</span><span>
</span><span>biplot</span><span>(</span><span>detail_pc</span><span>,</span><span> </span><span>choices</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>2</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;darkblue&quot;</span><span>,</span><span> </span><span>&quot;grey75&quot;</span><span>),</span><span> </span><span>pc.biplot</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>xlim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>-3</span><span>,</span><span> </span><span>3</span><span>),</span><span> </span><span>xlab</span><span> </span><span>=</span><span> </span><span>&quot;Countries with peaks in the same months are grouped close together&quot;</span><span>,</span><span> </span><span>ylab</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Comparison of seasonality patterns by country&quot;</span><span>,</span><span>
</span><span>sub</span><span> </span><span>=</span><span> </span><span>&quot;(Data only from the first eight months of 2018)&quot;</span><span>,</span><span> </span><span>col.axis</span><span> </span><span>=</span><span> </span><span>&quot;transparent&quot;</span><span>)</span></code></pre>
</figure>
<p>Finally, here’s the view from the guesthouse where I’m writing this, in the hills above Kandy in central Sri Lanka:</p>
<p><img title="View westwards from guesthouse near Kandy. Source: Peter Ellis." src="https://i2.wp.com/freerangestats.io/img/0134-P9240358.JPG?w=100%25" data-recalc-dims="1"></p>
<p>Nice.</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://freerangestats.info/blog/2018/09/26/sri-lanka-arrivals"> free range statistics - R</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/KwSpD2ms064" height="1"></a></div>