[d79ff0]: / R / getSilhouette.R

Download this file

118 lines (102 with data), 3.4 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
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#' Get Silhouette Coefficient from (s)PCA, (s)PLS or block.(s)PLS clusters
#'
#' \code{getSilhouette} is a generic function that compute silhouette coefficient
#' for an object of the type \code{pca}, \code{spca}, \code{pls}, \code{spls},
#' \code{block.pls}, \code{block.spls}.
#'
#' @param object a mixOmics object of the class \code{pca}, \code{spca}, \code{pls}, \code{spls}, \code{block.pls}, \code{block.spls}
#'
#' @details
#' This method extract the componant contribution depending on the object,
#' perform the clustering step, and compute the silhouette coefficient.
#'
#' @return
#' silhouette coefficient
#'
#' @examples
#' demo <- suppressWarnings(get_demo_cluster())
#' getSilhouette(object = demo$pca)
#' getSilhouette(object = demo$spca)
#' getSilhouette(object = demo$pls)
#' getSilhouette(object = demo$spls)
#' getSilhouette(object = demo$block.pls)
#' getSilhouette(object = demo$block.spls)
#'
#'
#' @export
getSilhouette <- function(object){
UseMethod("getSilhouette")
}
#' @importFrom magrittr %>%
#' @importFrom dplyr select
#' @export
getSilhouette.pca <- function(object){
cluster <- getCluster(object) %>%
dplyr::select("molecule", "cluster")
silhouette.res <- wrapper.silhouette(X = as.data.frame(object$X), cluster = cluster$cluster)
return(silhouette.res$average)
}
#' @importFrom magrittr %>%
#' @importFrom dplyr select
#' @export
getSilhouette.spca <- function(object){
cluster <- getCluster(object) %>%
dplyr::select("molecule", "cluster")
X <- object$X %>%
as.data.frame() %>%
dplyr::select(cluster$molecule)
silhouette.res <- wrapper.silhouette(X = X, cluster = cluster$cluster)
return(silhouette.res$average)
}
#' @export
#' @importFrom dplyr select
#' @importFrom magrittr %>%
getSilhouette.mixo_pls <- function(object){
cluster <- getCluster(object) %>%
dplyr::select("molecule", "cluster")
X <- object$X %>%
as.data.frame()
Y <- object$Y %>%
as.data.frame()
data <- cbind(X,Y)
silhouette.res <- wrapper.silhouette(X = data, cluster = cluster$cluster)
return(silhouette.res$average)
}
#' @importFrom magrittr %>%
#' @importFrom dplyr select
#' @export
getSilhouette.mixo_spls <- function(object){
cluster <- getCluster(object) %>%
dplyr::select("molecule", "cluster")
X <- object$X %>%
as.data.frame()
Y <- object$Y %>%
as.data.frame()
data <- cbind(X,Y) %>%
dplyr::select(cluster$molecule)
silhouette.res <- wrapper.silhouette(X = data, cluster = cluster$cluster)
return(silhouette.res$average)
}
#' @importFrom magrittr %>%
#' @importFrom dplyr select
#' @export
getSilhouette.block.pls <- function(object){
cluster <- getCluster(object) %>%
dplyr::select("molecule", "cluster")
X <- do.call("cbind", object$X) %>%
as.data.frame
silhouette.res <- wrapper.silhouette(X = X, cluster = cluster$cluster)
return(silhouette.res$average)
}
#' @importFrom magrittr %>%
#' @importFrom dplyr select
#' @export
getSilhouette.block.spls <- function(object){
cluster <- getCluster(object) %>%
dplyr::select("molecule", "cluster")
X <- do.call("cbind", object$X) %>%
as.data.frame %>%
dplyr::select(cluster$molecule)
silhouette.res <- wrapper.silhouette(X = X, cluster = cluster$cluster)
return(silhouette.res$average)
}