How to Perform Hierarchical Clustering in RStudio

Learn how to perform hierarchical clustering in RStudio using the agnes() function from the cluster package and how to visualize and evaluate your clu

Key Points

Hierarchical clustering is a type of unsupervised learning that groups observations based on their similarity or dissimilarity without specifying the number of clusters beforehand.

To perform hierarchical clustering in RStudio, you must install and load two packages: factoextra and cluster. Then, you need to scale your data using the scale() function and perform hierarchical clustering using the agnes() function from the cluster package.

To visualize and interpret your clustering results, you can use a dendrogram, a tree-like diagram showing how the clusters are nested within each other. You can plot a dendrogram using the fviz_dend() function from the factoextra package.

Read More and Get Code: https://www.data03.online/2023/08/hierarchical-clustering-rstudio.html

## Loading required package: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
##            Murder Assault UrbanPop Rape
## Alabama      13.2     236       58 21.2
## Alaska       10.0     263       48 44.5
## Arizona       8.1     294       80 31.0
## Arkansas      8.8     190       50 19.5
## California    9.0     276       91 40.6
## Colorado      7.9     204       78 38.7
##                Murder   Assault   UrbanPop         Rape
## Alabama    1.24256408 0.7828393 -0.5209066 -0.003416473
## Alaska     0.50786248 1.1068225 -1.2117642  2.484202941
## Arizona    0.07163341 1.4788032  0.9989801  1.042878388
## Arkansas   0.23234938 0.2308680 -1.0735927 -0.184916602
## California 0.27826823 1.2628144  1.7589234  2.067820292
## Colorado   0.02571456 0.3988593  0.8608085  1.864967207
## Call:     agnes(x = USArrests.scaled, method = "ward") 
## Agglomerative coefficient:  0.934621 
## Order of objects:
##  [1] Alabama        Louisiana      Georgia        Tennessee      Mississippi   
##  [6] South Carolina North Carolina Alaska         California     Nevada        
## [11] Colorado       Arizona        Maryland       New Mexico     Michigan      
## [16] Florida        Illinois       New York       Texas          Arkansas      
## [21] Kentucky       Virginia       Wyoming        Connecticut    Rhode Island  
## [26] Massachusetts  New Jersey     Delaware       Hawaii         Utah          
## [31] Indiana        Kansas         Oklahoma       Ohio           Pennsylvania  
## [36] Missouri       Oregon         Washington     Idaho          Montana       
## [41] Nebraska       Iowa           New Hampshire  Maine          Minnesota     
## [46] Wisconsin      North Dakota   Vermont        South Dakota   West Virginia 
## Height (summary):
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.2059  0.7781  1.0911  1.8089  1.8118 13.5162 
## 
## Available components:
## [1] "order"     "height"    "ac"        "merge"     "diss"      "call"     
## [7] "method"    "order.lab" "data"
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## ℹ The deprecated feature was likely used in the factoextra package.
##   Please report the issue at <https://github.com/kassambara/factoextra/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

## [1] 1 2 2 3
## grp
##  1  2  3  4 
##  7 12 19 12
## [1] "Alabama"        "Georgia"        "Louisiana"      "Mississippi"   
## [5] "North Carolina" "South Carolina" "Tennessee"
##  [1] "Alaska"     "Arizona"    "California" "Colorado"   "Florida"   
##  [6] "Illinois"   "Maryland"   "Michigan"   "Nevada"     "New Mexico"
## [11] "New York"   "Texas"
##  [1] "Arkansas"      "Connecticut"   "Delaware"      "Hawaii"       
##  [5] "Indiana"       "Kansas"        "Kentucky"      "Massachusetts"
##  [9] "Missouri"      "New Jersey"    "Ohio"          "Oklahoma"     
## [13] "Oregon"        "Pennsylvania"  "Rhode Island"  "Utah"         
## [17] "Virginia"      "Washington"    "Wyoming"
##  [1] "Idaho"         "Iowa"          "Maine"         "Minnesota"    
##  [5] "Montana"       "Nebraska"      "New Hampshire" "North Dakota" 
##  [9] "South Dakota"  "Vermont"       "West Virginia" "Wisconsin"

Find Us on Social Media Facebook: https://www.facebook.com/DataAnalysis03 Instagram: https://www.instagram.com/dataanalysis03/ Twitter: https://www.twitter.com/Zubair01469079/ Youtube: https://www.youtube.com/@data.03?sub-confirmation=1 Whatsapp Community: https://chat.whatsapp.com/IGjjvZVsGCiLn5jSmrFQaJ Telegram Channel: https://t.me/dataanalysis03 Medium: https://medium.com/@zubairishaq8305 Quora: https://www.quora.com/profile/Muhammad-Zubair-Ishaq Google News: https://news.google.com/publications/CAAqBwgKMIaV0QswxbDoAw?hl=en-PK&gl=PK&ceid=PK%3Aen