Week 4 practice

Clusters and similarity

In Week 4 Workshop we are focusing on clustering and dimension reduction.

k-means clustering

The code sets a seed for reproducibility as we are randomly sampling from the runif (random uniform) and rnorm (random normal) distributions to generate a dataset of k clusters.

We define parameters for number of clusters k and number of points per cluster N. We randomly pick cluster centres xs and xy. For each centre, we generate normally distributed data points around those centres, with a standard deviation of 0.5.

Try changing k with the same seed. Try changing the seed. What happens?

See if the Within Cluster Sum of Squares (WSS) recommends the same number of clusters as those we have randomly generated.

Try changing the method to ‘silhouette’. The Silhouette method shows how close each point in a cluster is to points in the neighboring cluster.

Are they the same or different? Try it with different k and different seeds.

Let’s use the dataset we created d and run a kmeans algorithm. What happens? Try it with different k and with different seeds.

We did not set a seed when running the algorithm. If we did, our results would be identical each time we run it (try it, eg set.seed(123) before running kmeans so the initial cluster centres are chosen reproducibly.

kmeans starts by randomly choosing initial cluster centres unless you have explictly specified an intialisation method. Because of this random intialisation, each run can converge to slightly different final centres and varying cluster assignments.

A better way of dealing with this is to use an nstart parameter to tell kmeans how many different random sets of intial centroids to try. It then picks the best solution, i.e. the one with the lowest total within-cluster variation. Try adding nstart = 25 to the kmeans function.

Here are a couple of blank code chunks. You could, for example: * Inspect d (e.g. head(d))
* Look at the cluster assignments (kmeans_result$cluster)
* Look athe the resulting cluster centres (kmeans_result$centers)