The idea behind hierarchical cluster analysis is to show which of a (potentially large) set
of samples are most similar to one another, and to group these similar samples in the same
limb of a tree.
Each of the samples can be thought of a sitting in an m-dimensional space, defined by the m variables (columns) in the dataframe. We define similarity on the basis of the distance between two samples in this m-dimensional space.
Several different distance measures could be used, but the default is Euclidean distance and this is used to work out the distance from every sample to every other sample.
for the other options, check
This quantitative dissimilarity structure of the data is stored in a matrix produced by the “dist function”.
Initially, each sample is assigned to its own cluster, and then the hclust algorithm proceeds iteratively, at each stage joining the two most similar clusters, continuing until there is just a single cluster.
for more details about hclust function, check
Here in this example we will cluster the similar countries on the basis of similarity. so the decision making can be easier. In order to find the similarities in observation and group the data, we need to perform cluster analysis.
|country||area||gdp||inflation||life expect||military||pop growth||unemployment|
In this example we will use hierarchical cluster analysis to group the countries. This cluster analysis also allows us to summarise the data by grouping all the similar observation into different clusters. These observations are made by considering similar values for number of variables. i.e. if the eucladien distance between two values is less than they are group together we can perform cluster analysis with the dist and hclust function.
dist function:- calculates a distance matrix of the provided values and provides the eucledian distance between those values by default. from the calculated eucladien distance hierarchical clustering can be derived, to perform this we use the hclust function.
The hclust function has a method attributes that specifies hows the clustering is to be done. The method includes average, gord, single, median complete and centroid methods. The complete linkage method being the default.
Steps to make hierarchical clustering in R
step1). First we load the dataset in R workspace and saved it in variable name- data
step2). the syntax to perform hierarchical cluster is hclust of dist of dataset name
Saving the hclust in variable name surveyclust
-1 is to remove the first column i.e country name, since it does not have logical relationship with the data.
step3). plot the denddogram
clustering result variable to plot the dendrogram
the numbers you are seeing on dendrogram plot is country in the table
countries are plot based on their similarities
step4). we can also make clusters from these dendrograms using
model name and number of argument(number of clusters)
the dendrogram now will show 5 clusters in color.