Performs clustering using specified dissimilarity measures
and clustering methods. The rows of the data are first converted to strings
and compared using the dissimilarity measures available in the
stringdist package.
Arguments
- data
A
data.frame, amatrixin wide format, or atna_dataobject (in which case, the sequence data is extracted automatically).- k
An
integergiving the number of clusters.- dissimilarity
A
characterstring specifying the dissimilarity measure. The available options are:"osa","lv","dl","hamming","lcs","qgram","cosine","jaccard", and"jw". See stringdist::stringdist-metrics for more information on these measures.- method
A
characterstring specifying clustering method. The available methods are"pam","ward.D","ward.D2","complete","average","single","mcquitty","median", and"centroid". Seecluster::pam()andstats::hclust()for more information on these methods.- na_syms
A
charactervector of symbols or factor levels to convert to explicit missing values.- weighted
A
logicalvalue indicating whether the dissimilarity measure should be weighted (the default isFALSEfor no weighting). IfTRUE, earlier observations of the sequences receive a greater weight in the distance calculation with an exponential decay. Currently only supported for the Hamming distance.- lambda
A
numericvalue defining the strength of the decay whenweighted = TRUE. The default is1.0.- ...
Additional arguments passed to
stringdist::stringdist().
Value
A tna_clustering object which is a list containing:
data: The original data.k: The number of clusters.assignments: Anintegervector of cluster assignments, one per sequence (row ofdata).silhouette: The average silhouette width of the clustering.sizes: Anintegervector of cluster sizes.method: The clustering method used.distance: The dissimilarity matrix (adistobject).
The result can be passed to group_model() to build a TNA model for
each cluster.
References
Kaufman, L., & Rousseeuw, P. J. (1990). Finding Groups in Data: An Introduction to Cluster Analysis. Wiley. doi:10.1002/9780470316801
Rousseeuw, P. J. (1987). Silhouettes: A graphical aid to the interpretation and validation of cluster analysis. Journal of Computational and Applied Mathematics, 20, 53–65. doi:10.1016/0377-0427(87)90125-7
See also
Cluster-related functions
communities(),
group_model(),
mmm_stats(),
print.tna_clustering(),
rename_groups()
Examples
data <- data.frame(
T1 = c("A", "B", "A", "C", "A", "B"),
T2 = c("B", "A", "B", "A", "C", "A"),
T3 = c("C", "C", "A", "B", "B", "C")
)
# PAM clustering with the Hamming distance (the defaults)
result <- cluster_sequences(data, k = 2)
print(result)
#> Clustering method: pam
#> Number of clusters: 2
#> Silhouette score: 0.4345238
#> Cluster sizes:
#> 1 2
#> 3 3
# Hierarchical clustering with the optimal string alignment distance
result <- cluster_sequences(data, k = 2, dissimilarity = "osa",
method = "complete")
