| 1 |
# Copyright (C) Tal Galili |
| 2 |
# |
| 3 |
# This file is part of dendextend. |
| 4 |
# |
| 5 |
# dendextend is free software: you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 2 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# dendextend is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# A copy of the GNU General Public License is available at |
| 16 |
# http://www.r-project.org/Licenses/ |
| 17 |
# |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#' @title Correlation matrix between a list of trees. |
| 24 |
#' @export |
| 25 |
#' @description |
| 26 |
#' A correlation matrix between a list of trees. |
| 27 |
#' |
| 28 |
#' Assumes the labels in the two trees fully match. If they do not |
| 29 |
#' please first use \link{intersect_trees} to have them matched. |
| 30 |
#' |
| 31 |
#' @param dend a \link{dendlist} of trees |
| 32 |
#' @param method a character string indicating which correlation coefficient |
| 33 |
#' is to be computed. One of "cophenetic" (default), "baker", |
| 34 |
#' "common_nodes", or "FM_index". |
| 35 |
#' It can be abbreviated. |
| 36 |
#' @param ... passed to cor functions. |
| 37 |
#' |
| 38 |
#' @seealso |
| 39 |
#' \link{cophenetic}, \link{cor_cophenetic}, \link{cor_bakers_gamma}, |
| 40 |
#' \link{cor_common_nodes}, \link{cor_FM_index} |
| 41 |
#' @return |
| 42 |
#' A correlation matrix between the different trees |
| 43 |
#' |
| 44 |
#' @examples |
| 45 |
#' |
| 46 |
#' \dontrun{ |
| 47 |
#' |
| 48 |
#' set.seed(23235) |
| 49 |
#' ss <- sample(1:150, 10) |
| 50 |
#' dend1 <- iris[ss, -5] %>% |
| 51 |
#' dist() %>% |
| 52 |
#' hclust("com") %>% |
| 53 |
#' as.dendrogram() |
| 54 |
#' dend2 <- iris[ss, -5] %>% |
| 55 |
#' dist() %>% |
| 56 |
#' hclust("single") %>% |
| 57 |
#' as.dendrogram() |
| 58 |
#' dend3 <- iris[ss, -5] %>% |
| 59 |
#' dist() %>% |
| 60 |
#' hclust("ave") %>% |
| 61 |
#' as.dendrogram() |
| 62 |
#' dend4 <- iris[ss, -5] %>% |
| 63 |
#' dist() %>% |
| 64 |
#' hclust("centroid") %>% |
| 65 |
#' as.dendrogram() |
| 66 |
#' # cutree(dend1) |
| 67 |
#' cors <- cor.dendlist(dendlist(d1 = dend1, d2 = dend2, d3 = dend3, d4 = dend4)) |
| 68 |
#' |
| 69 |
#' cors |
| 70 |
#' |
| 71 |
#' # a nice plot for them: |
| 72 |
#' library(corrplot) |
| 73 |
#' corrplot(cor.dendlist(dend1234), "pie", "lower") |
| 74 |
#' } |
| 75 |
cor.dendlist <- function(dend, method = c("cophenetic", "baker", "common_nodes", "FM_index"), ...) { |
| 76 |
if (!is.dendlist(dend)) stop("dend needs to be a dendlist object") |
| 77 |
method <- match.arg(method) |
| 78 |
|
| 79 |
n_list <- length(dend) |
| 80 |
the_cor <- matrix(1, n_list, n_list) |
| 81 |
pairwise_combn <- combn(n_list, 2) |
| 82 |
|
| 83 |
for (i in 1:ncol(pairwise_combn)) { |
| 84 |
l1 <- pairwise_combn[1, i] |
| 85 |
l2 <- pairwise_combn[2, i] |
| 86 |
the_cor[l1, l2] <- the_cor[l2, l1] <- |
| 87 |
switch(method, |
| 88 |
cophenetic = cor_cophenetic(dend[[l1]], dend[[l2]], ...), |
| 89 |
baker = cor_bakers_gamma(dend[[l1]], dend[[l2]], ...), |
| 90 |
common_nodes = cor_common_nodes(dend[[l1]], dend[[l2]], ...), |
| 91 |
FM_index = cor_FM_index(dend[[l1]], dend[[l2]], ...) |
| 92 |
) |
| 93 |
} |
| 94 |
|
| 95 |
rownames(the_cor) <- colnames(the_cor) <- names(dend) |
| 96 |
|
| 97 |
the_cor |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
# edgeset_dist |
| 104 |
|
| 105 |
|
| 106 |
#' Proportion of commong nodes between two trees |
| 107 |
#' @export |
| 108 |
#' @description |
| 109 |
#' Calculates the number of nodes, in each tree, that are common (i.e.: that have the same exact list of labels). |
| 110 |
#' The correlation is between 0 (actually, 2*(nnodes-1)/(2*nnodes), for two trees with |
| 111 |
#' the same list of labels - since the top node will always be identical for them). |
| 112 |
#' Where 1 means that every node in the one tree, has a node in the other tree with the exact |
| 113 |
#' same list of labels. |
| 114 |
#' Notice this measure is non-parameteric (it ignores the heights and relative position of the nodes). |
| 115 |
#' |
| 116 |
#' @param dend1 a dendrogram. |
| 117 |
#' @param dend2 a dendrogram. |
| 118 |
#' @param ... not used. |
| 119 |
#' |
| 120 |
#' @return |
| 121 |
#' A correlation value between 0 to 1 (almost identical trees) |
| 122 |
#' @seealso \link{distinct_edges}, \link{cor.dendlist} |
| 123 |
#' |
| 124 |
#' @examples |
| 125 |
#' |
| 126 |
#' set.seed(23235) |
| 127 |
#' ss <- sample(1:150, 10) |
| 128 |
#' hc1 <- iris[ss, -5] %>% |
| 129 |
#' dist() %>% |
| 130 |
#' hclust("com") |
| 131 |
#' hc2 <- iris[ss, -5] %>% |
| 132 |
#' dist() %>% |
| 133 |
#' hclust("single") |
| 134 |
#' dend1 <- as.dendrogram(hc1) |
| 135 |
#' dend2 <- as.dendrogram(hc2) |
| 136 |
#' |
| 137 |
#' cor_cophenetic(dend1, dend2) |
| 138 |
#' cor_common_nodes(dend1, dend2) |
| 139 |
#' tanglegram(dend1, dend2) |
| 140 |
#' # we can see we have only two nodes which are different... |
| 141 |
cor_common_nodes <- function(dend1, dend2, ...) { |
| 142 |
# dendextend:::edgeset_dist |
| 143 |
n_diff_nodes <- edgeset_dist(dend1, dend2) |
| 144 |
nnodes_trees <- nnodes(dend1) + nnodes(dend2) |
| 145 |
|
| 146 |
(nnodes_trees - n_diff_nodes) / nnodes_trees |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
#' Correlation of FM_index for some k |
| 156 |
#' @export |
| 157 |
#' @description |
| 158 |
#' Calculates the FM_index Correlation for some k. |
| 159 |
#' |
| 160 |
#' |
| 161 |
#' @param dend1 a dendrogram. |
| 162 |
#' @param dend2 a dendrogram. |
| 163 |
#' @param k an integer (number of clusters to cut the tree) |
| 164 |
#' @param ... not used. |
| 165 |
#' |
| 166 |
#' @return |
| 167 |
#' A correlation value between 0 to 1 (almost identical clusters for some k) |
| 168 |
#' @seealso \link{FM_index}, \link{cor.dendlist}, \link{Bk} |
| 169 |
#' |
| 170 |
#' @examples |
| 171 |
#' |
| 172 |
#' set.seed(23235) |
| 173 |
#' ss <- sample(1:150, 10) |
| 174 |
#' hc1 <- iris[ss, -5] %>% |
| 175 |
#' dist() %>% |
| 176 |
#' hclust("com") |
| 177 |
#' hc2 <- iris[ss, -5] %>% |
| 178 |
#' dist() %>% |
| 179 |
#' hclust("single") |
| 180 |
#' dend1 <- as.dendrogram(hc1) |
| 181 |
#' dend2 <- as.dendrogram(hc2) |
| 182 |
#' |
| 183 |
#' cor_FM_index(dend1, dend2, k = 2) |
| 184 |
#' cor_FM_index(dend1, dend2, k = 3) |
| 185 |
#' cor_FM_index(dend1, dend2, k = 4) |
| 186 |
cor_FM_index <- function(dend1, dend2, k, ...) { |
| 187 |
if (missing(k)) stop("You need to specifiy k.") |
| 188 |
# dendextend:::edgeset_dist |
| 189 |
clus1 <- cutree(dend1, k = k)[order.dendrogram(dend1)] |
| 190 |
clus2 <- cutree(dend2, k = k)[order.dendrogram(dend2)] |
| 191 |
|
| 192 |
if (all(clus1 == 0) | all(clus2 == 0)) { |
| 193 |
warning("Can't calculate k - returning NA") |
| 194 |
return(NA) |
| 195 |
} |
| 196 |
|
| 197 |
FM_index(clus1, clus2, assume_sorted_vectors = TRUE) |
| 198 |
} |