blob: 78ed9d7d6af5a385264a276ed0101c15a997ba66 (
plain)
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
|
#' Identify Order
#'
#' Perform clustering to identify the most useful ordering
#' to analyze a heatmap or networkplot.
#' @param R A square numeric correlation matrix
#' @param method Hierarchical method to perform (default hclust)
#'
#' @return A character vector
#'
#' @examples
#' X <- matrix(data = rnorm(100), nrow = 10)
#' R <- cor(X)
#' get_order(R)
#'
#' @importFrom stats as.dist hclust
#'
#' @export
get_order <- function(R, method = 'hclust'){
if(is.matrix(R)){
if(nrow(R) != ncol(R)){
stop('Input must be a square matrix')
}
}else{
stop('Input must be a square matrix')
}
if(is.null(colnames(R))){
colnames(R) <- as.character(seq_len(ncol(R)))
}
if(is.null(rownames(R))){
rownames(R) <- as.character(seq_len(nrow(R)))
}
if(method == 'hclust'){
# convert R into a distance object
D <- as.dist(1 - R)
hc <- hclust(D, method = 'complete')
cluster_order <- hc$order
}else if(method == 'laplacian'){
stop('Method not implemented yet')
}else{
stop('Invalid method selected (must be one of: hclust, laplacian)')
}
return(colnames(R)[cluster_order])
}
|