I have a time-series dataset and I am required to find similar clusters in the data.

Based on my current knowledge and the requirements of my application, I used SBD measure (shape based distance) to calculate the dissimilarity matrix for my dataset and applied hierarchical clustering on it (using tsclust). The R command used is:

hclust=tsclust(mydata,type="h", distance = "sbd")

I also used cvi for cluster validation (cvi(hclust)) and was able to get a value of 0.508 for Silhouette width (which I believe is good enough). The problem is that I don't know at which point to cut this cluster tree - for how many clusters (value of k) or at what height (value of h) to get the Silhouette width of 0.5?

Moreover, once I know this value of k or h, how do I find the centroids (time-series data) that represent these clusters?


The answer was pretty straight-forward - thanks to Alexis who suggested me to read Appendix-A of the documentation, and @Haroon who suggested writing an email to Alexis. Here are the answers to my questions:

1) How to identify the value of k or h for which the Silhouette value was 0.5?

I ran a loop for different number of clusters and cut the dendrogram for these many clusters. Then, I computed the silhouette value for each of the cluster sets and identified the value for k where the value was 0.5. The code for this is:


#running the loop now
for(size in numberofclusters){
plot(clustersizes,silSBDclust,xlab="Number of clusters",ylab="Average silhouette width")

2) How to identify cluster centroids? Once we have identified the number of clusters, we need to find the centroids for these clusters. Please note that the object returned by tsclust() is of type S4 (refer to the documentation of TSClusters-Class) and to access its formal elements, we need @ operator unlike the $ operator often used in R. When we know the value of k for our dendrogram, we can use this value in tsclust() again for better clustering.

View(hclust@centroids)   #gives you a list of centroids

Hope it helps someone!

  • $\begingroup$ I tried this but I get "Error in View : trying to get slot "centroids" from an object of a basic class ("list") with no slots" Any idea on this? $\endgroup$ – Thusitha Thilina Dayaratne Jun 3 at 8:05

Just as a suggestion, what is being done by BajajG can be done directly with compare_clusterings:

cfg <- compare_clusterings_configs(
  k = 2L:100L,
  controls = list(
    hierarchical = hierarchical_control(method = "average", symmetric = TRUE)
  distances = pdc_configs("d", sbd = list())

evaluator <- cvi_evaluators("Sil")
comparison <- compare_clusterings(mydata, "h", cfg, 
                                  score.clus = evaluator$score,
                                  pick.clus = evaluator$pick)

best <- repeat_clustering(mydata, comparison, comparison$pick$config_id)

Check also the results in comparison$results$hierarchical.

EDIT: and also set method = "all" in hierarchical_control if you want to test other linkage methods.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.