<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/hierarchical-clustering-exercises-beginner/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2016/12/14/hierarchical-clustering-exercises-beginner/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="http://r-exercises.com/wp-content/uploads/2016/12/hierarchy-261985__340.jpg?x61433"><img sizes="(max-width: 150px) 100vw, 150px" alt="" class="wp-image-3462" src="https://i0.wp.com/r-exercises.com/wp-content/uploads/2016/12/hierarchy-261985__340-150x150.jpg?resize=150%2C150" data-recalc-dims="1"></a><br>
Grouping objects into clusters is a frequent task in data analysis. In this set of exercises we will use <strong>hierarchical clustering</strong> to cluster European capitals based on their latitude and longitude. Before trying out this exercise please make sure that you are familiar with the following functions: <code>dist</code>, <code>hlcust</code>, <code>cutree</code>, <code>rect.hclust</code></p>
<p>We will be using a custom-made dataset. Before starting the exercise please run the following code to obtain the capital locations for Europe (note that you will need to have <code>ggmap</code> library installed):</p>
<p><code><br>
library(ggmap)<br>
capitals &lt;- c(&quot;Albania, Tirana&quot;, &quot;Andorra, Andorra la Vella&quot;, &quot;Armenia, Yerevan&quot;, &quot;Austria, Vienna&quot;, &quot;Azerbaijan, Baku&quot;, &quot;Belarus, Minsk&quot;, &quot;Belgium, Brussels&quot;, &quot;Bosnia and Herzegovina, Sarajevo&quot;, &quot;Bulgaria, Sofia&quot;, &quot;Croatia, Zagreb&quot;, &quot;Cyprus, Nicosia&quot;, &quot;Czech Republic, Prague&quot;, &quot;Denmark, Copenhagen&quot;, &quot;Estonia, Tallinn&quot;, &quot;Finland, Helsinki&quot;, &quot;France, Paris&quot;, &quot;Germany, Berlin&quot;, &quot;Greece, Athens&quot;, &quot;Georgia, Tbilisi&quot;, &quot;Hungary, Budapest&quot;, &quot;Iceland, Reykjavik&quot;, &quot;Italy, Rome&quot;, &quot;Latvia, Riga&quot;, &quot;Kazakhstan, Astana&quot;, &quot;Liechtenstein, Vaduz&quot;, &quot;Lithuania, Vilnius&quot;, &quot;Luxembourg, Luxembourg&quot;, &quot;Macedonia, Skopje&quot;, &quot;Malta, Valletta&quot;, &quot;Moldova, Chişinău&quot;, &quot;Monaco, Monaco-Ville&quot;, &quot;Montenegro, Podgorica&quot;, &quot;Netherlands, Amsterdam&quot;, &quot;Norway, Oslo&quot;, &quot;Poland, Warsaw&quot;, &quot;Portugal, Lisbon&quot;, &quot;Republic of Ireland, Dublin&quot;, &quot;Romania, Bucharest&quot;, &quot;Russia, Moscow&quot;, &quot;San Marino, San Marino&quot;, &quot;Serbia, Belgrade&quot;, &quot;Slovakia, Bratislava&quot;, &quot;Slovenia, Ljubljana&quot;, &quot;Spain, Madrid&quot;, &quot;Sweden, Stockholm&quot;, &quot;Switzerland, Bern&quot;, &quot;Turkey, Ankara&quot;, &quot;Ukraine, Kiev&quot;, &quot;United Kingdom, London&quot;, &quot;Vatican City, Vatican City&quot;)<br>
theData &lt;- geocode(capitals)<br>
rownames(theData) &lt;- capitals<br>
</code></p>
<p>Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2016/12/14/hierarchical-clustering-solutions-beginner/">here</a>.</p>
<p>If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.</p>
<p><strong>Exercise 1</strong><br>
Calculate the Euclidean latitude/longitude distances between all pairs of capital cities.</p>
<p><strong>Exercise 2</strong><br>
Use the obtained distances to produce the hierarchical clustering dendrogram object. Use all the default parameters.<br>
NOTE: By default the clusters will be merged together using the maximum possible distance between all pairs of their elements (this fact will be useful later).</p>
<p><strong>Exercise 3</strong><br>
Visualize the obtained hierarchical clustering dendrogram.</p>
<p><strong>Exercise 4</strong><br>
In the previous step the leaves of our dendrogram were placed at different heights. Let’s redo the plot so that all capital names are written at the same level.</p>
<p><strong>Exercise 5</strong><br>
Hierarchical clustering procedure builds a hierarchy of clusters. One advantage of this method is that we can use the same dendrogram to obtain different numbers of groups.<br>
Cluster the European capitals into 3 groups.</p>
<p><strong>Exercise 6</strong><br>
Instead of specifying the wanted number of groups we can select the dendrogram height where the tree will be partitioned into groups. Since we used the maximum linkage function (default in exercise 2) this height has a useful interpretation – it ensures that all elements within one cluster are not more than the selected distance apart.<br>
a) Cluster the European capitals by cutting the tree at height=20.<br>
b) Plot the dendrogram and visualize the height at which the tree was cut into groups using a line.</p>
<p><strong>Exercise 7</strong><br>
Now visualize the clustering solution obtained in the 5th exercise on the dendrogram plot. This should be done by drawing a rectangle around all capitals that fall in the same group. Use different colors for different groups.</p>
<p><strong>Exercise 8</strong><br>
Visualize the dendrogram again but this time present both cluster versions obtained in exercise 5 and exercise 6 on the same plot. Use red color to represent exercise 5 clusters and blue to represent clusters from exercise 6.</p>
<p><strong>Exercise 9</strong><br>
The <code>hclust</code> function has 8 implemented different linkage methods – methods used to merge two clusters when building the dendrogram. We want to experiment with all of them.<br>
Produce a dendrogram, obtain 5 groups and vizualize them using different color rectangles. Repeat this for all available linkage methods.</p>
<p><strong>Exercise 10</strong><br>
Design your own clustering solution based on what you learned in this exercise and visualize it as a map.<br>
Plot capital coordinates with longitude on the x-axis and latitude on the y-axis and color them based on the groups obtained using your hierarchical clustering version.</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://r-exercises.com/2016/12/14/hierarchical-clustering-exercises-beginner/"> R-exercises</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/yzvMXK9HcmI"></a></div>