Debian Codegraph @

R/find_dend.R

raw
1
2
3 #' @title Finds a "good" dendrogram for a dist
4 #' @export
5 #' @rdname find_dend
6 #'
7 #' @description
8 #' There are many options for choosing distance and linkage functions for hclust.
9 #' This function goes through various combinations of the two and helps find the one
10 #' that is most "similar" to the original distance matrix.
11 #'
12 #' @param x A matrix or a data.frame. Can also be a \link{dist} object.
13 #'
14 #' @param dist_methods A vector of possible \link{dist} methods.
15 #' @param hclust_methods A vector of possible \link{hclust} methods.
16 #' @param hclust_fun By default \link{hclust}.
17 #' @param optim_fun A function that accepts a dend and a dist and returns how the two
18 #' are in agreement. Default is \link{cor_cophenetic}.
19 #' @param ... options passed from find_dend to dend_expend.
20 #'
21 #' @return
22 #' dend_expend:
23 #' A list with three items. The first item is called "dends" and includes
24 #' a dendlist with all the possible dendrogram combinations. The second is "dists" and
25 #' includes a list with all the possible distance matrix combination.
26 #' The third. "performance", is data.frame with three columns: dist_methods, hclust_methods, and optim.
27 #' optim is calculated (by default) as the cophenetic correlation (see: \link{cor_cophenetic}) between the distance matrix and
28 #' the \link{cophenetic} distance of the hclust object.
29 #'
30 #' @examples
31 #'
32 #' x <- datasets::mtcars
33 #' out <- dend_expend(x, dist_methods = c("euclidean", "manhattan"))
34 #' out$performance
35 #'
36 #' dend_expend(dist(x))$performance
37 #'
38 #' best_dend <- find_dend(x, dist_methods = c("euclidean", "manhattan"))
39 #' plot(best_dend)
40 dend_expend <- function(x,
41 dist_methods = c("euclidean", "maximum", "manhattan", "canberra", "binary", "minkowski"),
42 hclust_methods = c("ward.D", "ward.D2", "single", "complete", "average", "mcquitty", "median", "centroid"),
43 hclust_fun = hclust,
44 optim_fun = cor_cophenetic,
45 ...) {
46 if (is.dist(x)) {
47 dist_combo <- list(x)
48 names(dist_combo) <- dist_methods <- "unknown"
49 } else {
50 dist_combo <- list()
51 for (i in 1:length(dist_methods)) {
52 dist_combo[[i]] <- x %>% dist(method = dist_methods[i])
53 }
54 names(dist_combo) <- dist_methods
55 }
56
57
58 out_dendlist <- dendlist()
59 dist_hclust_combo <- expand.grid(dist_methods, hclust_methods) %>% data.frame(stringsAsFactors = TRUE)
60 colnames(dist_hclust_combo) <- c("dist_methods", "hclust_methods")
61
62 for (i in 1:nrow(dist_hclust_combo)) {
63 tmp_dist_name <- dist_hclust_combo[i, 1]
64 tmp_dist <- dist_combo[[tmp_dist_name]]
65 tmp_hclust_method <- dist_hclust_combo[i, 2]
66 tmp_dend <- tmp_dist %>%
67 hclust(method = tmp_hclust_method) %>%
68 as.dendrogram()
69
70 dist_hclust_combo$optim[i] <- optim_fun(tmp_dend, tmp_dist)
71
72 out_dendlist <- dendlist(out_dendlist, tmp_dend)
73 }
74
75 names(out_dendlist) <- paste(dist_hclust_combo[, 1], dist_hclust_combo[, 2], sep = "_")
76 # attr(out_dendlist, "dist_method") <- dist_hclust_combo[,1]
77 # attr(out_dendlist, "hclust_method") <- dist_hclust_combo[,2]
78
79 # which.max(dist_hclust_combo$optim)
80 # dist_hclust_combo[29,]
81
82 list(
83 dends = out_dendlist,
84 dists = dist_combo,
85 performance = dist_hclust_combo
86 )
87 }
88
89
90 # a <- dend_expend(mtcars)
91 # cophenetic(a[[1]])
92 # dist(mtcars)
93 #
94 # identical(labels(cophenetic(a[[1]])),
95 # labels(dist(mtcars)))
96 #
97 # identical(labels(sort_dist_mat(cophenetic(a[[1]]))),
98 # labels(sort_dist_mat(dist(mtcars))))
99
100
101
102 #' @export
103 #' @rdname find_dend
104 #' @return
105 #' find_dend: A dendrogram which is "optimal" based on the output from dend_expend.
106 find_dend <- function(x, ...) {
107 out <- dend_expend(x, ...)
108 best_dend_loc <- which.max(out$performance$optim)
109 out$dends[[best_dend_loc]]
110 }