Debian Codegraph @

man/cor_cophenetic.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/cor_cophenetic.R
3 \name{cor_cophenetic}
4 \alias{cor_cophenetic}
5 \alias{cor_cophenetic.default}
6 \alias{cor_cophenetic.dendlist}
7 \title{Cophenetic correlation between two trees}
8 \usage{
9 cor_cophenetic(dend1, ...)
10
11 \method{cor_cophenetic}{default}(
12 dend1,
13 dend2,
14 method_coef = c("pearson", "kendall", "spearman"),
15 ...
16 )
17
18 \method{cor_cophenetic}{dendlist}(
19 dend1,
20 which = c(1L, 2L),
21 method_coef = c("pearson", "kendall", "spearman"),
22 ...
23 )
24 }
25 \arguments{
26 \item{dend1}{a tree (dendrogram/hclust/phylo, or dendlist)}
27
28 \item{...}{Ignored.}
29
30 \item{dend2}{Either a tree (dendrogram/hclust/phylo), or a \link{dist} object (for example, from the original data matrix).}
31
32 \item{method_coef}{a character string indicating which correlation coefficient
33 is to be computed. One of "pearson" (default), "kendall", or "spearman",
34 can be abbreviated. Passed to \link{cor}.}
35
36 \item{which}{an integer vector of length 2, indicating
37 which of the trees in a dendlist object should have
38 their cor_cophenetic calculated.}
39 }
40 \value{
41 The correlation between cophenetic
42 }
43 \description{
44 Cophenetic correlation coefficient for two trees.
45
46 Assumes the labels in the two trees fully match. If they do not
47 please first use \link{intersect_trees} to have them matched.
48 }
49 \details{
50 From \link{cophenetic}:
51 The cophenetic distance between two observations that have been clustered
52 is defined to be the intergroup dissimilarity at which the two observations
53 are first combined into a single cluster. Note that this distance has many
54 ties and restrictions.
55
56 cor_cophenetic calculates the correlation between two cophenetic distance
57 matrices of the two trees.
58
59 The value can range between -1 to 1. With near 0 values meaning that
60 the two trees are not statistically similar.
61 For exact p-value one should result to a permutation test. One such option
62 will be to permute over the labels of one tree many times, and calculating
63 the distriubtion under the null hypothesis (keeping the trees topologies
64 constant).
65
66 Notice that this measure IS affected by the height of a branch.
67 }
68 \examples{
69
70 \dontrun{
71
72 set.seed(23235)
73 ss <- sample(1:150, 10)
74 hc1 <- iris[ss, -5] \%>\%
75 dist() \%>\%
76 hclust("com")
77 hc2 <- iris[ss, -5] \%>\%
78 dist() \%>\%
79 hclust("single")
80 dend1 <- as.dendrogram(hc1)
81 dend2 <- as.dendrogram(hc2)
82 # cutree(dend1)
83
84 cophenetic(hc1)
85 cophenetic(hc2)
86 # notice how the dist matrix for the dendrograms have different orders:
87 cophenetic(dend1)
88 cophenetic(dend2)
89
90 cor(cophenetic(hc1), cophenetic(hc2)) # 0.874
91 cor(cophenetic(dend1), cophenetic(dend2)) # 0.16
92 # the difference is becasue the order of the distance table in the case of
93 # stats:::cophenetic.dendrogram will change between dendrograms!
94
95 # however, this is consistant (since I force-sort the rows/columns):
96 cor_cophenetic(hc1, hc2)
97 cor_cophenetic(dend1, dend2)
98
99 cor_cophenetic(dendlist(dend1, dend2))
100
101 # we can also use different cor methods (almost the same result though):
102 cor_cophenetic(hc1, hc2, method = "spearman") # 0.8456014
103 cor_cophenetic(dend1, dend2, method = "spearman") #
104
105
106 # cophenetic correlation is about 10 times (!) faster than bakers_gamma cor:
107 library(microbenchmark)
108 microbenchmark(
109 cor_bakers_gamma = cor_bakers_gamma(dend1, dend2, try_cutree_hclust = FALSE),
110 cor_cophenetic = cor_cophenetic(dend1, dend2),
111 times = 10
112 )
113
114 # but only because of the cutree for dendrogram. When allowing hclust cutree
115 # it is only about twice as fast:
116 microbenchmark(
117 cor_bakers_gamma = cor_bakers_gamma(dend1, dend2, try_cutree_hclust = TRUE),
118 cor_cophenetic = cor_cophenetic(dend1, dend2),
119 times = 10
120 )
121 }
122
123 }
124 \references{
125 Sokal, R. R. and F. J. Rohlf. 1962. The comparison of dendrograms by
126 objective methods. Taxon, 11:33-40
127
128 Sneath, P.H.A. and Sokal, R.R. (1973) Numerical Taxonomy: The Principles
129 and Practice of Numerical Classification, p. 278 ff; Freeman, San Francisco.
130
131 \url{https://en.wikipedia.org/wiki/Cophenetic_correlation}
132 }
133 \seealso{
134 \link{cophenetic}, \link{cor_bakers_gamma}
135 }