[381c22]: / R / DIscBIO-generic-plotSilhouette.R

Download this file

57 lines (55 with data), 2.2 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#' @title Silhouette Plot for K-means clustering
#' @description The silhouette provides a representation of how well each point
#' is represented by its cluster in comparison to the closest neighboring
#' cluster. It computes for each point the difference between the average
#' similarity to all points in the same cluster and to all points in the
#' closest neighboring cluster. This difference it normalize such that it can
#' take values between -1 and 1 with higher values reflecting better
#' representation of a point by its cluster.
#' @param object \code{DISCBIO} class object.
#' @param K A numeric value of the number of clusters
#' @importFrom stats as.dist cor
#' @importFrom cluster silhouette
#' @return A silhouette plot
setGeneric(
name = "plotSilhouette",
def = function(object, K) standardGeneric("plotSilhouette")
)
#' @export
#' @rdname plotSilhouette
setMethod(
f = "plotSilhouette",
signature = "DISCBIO",
definition = function(object, K) {
# ======================================================================
# Validation
# ======================================================================
ran_clustexp <- length(object@kmeans$kpart) > 0
ran_exprmclust <- length(object@MBclusters) > 0
if (ran_clustexp) {
kpart <- object@kmeans$kpart
DIS <- object@distances
} else if (ran_exprmclust) {
kpart <- object@MBclusters$clusterid
y <- clustfun(
object@fdata,
clustnr = 3, bootnr = 50,
metric = "pearson", do.gap = TRUE, SE.method = "Tibs2001SEmax",
SE.factor = .25, B.gap = 50, cln = 0, rseed = NULL, quiet = TRUE
)
DIS <- as.matrix(y$di)
} else {
stop("run clustexp or exprmclust before plotSilhouette")
}
if (length(unique(kpart)) < 2) {
stop("only a single cluster: no silhouette plot")
}
# ======================================================================
# Plotting
# ======================================================================
col <- c("black", "blue", "green", "red", "yellow", "gray")
distances <- DIS
si <- silhouette(kpart, distances)
plot(si, col = col[1:K])
}
)