🏡 Welcome to the seventh computer lab for the Science/Health stream of STM1001.
In this computer lab we will carry out a variety of clustering analyses using penguin data from the palmerpenguins R package (Horst et al. 2020).
By the end of this lab, you should feel comfortable applying various clustering techniques in jamovi. Let’s get started!
🏡 This week’s computer lab is based on the Topic 7B lecture , where we discussed Clustering. If you did not attend the lecture and have not yet reviewed the recording, you can review the slides here .
🏡 In this computer lab, we will be analysing the penguins data set from the palmerpenguins R package (Horst et al. 2020). It contains recent data on 3 species of penguin (Adelie, Chinstrap and Gentoo) living on islands in the Palmer archipelago, off the coast of Antarctica. We will use this data for our clustering analyses.
This penguin data contains information on numerous variables. The table below provides an example of some recorded values for these different variables (the column names), for 3 different penguins:
| species | island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex | year |
|---|---|---|---|---|---|---|---|
| Adelie | Torgersen | 39.1 | 18.7 | 181 | 3750 | male | 2007 |
| Gentoo | Biscoe | 49.2 | 15.2 | 221 | 6300 | male | 2007 |
| Chinstrap | Dream | 45.5 | 17.0 | 196 | 3500 | female | 2008 |
🏡 For the purposes of this computer lab, we will assume that we don’t actually have data on the species of each penguin in the penguins data set.
Instead, we will conduct cluster analyses using the other information contained in this data set.
If the cluster technique works well, we may end up with clusters for each penguin species!
The penguins_jamovi.csv file on the LMS has been prepared for our cluster analysis. This preparation included normalisation of the data (this process is like the standardisation process covered in section 4.2 of Topic 3). The penguins_jamovi.csv data set contains the following variables:
index: Row number, can be used as identifierbill_length_mm, bill_depth_mm, flipper_length_mm and body_mass_gspecies (Adelie, Gentoo or Chinstrap)species_numeric: Species stored in numeric format, i.e. with a number from 1-3 to represent each species.🏡 Before moving on to the next question, carry out the following steps:
penguins_jamovi.csv from the LMS and open it in jamovi.snowCluster module (Seol 2022; Kassambara and Mundt 2020) in jamovi (The jamovi project 2022).💻 We will now begin begin our cluster analysis.
The following video demonstrates \(k\)-means clustering in jamovi and will be useful for the remainder of this question question.
💻 Following the steps shown in the above video, carry out a \(k\)-means cluster analysis in jamovi using \(k = 3\) clusters.
💻 Let’s discuss the results.
Cluster number option in the Save section of the cluster analysis you carried out in jamovi.)between_ss /total_ss value. (Note that between_ss is the “Between clusters” value and total_ss is the “Total” value, both from the Sum of squares Table in jamovi)To further understand the sum of squares table, note that:
between_SS denotes the sum of squares between clusters (i.e., how well the clusters are separated from each other),total_ss denotes the total variability in the data.between_ss /total_ss tells us how much of the variability in the data is accounted for by the clusters found by the algorithm. This value can range from 0% to 100%, with larger values indicating a better result. Here, 72.1% is decent, although we might have expected a slightly higher result.💻 Using jamovi, create the following plots:
species separated by clusterindex on the \(x\)-axis, species_numeric on the \(y\)-axis, and grouped by ClusteringBased on these plot, and the plot produced in 2.3, do you think the \(k\)-means clustering has performed well? 💬
💻 At this point, it might seem that our job is done. We have results for \(k=3\), and there are three species of penguins.
Remember though, normally we would not actually know what the number of clusters should be. Therefore, we would usually try a range of different \(k\) values.
Conduct new \(k\)-means cluster analyses on our data, for \(k\) values of 2 and 4. (Remember to click on the snowCluster module and again choose K-means clustering method to begin a new \(k\)-means cluster analysis.)
Note down the between_ss /total_ss results for each of the \(k\) values of 2, 3 and 4. What do you observe? 💬
🏡 You might have noticed that the between_ss /total_ss value increases as we increase the number of clusters. This can happen even if adding another cluster isn’t actually that helpful. Therefore, we should also consider some other assessment methods.
If we did not know the number of clusters, we could use several methods to determine the appropriate value of \(k\). For the purposes of this lab, we will focus on just the gap statistic method.
Using the Gap statistic plot, determine the optimal number of clusters for our data. 💬
🏡 We can also visualise the clusters using the Cluster plot produced in jamovi.
Compare the cluster plots produced for each of the \(k = 2\), \(k = 3\) and \(k = 4\) analyses. Which one do you prefer, and why? 💬
🏡 In conclusion, based on all your \(k\)-means clustering analyses, which value of \(k\) would you recommend using, and why? 💬
Unlike the previous clustering techniques discussed, hierarchical clustering begins by assigning each point to its own cluster.
This means that, for our penguins data set, we would start with 333 clusters - 1 for each penguin. Then, the two most similar clusters are merged (continuing our example, this would result in us now having 332 clusters). We repeat this process, until we have just one large cluster (which contains all the points).
As a result, we now have a hierarchy of clusters, which looks a bit like an upside-down tree (with observations from the same cluster on the same ‘branch’).
By clicking on the snowCluster module and then choosing Hierarchical clustering mendrogram, carry out hierarchical clustering on the penguins data set.
Standardize databy height (leave default as 15)We can visualise the results of the hierarchical clustering using a dendrogram and a pairs plot.
In the Plots section, select the Plot dendrogram and Plot pairs of varibles options.
Do you understand what is shown in both plots, and are you able to identify how many clusters have been decided on by looking at the dendrogram? Check with your computer lab demonstrator if you are not sure. Note that it is also possible to select the number of clusters directly in the Decide on clusters section.
There are several linkage method options we can choose from when conducting hierarchical clustering. These relate to the way in which distance is measured between two points, to determine their level of similarity.
The default hierarchical clustering method in jamovi is ward.D2. Try re-running your hierarchical clustering analysis using some other options, by instead selecting "average" and then "single".
When you visualise the results, do the dendrograms look very different?
These notes have been prepared by Amanda Shaker and Rupert Kuveke. Please note that some of the content in these notes has been developed from content in Thulin (2021). The copyright for the material in these notes resides with the authors named above, with the Department of Mathematical and Physical Sciences and with La Trobe University. Copyright in this work is vested in La Trobe University including all La Trobe University branding and naming. Unless otherwise stated, material within this work is licensed under a Creative Commons Attribution-Non Commercial-Non Derivatives License BY-NC-ND.