| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/cutree.dendrogram.R |
| 3 |
\name{cutree} |
| 4 |
\alias{cutree} |
| 5 |
\alias{cutree.default} |
| 6 |
\alias{cutree.hclust} |
| 7 |
\alias{cutree.phylo} |
| 8 |
\alias{cutree.agnes} |
| 9 |
\alias{cutree.diana} |
| 10 |
\alias{cutree.dendrogram} |
| 11 |
\title{Cut a Tree (Dendrogram/hclust/phylo) into Groups of Data} |
| 12 |
\usage{ |
| 13 |
cutree(tree, k = NULL, h = NULL, ...) |
| 14 |
|
| 15 |
\method{cutree}{default}(tree, k = NULL, h = NULL, ...) |
| 16 |
|
| 17 |
\method{cutree}{hclust}( |
| 18 |
tree, |
| 19 |
k = NULL, |
| 20 |
h = NULL, |
| 21 |
use_labels_not_values = TRUE, |
| 22 |
order_clusters_as_data = TRUE, |
| 23 |
warn = dendextend_options("warn"), |
| 24 |
NA_to_0L = TRUE, |
| 25 |
... |
| 26 |
) |
| 27 |
|
| 28 |
\method{cutree}{phylo}(tree, k = NULL, h = NULL, ...) |
| 29 |
|
| 30 |
\method{cutree}{phylo}(tree, k = NULL, h = NULL, ...) |
| 31 |
|
| 32 |
\method{cutree}{agnes}(tree, k = NULL, h = NULL, ...) |
| 33 |
|
| 34 |
\method{cutree}{diana}(tree, k = NULL, h = NULL, ...) |
| 35 |
|
| 36 |
\method{cutree}{dendrogram}( |
| 37 |
tree, |
| 38 |
k = NULL, |
| 39 |
h = NULL, |
| 40 |
dend_heights_per_k = NULL, |
| 41 |
use_labels_not_values = TRUE, |
| 42 |
order_clusters_as_data = TRUE, |
| 43 |
warn = dendextend_options("warn"), |
| 44 |
try_cutree_hclust = TRUE, |
| 45 |
NA_to_0L = TRUE, |
| 46 |
... |
| 47 |
) |
| 48 |
} |
| 49 |
\arguments{ |
| 50 |
\item{tree}{a dendrogram object} |
| 51 |
|
| 52 |
\item{k}{numeric scalar (OR a vector) with the number of clusters |
| 53 |
the tree should be cut into.} |
| 54 |
|
| 55 |
\item{h}{numeric scalar (OR a vector) with a height where the tree |
| 56 |
should be cut.} |
| 57 |
|
| 58 |
\item{...}{(not currently in use)} |
| 59 |
|
| 60 |
\item{use_labels_not_values}{logical, defaults to TRUE. If the actual labels of the |
| 61 |
clusters do not matter - and we want to gain speed (say, 10 times faster) - |
| 62 |
then use FALSE (gives the "leaves order" instead of their labels.). |
| 63 |
This is passed to \code{cutree_1h.dendrogram}.} |
| 64 |
|
| 65 |
\item{order_clusters_as_data}{logical, defaults to TRUE. There are two ways by which |
| 66 |
to order the clusters: 1) By the order of the original data. 2) by the order of the |
| 67 |
labels in the dendrogram. In order to be consistent with \link[stats]{cutree}, this is set |
| 68 |
to TRUE. |
| 69 |
This is passed to \code{cutree_1h.dendrogram}.} |
| 70 |
|
| 71 |
\item{warn}{logical (default from dendextend_options("warn") is FALSE). |
| 72 |
Set if warning are to be issued, it is safer to keep this at TRUE, |
| 73 |
but for keeping the noise down, the default is FALSE. |
| 74 |
Should the function send a warning in case the desried k is not available?} |
| 75 |
|
| 76 |
\item{NA_to_0L}{logical. default is TRUE. When no clusters are possible, |
| 77 |
Should the function return 0 (TRUE, default), or NA (when set to FALSE).} |
| 78 |
|
| 79 |
\item{dend_heights_per_k}{a named vector that resulted from running. |
| 80 |
\code{heights_per_k.dendrogram}. When running the function many times, |
| 81 |
supplying this object will help improve the running time if using k!=NULL .} |
| 82 |
|
| 83 |
\item{try_cutree_hclust}{logical. default is TRUE. Since cutree for hclust is |
| 84 |
MUCH faster than for dendrogram - cutree.dendrogram will first try to change the |
| 85 |
dendrogram into an hclust object. If it will fail (for example, with unbranched trees), |
| 86 |
it will continue using the cutree.dendrogram function. |
| 87 |
If try_cutree_hclust=FALSE, it will force to use cutree.dendrogram and not |
| 88 |
cutree.hclust.} |
| 89 |
} |
| 90 |
\value{ |
| 91 |
If k or h are scalar - \code{cutree.dendrogram} returns an integer vector with group |
| 92 |
memberships. |
| 93 |
Otherwise a matrix with group memberships is returned where each column |
| 94 |
corresponds to the elements of k or h, respectively |
| 95 |
(which are also used as column names). |
| 96 |
|
| 97 |
In case there exists no such k for which exists a relevant split of the |
| 98 |
dendrogram, a warning is issued to the user, and NA is returned. |
| 99 |
} |
| 100 |
\description{ |
| 101 |
Cuts a dendrogram tree into several groups |
| 102 |
by specifying the desired number of clusters k(s), or cut height(s). |
| 103 |
|
| 104 |
For \code{hclust.dendrogram} - |
| 105 |
In case there exists no such k for which exists a relevant split of the |
| 106 |
dendrogram, a warning is issued to the user, and NA is returned. |
| 107 |
} |
| 108 |
\details{ |
| 109 |
At least one of k or h must be specified, k overrides h if both are given. |
| 110 |
|
| 111 |
as opposed to \link[stats]{cutree} for hclust, \code{cutree.dendrogram} allows the |
| 112 |
cutting of trees at a given height also for non-ultrametric trees |
| 113 |
(ultrametric tree == a tree with monotone clustering heights). |
| 114 |
} |
| 115 |
\examples{ |
| 116 |
|
| 117 |
\dontrun{ |
| 118 |
hc <- hclust(dist(USArrests[c(1, 6, 13, 20, 23), ]), "ave") |
| 119 |
dend <- as.dendrogram(hc) |
| 120 |
unbranch_dend <- unbranch(dend, 2) |
| 121 |
|
| 122 |
cutree(hc, k = 2:4) # on hclust |
| 123 |
cutree(dend, k = 2:4) # on dendrogram |
| 124 |
|
| 125 |
cutree(hc, k = 2) # on hclust |
| 126 |
cutree(dend, k = 2) # on dendrogram |
| 127 |
|
| 128 |
cutree(dend, h = c(20, 25.5, 50, 170)) |
| 129 |
cutree(hc, h = c(20, 25.5, 50, 170)) |
| 130 |
|
| 131 |
# the default (ordered by original data's order) |
| 132 |
cutree(dend, k = 2:3, order_clusters_as_data = FALSE) |
| 133 |
labels(dend) |
| 134 |
|
| 135 |
# as.hclust(unbranch_dend) # ERROR - can not do this... |
| 136 |
cutree(unbranch_dend, k = 2) # all NA's |
| 137 |
cutree(unbranch_dend, k = 1:4) |
| 138 |
cutree(unbranch_dend, h = c(20, 25.5, 50, 170)) |
| 139 |
cutree(dend, h = c(20, 25.5, 50, 170)) |
| 140 |
|
| 141 |
|
| 142 |
library(microbenchmark) |
| 143 |
## this shows how as.hclust is expensive - but still worth it if possible |
| 144 |
microbenchmark( |
| 145 |
cutree(hc, k = 2:4), |
| 146 |
cutree(as.hclust(dend), k = 2:4), |
| 147 |
cutree(dend, k = 2:4), |
| 148 |
cutree(dend, k = 2:4, try_cutree_hclust = FALSE) |
| 149 |
) |
| 150 |
# the dendrogram is MUCH slower... |
| 151 |
|
| 152 |
# Unit: microseconds |
| 153 |
## expr min lq median uq max neval |
| 154 |
## cutree(hc, k = 2:4) 91.270 96.589 99.3885 107.5075 338.758 100 |
| 155 |
## tree(as.hclust(dend), |
| 156 |
## k = 2:4) 1701.629 1767.700 1854.4895 2029.1875 8736.591 100 |
| 157 |
## cutree(dend, k = 2:4) 1807.456 1869.887 1963.3960 2125.2155 5579.705 100 |
| 158 |
## cutree(dend, k = 2:4, |
| 159 |
## try_cutree_hclust = FALSE) 8393.914 8570.852 8755.3490 9686.7930 14194.790 100 |
| 160 |
|
| 161 |
# and trying to "hclust" is not expensive (which is nice...) |
| 162 |
microbenchmark( |
| 163 |
cutree_unbranch_dend = cutree(unbranch_dend, k = 2:4), |
| 164 |
cutree_unbranch_dend_not_trying_to_hclust = |
| 165 |
cutree(unbranch_dend, k = 2:4, try_cutree_hclust = FALSE) |
| 166 |
) |
| 167 |
|
| 168 |
|
| 169 |
## Unit: milliseconds |
| 170 |
## expr min lq median uq max neval |
| 171 |
## cutree_unbranch_dend 7.309329 7.428314 7.494107 7.752234 17.59581 100 |
| 172 |
## cutree_unbranch_dend_not |
| 173 |
## _trying_to_hclust 6.945375 7.079198 7.148629 7.577536 16.99780 100 |
| 174 |
## There were 50 or more warnings (use warnings() to see the first 50) |
| 175 |
|
| 176 |
# notice that if cutree can't find clusters for the desired k/h, it will produce 0's instead! |
| 177 |
# (It will produce a warning though...) |
| 178 |
# This is a different behaviout than stats::cutree |
| 179 |
# For example: |
| 180 |
cutree(as.dendrogram(hclust(dist(c(1, 1, 1, 2, 2)))), |
| 181 |
k = 5 |
| 182 |
) |
| 183 |
} |
| 184 |
|
| 185 |
} |
| 186 |
\seealso{ |
| 187 |
\code{\link{hclust}}, \code{\link[stats]{cutree}}, |
| 188 |
\code{\link{cutree_1h.dendrogram}}, \code{\link{cutree_1k.dendrogram}}, |
| 189 |
} |
| 190 |
\author{ |
| 191 |
\code{cutree.dendrogram} was written by Tal Galili. |
| 192 |
\code{cutree.hclust} is redirecting the function |
| 193 |
to \link[stats]{cutree} from base R. |
| 194 |
} |