Debian Codegraph @

tests/testthat/test-cor_bakers_gamma.R

raw
1 # library(testthat)
2 context("Baker's gamma correlation between two trees")
3
4
5 test_that("lowest_common_branch works", {
6 item1 <- structure(c(1L, 1L, 1L, 1L), .Names = c("1", "2", "3", "4"))
7 item2 <- structure(c(1L, 1L, 2L, 2L), .Names = c("1", "2", "3", "4"))
8 expect_identical(lowest_common_branch(item1, item2), 2)
9 # if no shared items
10 item1 <- structure(c(1L, 1L, 1L, 1L), .Names = c("1", "2", "3", "4"))
11 item2 <- structure(c(2L, 2L, 2L, 2L), .Names = c("1", "2", "3", "4"))
12 expect_identical(lowest_common_branch(item1, item2), 0)
13 })
14
15
16 test_that("cor_bakers_gamma works", {
17 suppressWarnings(RNGversion("3.5.0"))
18 set.seed(23235)
19 ss <- sample(1:150, 10) # we want to compare small trees
20 hc1 <- hclust(dist(datasets::iris[ss, -5]), "com")
21 hc2 <- hclust(dist(datasets::iris[ss, -5]), "single")
22 dend1 <- as.dendrogram(hc1)
23 dend2 <- as.dendrogram(hc2)
24 # cutree(dend1)
25
26 # hclust and dendrogram works:
27 expect_identical(cor_bakers_gamma(hc1, hc2), cor_bakers_gamma(dend1, dend2))
28 expect_identical(round(cor_bakers_gamma(dend1, dend2), 2), 0.57)
29
30 # use_labels_not_values works:
31 expect_identical(
32 cor_bakers_gamma(dend1, dend2, use_labels_not_values = TRUE),
33 cor_bakers_gamma(dend1, dend2, use_labels_not_values = FALSE)
34 )
35
36 # Cor value of a tree with itself is 1:
37 expect_identical(round(cor_bakers_gamma(dend1, dend1), 2), 1)
38
39 # tree order has no effect on the correlation:
40 rev_dend1 <- rev(dend1)
41 expect_identical(round(cor_bakers_gamma(dend1, rev_dend1), 2), 1)
42
43 # But labels order does matter!!
44 dend1_mixed <- dend1
45 labels(dend1_mixed) <- rev(labels(dend1_mixed))
46 # does NOT mean having the worst cor!
47 expect_false(identical(cor_bakers_gamma(dend1, dend1_mixed), 1))
48
49 expect_identical(round(cor_bakers_gamma.default(dend1, dend2), 2), 0.57)
50
51 dend12 <- dendlist(dend1, dend2)
52 expect_identical(round(cor_bakers_gamma.dendlist(dend12), 2), 0.57)
53 })
54
55
56 test_that("bakers_gamma_for_2_k_matrix works", {
57 suppressWarnings(RNGversion("3.5.0"))
58 set.seed(23235)
59 ss <- sample(1:150, 10) # we want to compare small trees
60 hc1 <- hclust(dist(datasets::iris[ss, -5]), "com")
61 hc2 <- hclust(dist(datasets::iris[ss, -5]), "single")
62 dend1 <- as.dendrogram(hc1)
63 dend2 <- as.dendrogram(hc2)
64
65 k_matrix_dend1 <- cutree(dend1, k = 1:nleaves(dend1))
66 k_matrix_dend2 <- cutree(dend2, k = 1:nleaves(dend2))
67
68 expect_equal(
69 bakers_gamma_for_2_k_matrix(k_matrix_dend1, k_matrix_dend1, to_plot = T),
70 1
71 )
72
73 # if COR_object is NA
74 trace("bakers_gamma_for_2_k_matrix", quote(COR_object <- NA), at = 9, print = FALSE)
75 expect_no_error(
76 bakers_gamma_for_2_k_matrix(k_matrix_dend1, k_matrix_dend1)
77 )
78 untrace("bakers_gamma_for_2_k_matrix")
79
80 # if dimension misalignment
81 expect_error(
82 bakers_gamma_for_2_k_matrix(k_matrix_dend1, k_matrix_dend2[-1,])
83 )
84 # if all labels aren't shared
85 capture.output(expect_error(
86 bakers_gamma_for_2_k_matrix(k_matrix_dend1[-2,], k_matrix_dend2[-1,])
87 ))
88
89 })