Debian Codegraph @

R/cor_cophenetic.R

raw
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 #' @title Sorts a distance matrix by rows and columns names
22 #' @description Sorts a distance matrix by the names of the rows and columns.
23 #' @export
24 #' @param dist_mat a distance matrix.
25 #' @param by_rows logical (TRUE). Sort the distance matrix by rows?
26 #' @param by_cols logical (TRUE). Sort the distance matrix by columns?
27 #' @param ... Ignored.
28 #' @seealso
29 #' \link{dist}, \link{cor_cophenetic}
30 #' @return
31 #' A distance matrix (after sorting)
32 #'
33 sort_dist_mat <- function(dist_mat, by_rows = TRUE, by_cols = TRUE, ...) {
34 dist1 <- as.matrix(dist_mat)
35 if (by_rows) dist1 <- dist1[order(rownames(dist1)), ]
36 if (by_cols) dist1 <- dist1[, order(colnames(dist1))]
37 dist1 <- as.dist(dist1)
38 attributes(dist1)[c("Diag", "Upper")] <- attributes(dist_mat)[c("Diag", "Upper")]
39 dist1
40 }
41
42
43
44
45
46
47 #' @title Cophenetic correlation between two trees
48 #' @name cor_cophenetic
49 #' @export
50 #' @description
51 #' Cophenetic correlation coefficient for two trees.
52 #'
53 #' Assumes the labels in the two trees fully match. If they do not
54 #' please first use \link{intersect_trees} to have them matched.
55 #'
56 #'
57 #' @param dend1 a tree (dendrogram/hclust/phylo, or dendlist)
58 #' @param dend2 Either a tree (dendrogram/hclust/phylo), or a \link{dist} object (for example, from the original data matrix).
59 #' @param which an integer vector of length 2, indicating
60 #' which of the trees in a dendlist object should have
61 #' their cor_cophenetic calculated.
62 #' @param method_coef a character string indicating which correlation coefficient
63 #' is to be computed. One of "pearson" (default), "kendall", or "spearman",
64 #' can be abbreviated. Passed to \link{cor}.
65 #' @param ... Ignored.
66 #'
67 #' @details
68 #'
69 #' From \link{cophenetic}:
70 #' The cophenetic distance between two observations that have been clustered
71 #' is defined to be the intergroup dissimilarity at which the two observations
72 #' are first combined into a single cluster. Note that this distance has many
73 #' ties and restrictions.
74 #'
75 #' cor_cophenetic calculates the correlation between two cophenetic distance
76 #' matrices of the two trees.
77 #'
78 #' The value can range between -1 to 1. With near 0 values meaning that
79 #' the two trees are not statistically similar.
80 #' For exact p-value one should result to a permutation test. One such option
81 #' will be to permute over the labels of one tree many times, and calculating
82 #' the distriubtion under the null hypothesis (keeping the trees topologies
83 #' constant).
84 #'
85 #' Notice that this measure IS affected by the height of a branch.
86 #'
87 #' @seealso
88 #' \link{cophenetic}, \link{cor_bakers_gamma}
89 #' @return
90 #' The correlation between cophenetic
91 #'
92 #' @references
93 #'
94 #' Sokal, R. R. and F. J. Rohlf. 1962. The comparison of dendrograms by
95 #' objective methods. Taxon, 11:33-40
96 #'
97 #' Sneath, P.H.A. and Sokal, R.R. (1973) Numerical Taxonomy: The Principles
98 #' and Practice of Numerical Classification, p. 278 ff; Freeman, San Francisco.
99 #'
100 #' \url{https://en.wikipedia.org/wiki/Cophenetic_correlation}
101 #'
102 #' @examples
103 #'
104 #' \dontrun{
105 #'
106 #' set.seed(23235)
107 #' ss <- sample(1:150, 10)
108 #' hc1 <- iris[ss, -5] %>%
109 #' dist() %>%
110 #' hclust("com")
111 #' hc2 <- iris[ss, -5] %>%
112 #' dist() %>%
113 #' hclust("single")
114 #' dend1 <- as.dendrogram(hc1)
115 #' dend2 <- as.dendrogram(hc2)
116 #' # cutree(dend1)
117 #'
118 #' cophenetic(hc1)
119 #' cophenetic(hc2)
120 #' # notice how the dist matrix for the dendrograms have different orders:
121 #' cophenetic(dend1)
122 #' cophenetic(dend2)
123 #'
124 #' cor(cophenetic(hc1), cophenetic(hc2)) # 0.874
125 #' cor(cophenetic(dend1), cophenetic(dend2)) # 0.16
126 #' # the difference is becasue the order of the distance table in the case of
127 #' # stats:::cophenetic.dendrogram will change between dendrograms!
128 #'
129 #' # however, this is consistant (since I force-sort the rows/columns):
130 #' cor_cophenetic(hc1, hc2)
131 #' cor_cophenetic(dend1, dend2)
132 #'
133 #' cor_cophenetic(dendlist(dend1, dend2))
134 #'
135 #' # we can also use different cor methods (almost the same result though):
136 #' cor_cophenetic(hc1, hc2, method = "spearman") # 0.8456014
137 #' cor_cophenetic(dend1, dend2, method = "spearman") #
138 #'
139 #'
140 #' # cophenetic correlation is about 10 times (!) faster than bakers_gamma cor:
141 #' library(microbenchmark)
142 #' microbenchmark(
143 #' cor_bakers_gamma = cor_bakers_gamma(dend1, dend2, try_cutree_hclust = FALSE),
144 #' cor_cophenetic = cor_cophenetic(dend1, dend2),
145 #' times = 10
146 #' )
147 #'
148 #' # but only because of the cutree for dendrogram. When allowing hclust cutree
149 #' # it is only about twice as fast:
150 #' microbenchmark(
151 #' cor_bakers_gamma = cor_bakers_gamma(dend1, dend2, try_cutree_hclust = TRUE),
152 #' cor_cophenetic = cor_cophenetic(dend1, dend2),
153 #' times = 10
154 #' )
155 #' }
156 #'
157 cor_cophenetic <- function(dend1, ...) {
158 UseMethod("cor_cophenetic")
159 }
160
161
162
163 #' @export
164 #' @rdname cor_cophenetic
165 cor_cophenetic.default <- function(dend1, dend2, method_coef = c("pearson", "kendall", "spearman"), ...) {
166 dist_dend1 <- cophenetic(dend1)
167
168 # if(!is.dist(dend2)) stop("dend2 must be either a dendrogram or a dist object")
169 if (is.dist(dend2)) {
170 dist_dend2 <- dend2
171 } else { # either dendrogram or hclust or other
172 dist_dend2 <- cophenetic(dend2)
173 }
174 # else - dend2 must be a distance matrix
175
176 # hclust objects actually don't need the sorting...
177 if (!is.hclust(dend1)) {
178 dist_dend1 <- sort_dist_mat(dist_dend1)
179 dist_dend2 <- sort_dist_mat(dist_dend2)
180 }
181
182 method_coef <- match.arg(method_coef)
183 cor(dist_dend1, dist_dend2, method = method_coef)
184 }
185
186
187 #' @export
188 #' @rdname cor_cophenetic
189 cor_cophenetic.dendlist <- function(dend1, which = c(1L, 2L), method_coef = c("pearson", "kendall", "spearman"), ...) {
190 method_coef <- match.arg(method_coef)
191 cor_cophenetic(dend1[[which[1]]], dend1[[which[2]]], method = method_coef, ...)
192 }
193
194
195 #
196 #
197 # cor_cophenetic.hclust <- function(dend1, dend2, method = c("pearson", "kendall", "spearman"), ...) {
198 # # hclust keeps things consistant
199 # dist_dend1 <- cophenetic(dend1)
200 # dist_dend2 <- cophenetic(dend2)
201 #
202 # cor(dist_dend1, dist_dend2 , method=method)
203 # }
204 #
205 #
206 # # ' @S3method cor_bakers_gamma dendrogram
207 # cor_cophenetic.dendrogram <- function(dend1, dend2, use_labels_not_values = TRUE, to_plot = FALSE, warn = dendextend_options("warn"), ...)
208 # {
209 # return(bakers_gamma)
210 # }
211 #