|
a |
|
b/R/getSilhouette.R |
|
|
1 |
#' Get Silhouette Coefficient from (s)PCA, (s)PLS or block.(s)PLS clusters |
|
|
2 |
#' |
|
|
3 |
#' \code{getSilhouette} is a generic function that compute silhouette coefficient |
|
|
4 |
#' for an object of the type \code{pca}, \code{spca}, \code{pls}, \code{spls}, |
|
|
5 |
#' \code{block.pls}, \code{block.spls}. |
|
|
6 |
#' |
|
|
7 |
#' @param object a mixOmics object of the class \code{pca}, \code{spca}, \code{pls}, \code{spls}, \code{block.pls}, \code{block.spls} |
|
|
8 |
#' |
|
|
9 |
#' @details |
|
|
10 |
#' This method extract the componant contribution depending on the object, |
|
|
11 |
#' perform the clustering step, and compute the silhouette coefficient. |
|
|
12 |
#' |
|
|
13 |
#' @return |
|
|
14 |
#' silhouette coefficient |
|
|
15 |
#' |
|
|
16 |
#' @examples |
|
|
17 |
#' demo <- suppressWarnings(get_demo_cluster()) |
|
|
18 |
#' getSilhouette(object = demo$pca) |
|
|
19 |
#' getSilhouette(object = demo$spca) |
|
|
20 |
#' getSilhouette(object = demo$pls) |
|
|
21 |
#' getSilhouette(object = demo$spls) |
|
|
22 |
#' getSilhouette(object = demo$block.pls) |
|
|
23 |
#' getSilhouette(object = demo$block.spls) |
|
|
24 |
#' |
|
|
25 |
#' |
|
|
26 |
#' @export |
|
|
27 |
getSilhouette <- function(object){ |
|
|
28 |
UseMethod("getSilhouette") |
|
|
29 |
} |
|
|
30 |
|
|
|
31 |
#' @importFrom magrittr %>% |
|
|
32 |
#' @importFrom dplyr select |
|
|
33 |
#' @export |
|
|
34 |
getSilhouette.pca <- function(object){ |
|
|
35 |
cluster <- getCluster(object) %>% |
|
|
36 |
dplyr::select("molecule", "cluster") |
|
|
37 |
|
|
|
38 |
silhouette.res <- wrapper.silhouette(X = as.data.frame(object$X), cluster = cluster$cluster) |
|
|
39 |
return(silhouette.res$average) |
|
|
40 |
} |
|
|
41 |
|
|
|
42 |
#' @importFrom magrittr %>% |
|
|
43 |
#' @importFrom dplyr select |
|
|
44 |
#' @export |
|
|
45 |
getSilhouette.spca <- function(object){ |
|
|
46 |
cluster <- getCluster(object) %>% |
|
|
47 |
dplyr::select("molecule", "cluster") |
|
|
48 |
|
|
|
49 |
X <- object$X %>% |
|
|
50 |
as.data.frame() %>% |
|
|
51 |
dplyr::select(cluster$molecule) |
|
|
52 |
silhouette.res <- wrapper.silhouette(X = X, cluster = cluster$cluster) |
|
|
53 |
return(silhouette.res$average) |
|
|
54 |
} |
|
|
55 |
|
|
|
56 |
#' @export |
|
|
57 |
#' @importFrom dplyr select |
|
|
58 |
#' @importFrom magrittr %>% |
|
|
59 |
getSilhouette.mixo_pls <- function(object){ |
|
|
60 |
cluster <- getCluster(object) %>% |
|
|
61 |
dplyr::select("molecule", "cluster") |
|
|
62 |
|
|
|
63 |
X <- object$X %>% |
|
|
64 |
as.data.frame() |
|
|
65 |
Y <- object$Y %>% |
|
|
66 |
as.data.frame() |
|
|
67 |
data <- cbind(X,Y) |
|
|
68 |
silhouette.res <- wrapper.silhouette(X = data, cluster = cluster$cluster) |
|
|
69 |
return(silhouette.res$average) |
|
|
70 |
} |
|
|
71 |
|
|
|
72 |
#' @importFrom magrittr %>% |
|
|
73 |
#' @importFrom dplyr select |
|
|
74 |
#' @export |
|
|
75 |
getSilhouette.mixo_spls <- function(object){ |
|
|
76 |
cluster <- getCluster(object) %>% |
|
|
77 |
dplyr::select("molecule", "cluster") |
|
|
78 |
|
|
|
79 |
X <- object$X %>% |
|
|
80 |
as.data.frame() |
|
|
81 |
Y <- object$Y %>% |
|
|
82 |
as.data.frame() |
|
|
83 |
data <- cbind(X,Y) %>% |
|
|
84 |
dplyr::select(cluster$molecule) |
|
|
85 |
|
|
|
86 |
silhouette.res <- wrapper.silhouette(X = data, cluster = cluster$cluster) |
|
|
87 |
return(silhouette.res$average) |
|
|
88 |
} |
|
|
89 |
|
|
|
90 |
#' @importFrom magrittr %>% |
|
|
91 |
#' @importFrom dplyr select |
|
|
92 |
#' @export |
|
|
93 |
getSilhouette.block.pls <- function(object){ |
|
|
94 |
cluster <- getCluster(object) %>% |
|
|
95 |
dplyr::select("molecule", "cluster") |
|
|
96 |
|
|
|
97 |
X <- do.call("cbind", object$X) %>% |
|
|
98 |
as.data.frame |
|
|
99 |
|
|
|
100 |
silhouette.res <- wrapper.silhouette(X = X, cluster = cluster$cluster) |
|
|
101 |
return(silhouette.res$average) |
|
|
102 |
} |
|
|
103 |
|
|
|
104 |
|
|
|
105 |
#' @importFrom magrittr %>% |
|
|
106 |
#' @importFrom dplyr select |
|
|
107 |
#' @export |
|
|
108 |
getSilhouette.block.spls <- function(object){ |
|
|
109 |
cluster <- getCluster(object) %>% |
|
|
110 |
dplyr::select("molecule", "cluster") |
|
|
111 |
|
|
|
112 |
X <- do.call("cbind", object$X) %>% |
|
|
113 |
as.data.frame %>% |
|
|
114 |
dplyr::select(cluster$molecule) |
|
|
115 |
|
|
|
116 |
silhouette.res <- wrapper.silhouette(X = X, cluster = cluster$cluster) |
|
|
117 |
return(silhouette.res$average) |
|
|
118 |
} |