Debian Codegraph @

man/untangle_step_rotate_2side.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/untangle.R
3 \name{untangle_step_rotate_2side}
4 \alias{untangle_step_rotate_2side}
5 \title{Stepwise untangle two trees one at a time}
6 \usage{
7 untangle_step_rotate_2side(
8 dend1,
9 dend2,
10 L = 1.5,
11 direction = c("forward", "backward"),
12 max_n_iterations = 10L,
13 print_times = dendextend_options("warn"),
14 k_seq = NULL,
15 ...
16 )
17 }
18 \arguments{
19 \item{dend1}{a dendrogram object. The one we will rotate to best fit
20 dend2.}
21
22 \item{dend2}{a dendrogram object. The one we will rotate to best fit
23 dend1.}
24
25 \item{L}{the distance norm to use for measuring the distance between the
26 two trees. It can be any positive number,
27 often one will want to use 0, 1, 1.5, 2 (see 'details' in \link{entanglement}).}
28
29 \item{direction}{a character scalar, either "forward" (default) or "backward".
30 Impacts the direction of clustering that are tried. Either from 2 and up
31 (in case of "forward"), or from nleaves to down (in case of "backward")
32
33 If k_seq is not NULL, then it overrides "direction".}
34
35 \item{max_n_iterations}{integer. The maximal number of times to switch between optimizing one tree with another.}
36
37 \item{print_times}{logical (TRUE), should we print how many times we switched between rotating the two trees?}
38
39 \item{k_seq}{a sequence of k clusters to go through for improving
40 dend1. If NULL (default), then we use the "direction" parameter.}
41
42 \item{...}{not used}
43 }
44 \value{
45 A list with two dendrograms (dend1/dend2),
46 after they are rotated to best fit one another.
47 }
48 \description{
49 This is a greedy forward selection algorithm for rotating the tree and
50 looking for a better match.
51
52 This is useful for finding good trees for a \link{tanglegram}.
53
54 It goes through rotating dend1, then dend2, and so on - until a locally optimal solution is found.
55
56 Similar to "step1side", one tree is held fixed and the other tree is rotated.
57 This function goes through all of the k number of clusters (from 2 onward),
58 and each time rotates the branch which was introduced in the new k'th cluster.
59 This rotated tree is compared with the fixed tree, and if it has a better
60 entanglement, it will be used for the following iterations.
61 Once finished the rotated tree is held fixed, and the fixed tree
62 is now rotated. This continues until a local optimal solution is reached.
63 }
64 \examples{
65
66 \dontrun{
67 dend1 <- USArrests[1:20, ] \%>\%
68 dist() \%>\%
69 hclust() \%>\%
70 as.dendrogram()
71 dend2 <- USArrests[1:20, ] \%>\%
72 dist() \%>\%
73 hclust(method = "single") \%>\%
74 as.dendrogram()
75 set.seed(3525)
76 dend2 <- shuffle(dend2)
77 tanglegram(dend1, dend2, margin_inner = 6.5)
78 entanglement(dend1, dend2, L = 2) # 0.79
79
80 dend2_corrected <- untangle_step_rotate_1side(dend2, dend1)
81 tanglegram(dend1, dend2_corrected, margin_inner = 6.5) # Good.
82 entanglement(dend1, dend2_corrected, L = 2) # 0.0067
83 # it is better, but not perfect. Can we improve it?
84
85 dend12_corrected <- untangle_step_rotate_2side(dend1, dend2)
86 tanglegram(dend12_corrected[[1]], dend12_corrected[[2]], margin_inner = 6.5) # Better...
87 entanglement(dend12_corrected[[1]], dend12_corrected[[2]], L = 2) # 0.0045
88
89
90 # best combination:
91 dend12_corrected_1 <- untangle_random_search(dend1, dend2)
92 dend12_corrected_2 <- untangle_step_rotate_2side(dend12_corrected_1[[1]], dend12_corrected_1[[2]])
93 tanglegram(dend12_corrected_2[[1]], dend12_corrected_2[[2]], margin_inner = 6.5) # Better...
94 entanglement(dend12_corrected_2[[1]], dend12_corrected_2[[2]], L = 2) # 0 - PERFECT.
95 }
96 }
97 \seealso{
98 \link{tanglegram}, \link{match_order_by_labels},
99 \link{entanglement}, \link{flip_leaves}, \link{all_couple_rotations_at_k}.
100 \link{untangle_step_rotate_1side}.
101 }