I have written these functions for clustering sequence-based data:
library(TraMineR)
library(cluster)
clustering <- function(data){
data <- seqdef(data, left = "DEL", gaps = "DEL", right = "DEL")
couts <- seqsubm(data, method = "CONSTANT")
data.om <- seqdist(data, method = "OM", indel = 3, sm = couts)
clusterward <- agnes(data.om, diss = TRUE, method = "ward")
(clusterward)
}
rc <- clustering(rubinius_sequences)
cluster_cut <- function(data, clusterward, n_clusters, name_clusters){
data <- seqdef(data, left = "DEL", gaps = "DEL", right = "DEL")
cluster4 <- cutree(clusterward, k = n_clusters)
cluster4 <- factor(cluster4, labels = c("Type 1", "Type 2", "Type 3", "Type 4"))
(data[cluster4==name_clusters,])
}
rc1 <- cluster_cut(project_sequences, rc, 4, "Type 1")
However, here the number of clusters is arbitrarily assigned. Is there some way I can show that the amount of variance (or some similar measure) captured by a certain number of clusters starts to reach a point of diminishing returns at a certain number of clusters? I'm imagining something similar to a scree plot in factor analysis.
library(WeightedCluster)
(agnesRange <- wcKMedRange(rubinius.dist, 2:10))
plot(agnesRange, stat = c("ASW", "HG", "PBC"), lwd = 5)
This will give multiple indices for finding the ideal number of clusters, as well as as graph. More information about the indices can be found here (under cluster quality): http://mephisto.unige.ch/weightedcluster/