1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
|
# library(testthat)
# library(dendextend)
# set this for testing
dendextend_options("warn", TRUE)
context("Cutting a dendrogram")
test_that("Checking if a number is natural", {
expect_true(is.natural.number(1)) # is TRUE
expect_true(all(is.natural.number(seq(1, 5, by = 1))))
expect_false(all(is.natural.number(seq(0, 5, by = 1))))
expect_false(all(is.natural.number(seq(-1, 5, by = 0.5))))
expect_error(is.natural.number("a"))
})
test_that("Turning factor into an integer", {
x <- factor(2:4)
expect_equal(as.numeric(x), 1:3)
names(x) <- letters[x]
expect_equal(as.numeric(x), 1:3)
# dput(fac2num(x))
expect_identical(
fac2num(x),
structure(c(2, 3, 4), .Names = c("a", "b", "c"))
)
expect_identical(fac2num(x, keep_names = FALSE), c(2, 3, 4))
})
test_that("cutree a dendrogram by height h", {
# data
hc <- hclust(dist(USArrests[c(1, 6, 13, 20, 23), ]), "ave")
dend <- as.dendrogram(hc)
# we need h!
expect_error(cutree_1h.dendrogram(dend))
# the same as cutree
expect_identical(
cutree_1h.dendrogram(dend, h = 100),
cutree(hc, h = 100)
)
expect_identical(
cutree_1h.dendrogram(dend, h = 30),
cutree(hc, h = 30)
)
# the same as cutree - also when there are NO clusters
expect_identical(
cutree_1h.dendrogram(dend, h = 1000),
cutree(hc, h = 1000)
)
# the same as cutree - also when there are NO clusters
expect_identical(
cutree_1h.dendrogram(dend, h = 0),
cutree(hc, h = 0)
)
expect_identical(
cutree_1h.dendrogram(dend, h = 1),
cutree(hc, h = 1)
)
# get return in the order of the dendrogram:
expect_identical(
names(cutree_1h.dendrogram(dend, 100, order_clusters_as_data = FALSE)),
labels(dend)
)
# dealing with cutree_1h.dendrogram in negative h!
expect_identical(
cutree_1h.dendrogram(dend, h = -1),
stats::cutree(as.hclust(dend), h = -1)
)
expect_identical(
stats::cutree(as.hclust(dend), k = 5),
stats::cutree(as.hclust(dend), h = -1)
)
})
test_that("get dendrogram heights for k clusters", {
# data
hc <- hclust(dist(USArrests[c(1, 6, 13, 20, 23), ]), "ave")
dend <- as.dendrogram(hc)
unbranch_dend <- unbranch(dend, 2)
# plot(unbranch_dend)
dend_heights <- heights_per_k.dendrogram(dend)
unbranch_dend_heights <- heights_per_k.dendrogram(unbranch_dend)
# cutree_1h.dendrogram(dend, h=dend_heights[[3]])
expect_equal(length(dend_heights), 5)
expect_equal(length(unbranch_dend_heights), 4)
expect_equal(nnodes(unbranch_dend), 8)
# dput(names(unbranch_dend_heights))
expect_equal(names(unbranch_dend_heights), c("1", "3", "4", "5"))
})
test_that("cutree a dendrogram to k clusters", {
# data
hc <- hclust(dist(USArrests[c(1, 6, 13, 20, 23), ]), "ave")
dend <- as.dendrogram(hc)
unbranch_dend <- unbranch(dend, 2)
# plot(unbranch_dend)
# the same as cutree
expect_identical(
cutree_1k.dendrogram(dend, k = 3),
cutree(hc, k = 3)
)
expect_identical(
cutree_1k.dendrogram(dend, k = 1),
cutree(hc, k = 1)
)
# the same as cutree - also when there are NO clusters
expect_identical(
cutree_1k.dendrogram(dend, k = 5),
cutree(hc, k = 5)
)
# if ignoring the "names" on the vector - the numbers will be identical:
expect_identical(
unname(cutree_1k.dendrogram(dend, k = 3, use_labels_not_values = FALSE)),
unname(cutree(hc, k = 3))
)
# errors:
expect_error(cutree_1k.dendrogram(dend)) # we need h!
expect_error(cutree_1k.dendrogram(dend, k = -1))
expect_error(cutree_1k.dendrogram(dend, k = 0))
# expect_error( cutree_1k.dendrogram(dend, k = 1.5)) # I no longer expect an error since it is turned into "as.integer"
expect_error(cutree_1k.dendrogram(dend, k = 50))
expect_error(cutree(hc, k = 50))
# get return in the order of the dendrogram:
expect_identical(
names(cutree_1k.dendrogram(dend, k = 3, order_clusters_as_data = FALSE)),
labels(dend)
)
# cases of no possible k's:
expect_warning(cutree_1k.dendrogram(unbranch_dend, 2, warn = TRUE))
expect_equal(cutree_1k.dendrogram(unbranch_dend, 2, warn = FALSE), rep(NA, 5))
})
test_that("cutree dendrogram method works for k", {
# data
hc <- USArrests[c(1, 6, 13, 20, 23), ] %>%
dist() %>%
hclust("ave")
dend <- as.dendrogram(hc)
unbranch_dend <- unbranch(dend, 2)
# plot(unbranch_dend)
# the same as cutree
expect_identical(
cutree(dend, k = 3),
cutree(hc, k = 3)
)
expect_identical(
cutree(dend, k = 1),
cutree(hc, k = 1)
)
# the same as cutree - also when there are NO clusters
expect_identical(
cutree(dend, k = 5),
cutree(hc, k = 5)
)
# if ignoring the "names" on the vector - the numbers will be identical:
expect_identical(
unname(cutree(dend, k = 3, use_labels_not_values = FALSE)),
unname(cutree(hc, k = 3))
)
# use_labels_not_values doesn't harm cutree
expect_identical(
cutree(dend, k = 3, use_labels_not_values = TRUE),
cutree(dend, k = 3, use_labels_not_values = FALSE)
)
# use_labels_not_values doesn't harm cutree also when try_cutree_hclust=FALSE
expect_identical(
unname(cutree(dend, k = 3, use_labels_not_values = TRUE, try_cutree_hclust = FALSE)),
unname(cutree(dend, k = 3, use_labels_not_values = FALSE, try_cutree_hclust = FALSE))
)
# errors:
expect_error(cutree(dend)) # we need h!
expect_error(cutree(dend, k = -1))
expect_error(cutree(dend, k = 0))
# expect_error( cutree(dend, k = 1.5)) # I no longer expect an error since it is turned into "as.integer"
expect_error(cutree(dend, k = 50))
expect_error(cutree(hc, k = 50))
# get return in the order of the dendrogram:
expect_identical(
names(cutree(dend, k = 3, order_clusters_as_data = FALSE, try_cutree_hclust = FALSE)),
labels(dend)
)
expect_identical(
names(cutree(dend, k = 3, order_clusters_as_data = FALSE, try_cutree_hclust = TRUE)),
labels(dend)
)
# cases of no possible k's:
expect_warning(cutree(unbranch_dend, 2))
expect_equal(
suppressWarnings(cutree(unbranch_dend, 2, warn = FALSE)),
rep(0, 5)
)
# now to check vectorization
})
test_that("cutree for flat edges", {
# cutree(hclust(dist(c(1,1,1,2,2))), k=5)
# cutree(hclust(dist(c(1,1,1,2,2))), k=1:5)
dend <- as.dendrogram(hclust(dist(c(1, 1, 1, 2, 2))))
# dendextend:::cutree.dendrogram(dend, k=5)
# as.hclust(dend) # Error: all(vapply(s, is.integer, NA)) is not TRUE
# cutree(dend,k=5)
# plot(dend)
# dendextend_cut_lower_fun(dend, -.5, labels)
# cut_lower_fun(dend, -.5, labels)
expect_equal(unname(cutree(dend, k = 2)), c(1, 1, 1, 2, 2))
expect_equal(unname(cutree(dend, h = -1)), 1:5) # weird definition
expect_equal(unname(cutree(dend, k = 5)), 1:5)
expect_warning(cutree(dend, k = 4, try_cutree_hclust = FALSE))
expect_equal(suppressWarnings(cutree(dend, k = 4, try_cutree_hclust = FALSE)), rep(0, 5))
# as of R R 3.2.4 (or 3.3.0 -not sure ) - as.hclust was fixed to deal better with ties on the branch heights.
# That means that:
# cutree(as.hclust(dend), k=4)
# would work (it will give hard-to-interpret results - but it would work)
# as.hclust(dend)
})
test_that("cutree for dendrogram works (k,h and vectorization)", {
# data
hc <- hclust(dist(USArrests[c(1, 6, 13, 20, 23), ]), "ave")
dend <- as.dendrogram(hc)
unbranch_dend <- unbranch(dend, 2)
# plot(unbranch_dend)
# the same as cutree
expect_identical(
cutree(dend, k = 1:4),
cutree(hc, k = 1:4)
)
expect_identical(
cutree(dend, h = c(20, 25.5, 50, 170)),
cutree(hc, h = c(20, 25.5, 50, 170))
)
expect_warning(cutree(unbranch_dend, k = 1:2))
# it still works for missing k's, it just returns NA's in the second column
cutree_unbranch_dend <- suppressWarnings(cutree(unbranch_dend, k = 1:4, warn = FALSE))
expect_true(is.matrix(cutree_unbranch_dend))
expect_true(all(cutree_unbranch_dend[, 2] == 0)) # 2nd column is NA.
cutree_unbranch_dend_2 <- suppressWarnings(cutree(unbranch_dend,
k = 1:4,
warn = FALSE, order_clusters_as_data = FALSE,
try_cutree_hclust = FALSE
))
expect_identical(rownames(cutree_unbranch_dend_2), labels(unbranch_dend))
})
# test_that("Making cutted clusters be numbered from left to right",{
test_that("Testing sort_levels_values works", {
# the function can return the same object:
x <- 1:4
names(x) <- letters[x]
attr(x, "keep_me") <- "a cat"
expect_equal(x, sort_levels_values(x))
expect_identical(names(x), names(sort_levels_values(x)))
expect_identical(attributes(x), attributes(sort_levels_values(x)))
x <- c(4:1)
names(x) <- letters[x]
attr(x, "keep_me") <- "a cat"
# it will keep the attributes as they are:
expect_identical(attributes(x), attributes(sort_levels_values(x)))
expect_equivalent(sort(x), sort_levels_values(x)) # not equal since "sort" removes the attr!
x <- c(4:1, 4, 2)
# dput(sort_levels_values(x)) # 1 2 3 4 1 3
expect_identical(sort_levels_values(x), c(1, 2, 3, 4, 1, 3))
x <- c(2, 2, 3, 2, 1)
expect_identical(sort_levels_values(x), c(1, 1, 2, 1, 3))
# works when used on matrices:
x <- matrix(1:16, 4, 4)
rownames(x) <- letters[1:4]
# x
expect_equal(x, apply(x, 2, sort_levels_values))
x <- matrix(4:1, 2, 2)
rownames(x) <- letters[1:2]
# x
# dput(apply(x, 2, sort_levels_values))
expect_identical(
apply(x, 2, sort_levels_values),
structure(c(3, 4, 1, 2), .Dim = c(2L, 2L), .Dimnames = list(c(
"a",
"b"
), NULL))
)
# checking that sort_levels_values can be used on a matrix!
x <- matrix(4:1, 2, 2)
rownames(x) <- letters[1:2]
# x
# dput(apply(x, 2, sort_levels_values))
expect_identical(apply(x, 2, sort_levels_values), sort_levels_values(x))
# Yay!
})
test_that("Making cutted clusters be numbered from left to right", {
hc <- hclust(dist(USArrests[c(1, 6, 13, 20, 23), ]), "ave")
dend <- as.dendrogram(hc)
sorted_cutree_hc_orig <- stats::cutree(hc, k = 1:4)
sorted_cutree_hc <- dendextend:::cutree.hclust(hc, k = 1:4)
sorted_cutree_dend <- dendextend:::cutree.dendrogram(dend, k = 1:4, try_cutree_hclust = FALSE)
expect_identical(
sorted_cutree_hc_orig,
sorted_cutree_hc
)
expect_identical(
mode(sorted_cutree_hc),
mode(sorted_cutree_dend)
)
expect_identical(
attributes(sorted_cutree_hc),
attributes(sorted_cutree_dend)
)
# the same as cutree
expect_identical(
as.integer(cutree(dend, k = 1:4, try_cutree_hclust = FALSE)),
as.integer(cutree(hc, k = 1:4))
)
expect_identical(
as.vector(sorted_cutree_hc),
as.vector(sorted_cutree_dend)
) # this is identical since we are forcing the numbers to be integers!
expect_identical(
(sorted_cutree_hc),
(sorted_cutree_dend)
) # this is identical since we are forcing the numbers to be integers!
})
test_that("Compare labels which are character vs integer", {
iris <- datasets::iris
# they seem to be identical - but they are not in the way the are coerced!
expect_identical(
iris[1:150, -5],
iris[, -5]
)
# once they are coerced into a matrix - they are NOT identical!
# the rownames are now NULL!
expect_false(identical(
as.matrix(iris[1:150, -5]),
as.matrix(iris[, -5])
))
expect_false(identical(
attributes(as.matrix(iris[1:150, -5])),
attributes(as.matrix(iris[, -5]))
))
expect_false(identical(
rownames(as.matrix(iris[1:150, -5])),
rownames(as.matrix(iris[, -5]))
))
# it now has no rownames!
expect_true(is.null(rownames(as.matrix(iris[, -5]))))
# what about their dist - not the same!:
expect_false(identical(
dist(iris[1:150, -5]),
dist(iris[, -5])
))
# the first one has "labels" and the second one doesn't
expect_false(identical(
attributes(dist(iris[1:150, -5])),
attributes(dist(iris[, -5]))
))
d_iris <- dist(iris[, -5])
hc_iris <- hclust(d_iris)
dend_iris <- as.dendrogram(hc_iris)
expect_true(is.integer(labels(dend_iris))) # this is a source of BUGS!
expect_false(is.character(labels(dend_iris)))
})
test_that("heights_per_k.dendrogram", {
dend15 <- c(1:5) %>%
dist() %>%
hclust(method = "average") %>%
as.dendrogram()
tmp <- heights_per_k.dendrogram(dend15)
tmp_should_be <- structure(c(2.75, 2.25, 1.25, 0.75), .Names = c("1", "2", "3", "5"))
expect_equal(tmp, tmp_should_be)
dend15 <- c(1:5) %>%
dist() %>%
hclust(method = "sin") %>%
as.dendrogram()
# dput(tmp)
expect_warning(tmp <- heights_per_k.dendrogram(dend15))
tmp_should_be <- structure(c(Inf, -Inf), .Names = c("1", "5"))
expect_equal(tmp, tmp_should_be)
})
# library(stats)
# library(dendextendRcpp)
#
# test_that("Having cutree work when using a subsetted tree",{
# # Wo
# # get a dendrogram:
# # data(iris)
# d_iris <- dist(datasets::iris[1:10,-5])
# hc_iris <- hclust(d_iris)
# dend_iris <- as.dendrogram(hc_iris) # as.hclust.dendrogram - of course
#
# # taking a subset of the dendrogram:
# sub_dend_iris <- dend_iris[[1]]
# hc_sub_dend_iris <- as.hclust(sub_dend_iris)
# # We will have NA's:
# expect_true(any(is.na(stats:::labels.dendrogram(as.dendrogram(hc_sub_dend_iris )))))
#
#
# #if(require(dendextendRcpp)) {
# if("package:dendextendRcpp" %in% search()) {
#
# # notice that for Rcpp this would be false since the returned vector
# # has "NA" characters instead of NA:
# expect_false(any(is.na(dendextendRcpp::labels.dendrogram(as.dendrogram(hc_sub_dend_iris )))))
# # e.g: "NA" "3" "NA" "NA" "4" "7"
# # a[which(a == "NA")] <- NA # this is NOT a good idea, in the case we have a label with "NA" as a character.
# }
#
#
# # we will get warnings, but the functions would not collapse!
# expect_warning(
# dendextend:::cutree.dendrogram(as.dendrogram(hc_sub_dend_iris ), 3, try_cutree_hclust = TRUE)
# )
# expect_warning(
# dendextend:::cutree.dendrogram(as.dendrogram(hc_sub_dend_iris ), 3, try_cutree_hclust = FALSE)
# )
#
# # remove "iris" from the last test...
# # if(exists("iris")) # it says it doesn't exists - but it does (in the gloval env)!
# # suppressWarnings()
# # rm(iris, pos = 1)
#
# })
#
#
dendextend_options("warn", FALSE)
|