| 1 |
# library(testthat) |
| 2 |
# library(dendextend) |
| 3 |
|
| 4 |
context("Bk method (FM Index) between two trees") |
| 5 |
|
| 6 |
|
| 7 |
test_that("sort_2_clusters_vectors works", { |
| 8 |
suppressWarnings(RNGversion("3.5.0")) |
| 9 |
set.seed(23235) |
| 10 |
ss <- sample(1:150, 4) |
| 11 |
hc1 <- hclust(dist(datasets::iris[ss, -5]), "com") |
| 12 |
hc2 <- hclust(dist(datasets::iris[ss, -5]), "single") |
| 13 |
# dend1 <- as.dendrogram(hc1) |
| 14 |
# dend2 <- as.dendrogram(hc2) |
| 15 |
# cutree(dend1) |
| 16 |
|
| 17 |
A1_clusters <- cutree(hc1, k = 3) |
| 18 |
A2_clusters <- sample(cutree(hc1, k = 3)) |
| 19 |
|
| 20 |
sorted_As <- sort_2_clusters_vectors(A1_clusters, A2_clusters, assume_sorted_vectors = TRUE) # no sorting |
| 21 |
expect_false(identical(sorted_As[[1]], sorted_As[[2]])) |
| 22 |
|
| 23 |
sorted_As <- sort_2_clusters_vectors(A1_clusters, A2_clusters, assume_sorted_vectors = FALSE) # Sorted |
| 24 |
expect_true(identical(sorted_As[[1]], sorted_As[[2]])) |
| 25 |
|
| 26 |
# test edge case warnings |
| 27 |
A2_clusters <- A2_clusters[1:3] |
| 28 |
expect_warning( |
| 29 |
result <- sort_2_clusters_vectors(A1_clusters, A2_clusters, warn = T) |
| 30 |
) |
| 31 |
expect_identical(A1_clusters, result[[1]]) |
| 32 |
expect_identical(A2_clusters, result[[2]]) |
| 33 |
|
| 34 |
names(A2_clusters) <- NULL |
| 35 |
expect_warning( |
| 36 |
result <- sort_2_clusters_vectors(A1_clusters, A2_clusters, warn = T) |
| 37 |
) |
| 38 |
expect_identical(A1_clusters, result[[1]]) |
| 39 |
expect_identical(A2_clusters, result[[2]]) |
| 40 |
|
| 41 |
# create custom object with different length of names as length of object to trigger warning |
| 42 |
custom_object <- list(a = 1, b = 2, c = 3) |
| 43 |
class(custom_object) <- "custom_class" |
| 44 |
names.custom_class <<- function(x) { |
| 45 |
return(c("Name1", "Name2", "Name3", "Name4", "Name5")) |
| 46 |
} |
| 47 |
expect_warning(sort_2_clusters_vectors(custom_object, custom_object, warn = T)) |
| 48 |
}) |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
test_that("FM_index_R works", { |
| 54 |
suppressWarnings(RNGversion("3.5.0")) |
| 55 |
|
| 56 |
# set.seed(23235) |
| 57 |
ss <- TRUE # sample(1:150, 10 ) |
| 58 |
hc1 <- hclust(dist(datasets::iris[ss, -5]), "com") |
| 59 |
hc2 <- hclust(dist(datasets::iris[ss, -5]), "single") |
| 60 |
# dend1 <- as.dendrogram(hc1) |
| 61 |
# dend2 <- as.dendrogram(hc2) |
| 62 |
# cutree(dend1) |
| 63 |
|
| 64 |
# FM index of a cluster with himself is 1: |
| 65 |
expect_equivalent(FM_index_R(cutree(hc1, k = 3), cutree(hc1, k = 3)), 1) |
| 66 |
# removing the attr - it is EXACTLY 1: (but NOT 1L) |
| 67 |
expect_identical(as.vector(FM_index_R(cutree(hc1, k = 3), cutree(hc1, k = 3))), 1) |
| 68 |
|
| 69 |
# sorting of the clusters based on their names: |
| 70 |
set.seed(1341) |
| 71 |
expect_false(identical( |
| 72 |
as.vector( |
| 73 |
FM_index_R(cutree(hc1, k = 3), |
| 74 |
sample(cutree(hc1, k = 3)), |
| 75 |
assume_sorted_vectors = TRUE |
| 76 |
) |
| 77 |
), 1 |
| 78 |
)) |
| 79 |
# It actually becomes: 0.38037 |
| 80 |
# but if we leave sorting as TRUE, we will get 1: |
| 81 |
|
| 82 |
expect_true(identical( |
| 83 |
as.vector(FM_index_R(cutree(hc1, k = 3), |
| 84 |
sample(cutree(hc1, k = 3)), |
| 85 |
assume_sorted_vectors = FALSE |
| 86 |
)), 1 |
| 87 |
)) |
| 88 |
# it is actually the default: |
| 89 |
expect_true(identical( |
| 90 |
as.vector(FM_index_R( |
| 91 |
cutree(hc1, k = 3), |
| 92 |
sample(cutree(hc1, k = 3)) |
| 93 |
)), 1 |
| 94 |
)) |
| 95 |
|
| 96 |
|
| 97 |
# we can get a range of FM inexes using the following: |
| 98 |
fo <- function(k) FM_index_R(cutree(hc1, k), cutree(hc2, k)) |
| 99 |
# dput(round(unname(sapply(1:4, fo)),2)) |
| 100 |
|
| 101 |
expect_identical( |
| 102 |
round(unname(sapply(1:4, fo)), 2), |
| 103 |
c(1, 0.71, 0.81, 0.75) |
| 104 |
) |
| 105 |
# ks <- 1:150 |
| 106 |
# plot(sapply(ks, fo)~ ks, type = "b", main = "Bk plot for the datasets::iris dataset") |
| 107 |
# |
| 108 |
|
| 109 |
set.seed(234234) |
| 110 |
tmp_index <- FM_index_R(cutree(hc1, k = 3), sample(cutree(hc1, k = 3)), assume_sorted_vectors = TRUE) |
| 111 |
expect_identical(round(as.vector(tmp_index), 2), 0.38) |
| 112 |
expect_identical(round(attr(tmp_index, "E_FM"), 2), 0.37) |
| 113 |
expect_identical(round(sqrt(attr(tmp_index, "V_FM")), 3), 0.008) |
| 114 |
|
| 115 |
# test case where a cluster with an NA value is passed in |
| 116 |
hc1_with_na = cutree(hc1, k = 3) |
| 117 |
hc1_with_na[1] = NA |
| 118 |
expect_warning( |
| 119 |
FM_index_R(hc1_with_na, cutree(hc1, k = 3), warn = T) |
| 120 |
) |
| 121 |
|
| 122 |
# create custom class to pass in which triggers error otherwise not possible |
| 123 |
x <<- c(1, 2, 3, 4, 5) |
| 124 |
class(x) <<- "x" |
| 125 |
length.x <<- function(x) 1 |
| 126 |
y <<- c(1, 2, 3, 4, 5) |
| 127 |
class(y) <<- "y" |
| 128 |
length.y <<- function(y) 1 |
| 129 |
expect_error( |
| 130 |
FM_index_R(x, y, assume_sorted_vectors = T) |
| 131 |
) |
| 132 |
|
| 133 |
}) |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
test_that("FM_index works", { |
| 138 |
suppressWarnings(RNGversion("3.5.0")) |
| 139 |
# set.seed(23235) |
| 140 |
ss <- TRUE # sample(1:150, 10 ) |
| 141 |
hc1 <- hclust(dist(datasets::iris[ss, -5]), "com") |
| 142 |
hc2 <- hclust(dist(datasets::iris[ss, -5]), "single") |
| 143 |
# dend1 <- as.dendrogram(hc1) |
| 144 |
# dend2 <- as.dendrogram(hc2) |
| 145 |
# cutree(dend1) |
| 146 |
|
| 147 |
# FM index of a cluster with himself is 1: |
| 148 |
expect_equivalent(FM_index(cutree(hc1, k = 3), cutree(hc1, k = 3)), 1) |
| 149 |
# removing the attr - it is EXACTLY 1: (but NOT 1L) |
| 150 |
expect_identical(as.vector(FM_index(cutree(hc1, k = 3), cutree(hc1, k = 3))), 1) |
| 151 |
|
| 152 |
# sorting of the clusters based on their names: |
| 153 |
set.seed(1341) |
| 154 |
expect_false(identical( |
| 155 |
as.vector( |
| 156 |
FM_index(cutree(hc1, k = 3), |
| 157 |
sample(cutree(hc1, k = 3)), |
| 158 |
assume_sorted_vectors = TRUE |
| 159 |
) |
| 160 |
), 1 |
| 161 |
)) |
| 162 |
# It actually becomes: 0.38037 |
| 163 |
# but if we leave sorting as TRUE, we will get 1: |
| 164 |
|
| 165 |
expect_true(identical( |
| 166 |
as.vector(FM_index(cutree(hc1, k = 3), |
| 167 |
sample(cutree(hc1, k = 3)), |
| 168 |
assume_sorted_vectors = FALSE |
| 169 |
)), 1 |
| 170 |
)) |
| 171 |
# it is actually the default: |
| 172 |
expect_true(identical( |
| 173 |
as.vector(FM_index( |
| 174 |
cutree(hc1, k = 3), |
| 175 |
sample(cutree(hc1, k = 3)) |
| 176 |
)), 1 |
| 177 |
)) |
| 178 |
|
| 179 |
|
| 180 |
# we can get a range of FM inexes using the following: |
| 181 |
fo <- function(k) FM_index(cutree(hc1, k), cutree(hc2, k)) |
| 182 |
# dput(round(unname(sapply(1:4, fo)),2)) |
| 183 |
|
| 184 |
expect_identical( |
| 185 |
round(unname(sapply(1:4, fo)), 2), |
| 186 |
c(1, 0.71, 0.81, 0.75) |
| 187 |
) |
| 188 |
# ks <- 1:150 |
| 189 |
# plot(sapply(ks, fo)~ ks, type = "b", main = "Bk plot for the datasets::iris dataset") |
| 190 |
|
| 191 |
}) |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
test_that("FM_index_permutation works", { |
| 200 |
set.seed(23235) |
| 201 |
ss <- TRUE # sample(1:150, 10 ) |
| 202 |
hc1 <- hclust(dist(datasets::iris[ss, -5]), "com") |
| 203 |
hc2 <- hclust(dist(datasets::iris[ss, -5]), "single") |
| 204 |
# dend1 <- as.dendrogram(hc1) |
| 205 |
# dend2 <- as.dendrogram(hc2) |
| 206 |
# cutree(dend1) |
| 207 |
|
| 208 |
A1_clusters <- cutree(hc1, k = 3) |
| 209 |
A2_clusters <- A1_clusters |
| 210 |
|
| 211 |
R <- 10 |
| 212 |
set.seed(414130) |
| 213 |
FM_index_H0 <- replicate(R, FM_index_permutation(A1_clusters, A2_clusters)) # can take 10 sec |
| 214 |
|
| 215 |
expect_identical( |
| 216 |
round(mean(FM_index_H0), 2), |
| 217 |
0.37 |
| 218 |
) |
| 219 |
expect_identical( |
| 220 |
round(sd(FM_index_H0), 2), |
| 221 |
0.01 |
| 222 |
) |
| 223 |
}) |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
test_that("Bk works", { |
| 233 |
set.seed(23235) |
| 234 |
ss <- TRUE # sample(1:150, 10 ) |
| 235 |
hc1 <- hclust(dist(datasets::iris[ss, -5]), "com") |
| 236 |
hc2 <- hclust(dist(datasets::iris[ss, -5]), "single") |
| 237 |
dend1 <- as.dendrogram(hc1) |
| 238 |
dend2 <- as.dendrogram(hc2) |
| 239 |
# cutree(tree1) |
| 240 |
|
| 241 |
expect_identical( |
| 242 |
Bk(hc1, hc2, k = 3), |
| 243 |
Bk(dend1, dend2, k = 3, ) |
| 244 |
) |
| 245 |
|
| 246 |
expect_identical( |
| 247 |
Bk(hc1, hc2, k = 3), |
| 248 |
Bk(dend1, dend2, k = 3) |
| 249 |
) |
| 250 |
|
| 251 |
expect_identical( |
| 252 |
round(as.numeric(Bk(hc1, hc2, k = 3)), 3), |
| 253 |
0.806 |
| 254 |
) |
| 255 |
expect_identical( |
| 256 |
round(as.numeric(Bk(dend1, dend2, k = 3)), 3), |
| 257 |
0.806 |
| 258 |
) |
| 259 |
|
| 260 |
|
| 261 |
set.seed(23234535) |
| 262 |
expect_identical( |
| 263 |
round( |
| 264 |
Bk_permutations(dend1, |
| 265 |
dend2, |
| 266 |
k = 3, R = 2 |
| 267 |
)[[1]], 2 |
| 268 |
), |
| 269 |
c(.45, .44) |
| 270 |
) |
| 271 |
|
| 272 |
# test case where k is not specified |
| 273 |
expect_identical( |
| 274 |
Bk(dend1, dend2), |
| 275 |
Bk(dend1, dend2, k = 2:(nleaves(dend1) - 1)) |
| 276 |
) |
| 277 |
|
| 278 |
# test case where different number of items are provided |
| 279 |
hc1 <- hclust(dist(datasets::iris[1:149, -5]), "com") |
| 280 |
hc2 <- hclust(dist(datasets::iris[1:150, -5]), "single") |
| 281 |
dend1 <- as.dendrogram(hc1) |
| 282 |
dend2 <- as.dendrogram(hc2) |
| 283 |
expect_error(Bk(dend1, dend2, warn = T)) |
| 284 |
|
| 285 |
# test case where different labels are provided |
| 286 |
hc1 <- hclust(dist(datasets::iris[1:149, -5]), "com") |
| 287 |
hc2 <- hclust(dist(datasets::iris[2:150, -5]), "single") |
| 288 |
dend1 <- as.dendrogram(hc1) |
| 289 |
dend2 <- as.dendrogram(hc2) |
| 290 |
expect_error(Bk(dend1, dend2, warn = T)) |
| 291 |
|
| 292 |
# make computationally easier for permutations |
| 293 |
hc1 <- hclust(dist(datasets::iris[1:15, -5]), "com") |
| 294 |
hc2 <- hclust(dist(datasets::iris[1:15, -5]), "single") |
| 295 |
dend1 <- as.dendrogram(hc1) |
| 296 |
dend2 <- as.dendrogram(hc2) |
| 297 |
|
| 298 |
# test case where k is not specified |
| 299 |
expect_no_error( |
| 300 |
Bk_permutations(dend1, dend2) |
| 301 |
) |
| 302 |
|
| 303 |
# test case where different number of items are provided |
| 304 |
hc1 <- hclust(dist(datasets::iris[1:29, -5]), "com") |
| 305 |
hc2 <- hclust(dist(datasets::iris[1:30, -5]), "single") |
| 306 |
dend1 <- as.dendrogram(hc1) |
| 307 |
dend2 <- as.dendrogram(hc2) |
| 308 |
expect_error(Bk_permutations(dend1, dend2, warn = T)) |
| 309 |
|
| 310 |
# test case where different labels are provided |
| 311 |
hc1 <- hclust(dist(datasets::iris[1:29, -5]), "com") |
| 312 |
hc2 <- hclust(dist(datasets::iris[2:30, -5]), "single") |
| 313 |
dend1 <- as.dendrogram(hc1) |
| 314 |
dend2 <- as.dendrogram(hc2) |
| 315 |
expect_error(Bk_permutations(dend1, dend2, warn = T)) |
| 316 |
}) |
| 317 |
|
| 318 |
|
| 319 |
test_that("Bk_plot works", { |
| 320 |
|
| 321 |
ss <- TRUE |
| 322 |
hc1 <- hclust(dist(datasets::iris[1:15, -5]), "com") |
| 323 |
hc2 <- hclust(dist(datasets::iris[1:15, -5]), "single") |
| 324 |
dend1 <- as.dendrogram(hc1) |
| 325 |
dend2 <- as.dendrogram(hc2) |
| 326 |
|
| 327 |
# simply check if the function can run without error |
| 328 |
expect_no_error( |
| 329 |
Bk_plot(dend1, dend2, try_cutree_hclust = T, add_E = T, rejection_line_asymptotic = T, rejection_line_permutation = T, p.adjust.methods = "bonferroni") |
| 330 |
) |
| 331 |
expect_no_error( |
| 332 |
Bk_plot(dend1, dend2, try_cutree_hclust = T, add_E = F, rejection_line_asymptotic = T, rejection_line_permutation = T) |
| 333 |
) |
| 334 |
|
| 335 |
# case where object which cant be converted to phylo is passed in |
| 336 |
x <- matrix(1:4, nrow = 2) |
| 337 |
expect_error( |
| 338 |
Bk_plot(x, dend2, try_cutree_hclust = T, add_E = F, rejection_line_asymptotic = T, rejection_line_permutation = T) |
| 339 |
) |
| 340 |
expect_error( |
| 341 |
Bk_plot(dend1, x, try_cutree_hclust = T, add_E = F, rejection_line_asymptotic = T, rejection_line_permutation = T) |
| 342 |
) |
| 343 |
}) |