Debian Codegraph @

R/DendSer.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
22
23
24
25 #' @title Tries to run DendSer on a dendrogram
26 #' @export
27 #' @description
28 #' Implements dendrogram seriation.
29 #' The function tries to turn the dend into hclust, on
30 #' which it runs \link[DendSer]{DendSer}.
31 #'
32 #' Also, if a distance matrix is missing, it will try
33 #' to use the \link{cophenetic} distance.
34 #' @param dend An object of class dendrogram
35 #' @param ser_weight Used by cost function to evaluate
36 #' ordering. For cost=costLS, this is a vector of
37 #' object weights. Otherwise is a dist or symmetric matrix.
38 #' passed to DendSer.
39 #' If it is missing, the cophenetic distance is used instead.
40 #' @param ... parameters passed to \link[DendSer]{DendSer}
41 #' @return Numeric vector giving an optimal dendrogram order
42 #' @seealso \code{\link[DendSer]{DendSer}}, \link{DendSer.dendrogram} ,
43 #' \link{untangle_DendSer}, \link{rotate_DendSer}
44 #' @examples
45 #' \dontrun{
46 #' library(DendSer) # already used from within the function
47 #' hc <- hclust(dist(USArrests[1:4, ]), "ave")
48 #' dend <- as.dendrogram(hc)
49 #' DendSer.dendrogram(dend)
50 #' }
51 DendSer.dendrogram <- function(dend, ser_weight, ...) {
52 h <- as.hclust(dend)
53 if (missing(ser_weight)) ser_weight <- cophenetic(dend)
54 # library(DendSer)
55 DendSer::DendSer(h = h, ser_weight = ser_weight, ...)
56 }
57
58
59 #' @title Rotates dend based on DendSer
60 #' @export
61 #' @description
62 #' Rotates a dendrogram based on its seriation
63 #'
64 #' The function tries to turn the dend into hclust using
65 #' \link{DendSer.dendrogram} (based on \link[DendSer]{DendSer})
66 #'
67 #' Also, if a distance matrix is missing, it will try
68 #' to use the \link{cophenetic} distance.
69 #' @param dend An object of class dendrogram
70 #' @param ser_weight Used by cost function to evaluate
71 #' ordering. For cost=costLS, this is a vector of
72 #' object weights. Otherwise is a dist or symmetric matrix.
73 #' passed to \link{DendSer.dendrogram} and from
74 #' there to \link[DendSer]{DendSer}.
75 #'
76 #' If it is missing, the cophenetic distance is used instead.
77 #' @param ... parameters passed to \link[DendSer]{DendSer}
78 #' @return Numeric vector giving an optimal dendrogram order
79 #' @seealso \code{\link[DendSer]{DendSer}}, \link{DendSer.dendrogram} ,
80 #' \link{untangle_DendSer}, \link{rotate_DendSer}
81 #' @examples
82 #' \dontrun{
83 #' library(DendSer) # already used from within the function
84 #'
85 #' dend <- USArrests[1:4, ] %>%
86 #' dist() %>%
87 #' hclust("ave") %>%
88 #' as.dendrogram()
89 #' DendSer.dendrogram(dend)
90 #'
91 #' tanglegram(dend, rotate_DendSer(dend))
92 #' }
93 rotate_DendSer <- function(dend, ser_weight, ...) {
94 ord <- tryCatch(DendSer.dendrogram(dend, ser_weight = ser_weight), error = function(e) seq_len(nleaves(dend)))
95 # print(ord)
96 rotate(dend, ord)
97 }
98
99 #' @title Tries to run DendSer on a dendrogram
100 #' @export
101 #' @description
102 #' The function tries to turn the dend into hclust.
103 #' It then uses the \link{cophenetic} distance matrix
104 #' for optimizing the tree's rotation.
105 #'
106 #' This is a good (and fast) starting point for \link{untangle_step_rotate_2side}
107 #' @param dend An object of class \link{dendlist}
108 #' @param ... NOT USED
109 #' @return A dendlist object with ordered dends
110 #' @seealso \code{\link[DendSer]{DendSer}}, \link{DendSer.dendrogram} ,
111 #' \link{untangle_DendSer}, \link{rotate_DendSer}
112 #' @examples
113 #' \dontrun{
114 #' set.seed(232)
115 #' ss <- sample(1:150, 20)
116 #' dend1 <- iris[ss, -5] %>%
117 #' dist() %>%
118 #' hclust("com") %>%
119 #' as.dendrogram()
120 #' dend2 <- iris[ss, -5] %>%
121 #' dist() %>%
122 #' hclust("sin") %>%
123 #' as.dendrogram()
124 #' dend12 <- dendlist(dend1, dend2)
125 #'
126 #' # bad solutions
127 #' dend12 %>% tanglegram()
128 #' dend12 %>%
129 #' untangle("step2") %>%
130 #' tanglegram()
131 #' dend12 %>%
132 #' untangle_DendSer() %>%
133 #' tanglegram()
134 #' # but the combination is quite awsome:
135 #' dend12 %>%
136 #' untangle_DendSer() %>%
137 #' untangle("step2") %>%
138 #' tanglegram()
139 #' }
140 untangle_DendSer <- function(dend, ...) {
141 dendlist(
142 rotate_DendSer(dend[[1]]),
143 rotate_DendSer(dend[[2]])
144 )
145 }