| 1 |
# library(testthat) |
| 2 |
|
| 3 |
|
| 4 |
context("Cophenetic correlation between two trees") |
| 5 |
|
| 6 |
|
| 7 |
test_that("cor_cophenetic works", { |
| 8 |
suppressWarnings(RNGversion("3.5.0")) |
| 9 |
set.seed(23235) |
| 10 |
ss <- sample(1:150, 10) |
| 11 |
hc1 <- iris[ss, -5] %>% |
| 12 |
dist() %>% |
| 13 |
hclust("com") |
| 14 |
hc2 <- iris[ss, -5] %>% |
| 15 |
dist() %>% |
| 16 |
hclust("single") |
| 17 |
dend1 <- as.dendrogram(hc1) |
| 18 |
dend2 <- as.dendrogram(hc2) |
| 19 |
# cutree(dend1) |
| 20 |
|
| 21 |
# cophenetic(hc1) |
| 22 |
# cophenetic(hc2) |
| 23 |
# # notice how the dist matrix for the dendrograms have different orders: |
| 24 |
# cophenetic(dend1) |
| 25 |
# cophenetic(dend2) |
| 26 |
|
| 27 |
# just taking the cor of two cophenetic treess |
| 28 |
# will NOT give the same result for hclust and dendrogram |
| 29 |
# (due to the different order of matrices) |
| 30 |
expect_false( |
| 31 |
cor(cophenetic(hc1), cophenetic(hc2)) == # 0.874 |
| 32 |
cor(cophenetic(dend1), cophenetic(dend2)) # 0.16 |
| 33 |
) |
| 34 |
# the difference is becasue the order of the distance table in the case of |
| 35 |
# stats:::cophenetic.dendrogram will change between dendrograms! |
| 36 |
|
| 37 |
# however, the following numbers are consistant (since I force-sort the rows/columns): |
| 38 |
expect_equal( |
| 39 |
cor_cophenetic(hc1, hc2), |
| 40 |
cor_cophenetic(dend1, dend2) |
| 41 |
) |
| 42 |
|
| 43 |
# Let's see that dendlist works |
| 44 |
expect_equal( |
| 45 |
cor_cophenetic(dendlist(dend1, dend2)), |
| 46 |
cor_cophenetic(dend1, dend2) |
| 47 |
) |
| 48 |
}) |