| 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 |
#' @title Duplicate a leaf X times |
| 23 |
#' @export |
| 24 |
#' @description |
| 25 |
#' Duplicates a leaf in a tree. Useful for non-parametric bootstraping trees |
| 26 |
#' since it emulates what would have happened if the tree was constructed based |
| 27 |
#' on a row-sample with replacments from the original data matrix. |
| 28 |
#' @param dend a dendrogram object |
| 29 |
#' @param leaf_label the label of the laef to replicate. |
| 30 |
#' @param times the number of times we will have this leaf after replication |
| 31 |
#' @param fix_members logical (TRUE). Fix the number of members in attr |
| 32 |
#' using \link{fix_members_attr.dendrogram} |
| 33 |
#' @param fix_order logical (TRUE). Fix the leaves order |
| 34 |
#' @param fix_midpoint logical (TRUE). Fix the midpoint value. |
| 35 |
#' If TRUE, it overrides "fix_members" and turns it into TRUE (since it must |
| 36 |
#' have a correct number of members in order to work). |
| 37 |
#' values using \link{rank_order.dendrogram} |
| 38 |
#' @param ... not used |
| 39 |
#' @return |
| 40 |
#' A dendrogram, after duplicating one of its leaves. |
| 41 |
#' @examples |
| 42 |
#' |
| 43 |
#' \dontrun{ |
| 44 |
#' # define dendrogram object to play with: |
| 45 |
#' dend <- USArrests[1:3, ] %>% |
| 46 |
#' dist() %>% |
| 47 |
#' hclust(method = "ave") %>% |
| 48 |
#' as.dendrogram() |
| 49 |
#' plot(dend) |
| 50 |
#' duplicate_leaf(dend, "Alaska", 3) |
| 51 |
#' duplicate_leaf(dend, "Arizona", 2, fix_members = FALSE, fix_order = FALSE) |
| 52 |
#' plot(duplicate_leaf(dend, "Alaska", 2)) |
| 53 |
#' plot(duplicate_leaf(dend, "Alaska", 4)) |
| 54 |
#' plot(duplicate_leaf(dend, "Arizona", 2)) |
| 55 |
#' plot(duplicate_leaf(dend, "Arizona", 4)) |
| 56 |
#' } |
| 57 |
#' |
| 58 |
duplicate_leaf <- function(dend, leaf_label, times, fix_members = TRUE, fix_order = TRUE, fix_midpoint = TRUE, ...) { |
| 59 |
|
| 60 |
# extreme case: |
| 61 |
if (length(leaf_label) > 1) { |
| 62 |
leaf_label <- leaf_label[1] |
| 63 |
warning("leaf_label had a length >1, only the first element was used.") |
| 64 |
} |
| 65 |
|
| 66 |
# dend_node <- dend |
| 67 |
duplicate_leaf_in_node <- function(dend_node) { |
| 68 |
|
| 69 |
# if this is a leaf - we can not replicate the leaf |
| 70 |
# we have to do it in the parent node. |
| 71 |
if (is.leaf(dend_node)) { |
| 72 |
return(unclass(dend_node)) |
| 73 |
} |
| 74 |
|
| 75 |
# if leaf_label is not in this node, return the dend_node |
| 76 |
# as there is no point to keep going done that node |
| 77 |
if (!(leaf_label %in% labels(dend_node))) { |
| 78 |
return(unclass(dend_node)) |
| 79 |
} |
| 80 |
|
| 81 |
# since the leaf is in this node, is it one of its children? |
| 82 |
|
| 83 |
# let's first get the childrens labels |
| 84 |
return_label_or_NA <- function(x) { |
| 85 |
x_label <- attr(x, "label") |
| 86 |
if (is.null(x_label)) x_label <- NA |
| 87 |
x_label |
| 88 |
} |
| 89 |
# typeof(sapply(dend, return_label_or_NA)) |
| 90 |
childrens_labels <- sapply(dend_node, return_label_or_NA) |
| 91 |
# if we can't find the leaf in one of these node's children, |
| 92 |
# then Recall the function on all children nodes and then return the node: |
| 93 |
if (!(leaf_label %in% childrens_labels)) { |
| 94 |
for (i in seq(dend_node)) { |
| 95 |
dend_node[[i]] <- Recall(dend_node[[i]]) |
| 96 |
} |
| 97 |
return(unclass(dend_node)) |
| 98 |
} |
| 99 |
|
| 100 |
# but if it is here(!), let us duplicate it: |
| 101 |
n_childrens <- length(childrens_labels) |
| 102 |
leaf_label_location <- which(childrens_labels == leaf_label) |
| 103 |
the_leaf <- unclass(dend_node[[leaf_label_location]]) |
| 104 |
leaf_order_value <- as.vector(the_leaf) |
| 105 |
|
| 106 |
for (i in 2:times) { |
| 107 |
temp_new_leaf <- the_leaf |
| 108 |
attr(temp_new_leaf, "label") <- paste(leaf_label, i, sep = "_") |
| 109 |
temp_new_leaf[1] <- leaf_order_value + i / (n_childrens + 1) |
| 110 |
dend_node[[n_childrens + i - 1]] <- temp_new_leaf |
| 111 |
} |
| 112 |
|
| 113 |
return(unclass(dend_node)) |
| 114 |
} |
| 115 |
|
| 116 |
if (!(leaf_label %in% labels(dend))) { |
| 117 |
warning(paste("The label", leaf_label, "Is not available in this tree. Original tree returned.")) |
| 118 |
return(dend) |
| 119 |
} |
| 120 |
|
| 121 |
if (times == 1) { |
| 122 |
# no point in replicating this if it needs to be only once, now is there? |
| 123 |
return(dend) |
| 124 |
} |
| 125 |
|
| 126 |
if (!is.leaf(dend)) { |
| 127 |
dend <- duplicate_leaf_in_node(dend) |
| 128 |
} else { |
| 129 |
# extreme case: |
| 130 |
# in this case, we need to do something special |
| 131 |
# if we got this far - we already know this tree has the only leaf we want. |
| 132 |
attr(dend, "height") <- 0 # just to be sure the leaf has height 0. |
| 133 |
new_dend <- list() |
| 134 |
# dend <- rep(dend,times) # FAILS! |
| 135 |
for (i in seq_len(times)) new_dend[[i]] <- dend |
| 136 |
dend <- new_dend |
| 137 |
attr(dend, "members") <- times |
| 138 |
attr(dend, "height") <- 1 |
| 139 |
labels(dend) <- paste(leaf_label, seq_len(times), sep = "_") |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
class(dend) <- "dendrogram" |
| 144 |
|
| 145 |
if (fix_midpoint) fix_members <- TRUE # for midpoint to work, we MUST have a correct number of members. |
| 146 |
if (fix_members) dend <- fix_members_attr.dendrogram(dend) |
| 147 |
if (fix_order) dend <- rank_order.dendrogram(dend) |
| 148 |
if (fix_midpoint) dend <- suppressWarnings(stats_midcache.dendrogram(dend)) # fixing the middle point thing |
| 149 |
|
| 150 |
return(dend) |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
#' @title Sample a tree |
| 165 |
#' @export |
| 166 |
#' @description |
| 167 |
#' Samples a tree, either by permuting the labels (which is usefull for |
| 168 |
#' a permutation test), or by repeated sampling of the same labels (essential |
| 169 |
#' for bootstraping when we don't have access to the original data which |
| 170 |
#' produced the tree). |
| 171 |
#' |
| 172 |
#' Duplicates a leaf in a tree. Useful for non-parametric bootstraping trees |
| 173 |
#' since it emulates what would have happened if the tree was constructed based |
| 174 |
#' on a row-sample with replacments from the original data matrix. |
| 175 |
#' @param dend a dendrogram object |
| 176 |
#' @param replace logical (FALSE). Should we shuffle the labels (if FALSE), |
| 177 |
#' or should we replicate the same leaf over and over, while omitting other |
| 178 |
#' leaves? (this is when set to TRUE). |
| 179 |
#' @param dend_labels a character vector of the tree's labels. |
| 180 |
#' This can save the time it takes for getting the tree labels (in case we run |
| 181 |
#' a simulating, computing this once might save some running time). |
| 182 |
#' If missing, it uses \link{labels} in order to get the labels. |
| 183 |
#' @param sampled_labels a character vector of the tree's sampled labels. |
| 184 |
#' This can help us if we wish to compare two trees. In such a case we'd like |
| 185 |
#' to be able to have the same sample of labels used on both trees. |
| 186 |
#' If missing, it uses \link{sample} in order to get the sampled labels. |
| 187 |
#' |
| 188 |
#' Only works when replace=TRUE! |
| 189 |
#' @param fix_members logical (TRUE). Fix the number of members in attr |
| 190 |
#' using \link{fix_members_attr.dendrogram} |
| 191 |
#' @param fix_order logical (TRUE). Fix the leaves order |
| 192 |
#' @param fix_midpoint logical (TRUE). Fix the midpoint value. |
| 193 |
#' If TRUE, it overrides "fix_members" and turns it into TRUE (since it must |
| 194 |
#' have a correct number of members in order to work). |
| 195 |
#' values using \link{rank_order.dendrogram} |
| 196 |
#' @param ... not used |
| 197 |
#' @return |
| 198 |
#' A dendrogram, after "sampling" its leaves. |
| 199 |
#' @seealso |
| 200 |
#' \link{sample}, \link{duplicate_leaf} |
| 201 |
#' @examples |
| 202 |
#' |
| 203 |
#' \dontrun{ |
| 204 |
#' # define dendrogram object to play with: |
| 205 |
#' dend <- USArrests[1:5, ] %>% |
| 206 |
#' dist() %>% |
| 207 |
#' hclust(method = "ave") %>% |
| 208 |
#' as.dendrogram() |
| 209 |
#' plot(dend) |
| 210 |
#' |
| 211 |
#' # # same tree, with different order of labels |
| 212 |
#' plot(sample.dendrogram(dend, replace = FALSE)) |
| 213 |
#' |
| 214 |
#' # # A different tree (!), with some labels duplicated, |
| 215 |
#' # while others are pruned |
| 216 |
#' plot(sample.dendrogram(dend, replace = TRUE)) |
| 217 |
#' } |
| 218 |
#' |
| 219 |
sample.dendrogram <- function(dend, replace = FALSE, |
| 220 |
dend_labels, sampled_labels, |
| 221 |
fix_members = TRUE, fix_order = TRUE, fix_midpoint = TRUE, ...) { |
| 222 |
if (missing(dend_labels)) dend_labels <- labels(dend) |
| 223 |
|
| 224 |
if (replace) { |
| 225 |
if (missing(sampled_labels)) sampled_labels <- sample(dend_labels, replace = TRUE) |
| 226 |
|
| 227 |
# 1) prune redundent leaves |
| 228 |
ss_kept_labels <- dend_labels %in% sampled_labels |
| 229 |
ss_removed_labels <- !ss_kept_labels |
| 230 |
removed_labels <- dend_labels[ss_removed_labels] |
| 231 |
dend <- prune(dend, leaves = removed_labels) |
| 232 |
|
| 233 |
|
| 234 |
# 2) add new leaves |
| 235 |
# |
| 236 |
# table of the new sampled labels |
| 237 |
t_sampled_labels <- table(sampled_labels) |
| 238 |
names_t_sampled_labels <- names(t_sampled_labels) |
| 239 |
for (i in seq_along(t_sampled_labels)) { |
| 240 |
dend <- duplicate_leaf(dend, |
| 241 |
leaf_label = names_t_sampled_labels[i], |
| 242 |
times = unname(t_sampled_labels[i]), |
| 243 |
fix_members = FALSE, |
| 244 |
fix_order = FALSE, |
| 245 |
fix_midpoint = FALSE |
| 246 |
) |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
# it is better to do it here than over at duplicate_leaf |
| 251 |
# since we are fine with doing this fix only once after we finished |
| 252 |
# adding all of the new labels. |
| 253 |
# # 3) Fix members |
| 254 |
# # 4) Fix leaves order values |
| 255 |
if (fix_midpoint) fix_members <- TRUE # for midpoint to work, we MUST have a correct number of members. |
| 256 |
if (fix_members) dend <- fix_members_attr.dendrogram(dend) |
| 257 |
if (fix_order) dend <- rank_order.dendrogram(dend) |
| 258 |
if (fix_midpoint) dend <- suppressWarnings(stats_midcache.dendrogram(dend)) # fixing the middle point thing |
| 259 |
} else { |
| 260 |
# don't replace |
| 261 |
# this is MUCH simpler, and useful for permutation tests. |
| 262 |
|
| 263 |
n_dend <- nleaves(dend) |
| 264 |
new_order <- sample(n_dend) |
| 265 |
labels(dend) <- dend_labels[new_order] |
| 266 |
if (fix_order) order.dendrogram(dend) <- order.dendrogram(dend)[new_order] |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
class(dend) <- "dendrogram" |
| 271 |
|
| 272 |
return(dend) |
| 273 |
} |