| 1 |
# library(testthat) |
| 2 |
# library(dendextend) |
| 3 |
|
| 4 |
|
| 5 |
context("cor.dendlist") |
| 6 |
|
| 7 |
|
| 8 |
test_that("cor.dendlist works", { |
| 9 |
suppressWarnings(RNGversion("3.5.0")) |
| 10 |
set.seed(1) |
| 11 |
|
| 12 |
dend1 <- as.dendrogram(hclust(dist(mtcars[1:5, ]), method = "average")) |
| 13 |
dend2 <- as.dendrogram(hclust(dist(mtcars[1:5, ]), method = "ward.D2")) |
| 14 |
dend12 <- dendlist(dend1, dend2) |
| 15 |
|
| 16 |
expect_identical( |
| 17 |
round(cor.dendlist(dend12, "cophenetic"), 3), |
| 18 |
matrix(c(1.000, 0.711, 0.711, 1.000), nrow = 2) |
| 19 |
) |
| 20 |
expect_identical( |
| 21 |
round(cor.dendlist(dend12, "baker"), 3), |
| 22 |
matrix(c(1.000, 0.701, 0.701, 1.000), nrow = 2) |
| 23 |
) |
| 24 |
expect_identical( |
| 25 |
round(cor.dendlist(dend12, "common_nodes"), 3), |
| 26 |
matrix(c(1.000, 0.889, 0.889, 1.000), nrow = 2) |
| 27 |
) |
| 28 |
expect_identical( |
| 29 |
round(cor.dendlist(dend12, "FM_index", k = 2), 3), |
| 30 |
matrix(c(1.000, 0.408, 0.408, 1.000), nrow = 2) |
| 31 |
) |
| 32 |
|
| 33 |
# if non-dendrogram object is passed in |
| 34 |
x <- dist(mtcars) |
| 35 |
expect_error( |
| 36 |
cor.dendlist(x) |
| 37 |
) |
| 38 |
|
| 39 |
}) |
| 40 |
|
| 41 |
|
| 42 |
test_that("cor_FM_index works", { |
| 43 |
suppressWarnings(RNGversion("3.5.0")) |
| 44 |
set.seed(1) |
| 45 |
|
| 46 |
dend1 <- as.dendrogram(hclust(dist(mtcars[1:5, ]), method = "average")) |
| 47 |
dend2 <- as.dendrogram(hclust(dist(mtcars[1:5, ]), method = "ward.D2")) |
| 48 |
|
| 49 |
|
| 50 |
# if k isn't specified |
| 51 |
expect_error( |
| 52 |
cor_FM_index(dend1, dend2) |
| 53 |
) |
| 54 |
# if all leaves part of the same cluster |
| 55 |
expect_warning(with_mocked_bindings( |
| 56 |
cor_FM_index(dend1, dend2, k = 2), |
| 57 |
cutree = function(dend, k, ...) rep(0, length(labels(dend))) |
| 58 |
)) |
| 59 |
|
| 60 |
}) |