| 1 |
# library(testthat) |
| 2 |
# library(dendextend) |
| 3 |
|
| 4 |
|
| 5 |
context("find_k") |
| 6 |
|
| 7 |
|
| 8 |
test_that("dudahart2 works", { |
| 9 |
suppressWarnings(RNGversion("3.5.0")) |
| 10 |
set.seed(98765) |
| 11 |
|
| 12 |
iriss <- iris[sample(150,20),-5] |
| 13 |
km <- kmeans(iriss,2) |
| 14 |
p <- dudahart2(iriss,km$cluster)$p.value |
| 15 |
expect_identical(signif(p, 3), 2.38e-06) |
| 16 |
|
| 17 |
p <- dudahart2(iriss,rep(1, length(km$cluster)))$p.value |
| 18 |
expect_identical(signif(p, 3), 0.87) |
| 19 |
}) |
| 20 |
|
| 21 |
|
| 22 |
test_that("calinhara works", { |
| 23 |
suppressWarnings(RNGversion("3.5.0")) |
| 24 |
set.seed(98765) |
| 25 |
|
| 26 |
iriss <- iris[sample(150,20),-5] |
| 27 |
km <- kmeans(iriss,3) |
| 28 |
result <- round(calinhara(iriss,km$cluster),digits=2) |
| 29 |
expect_identical(result, 63.34) |
| 30 |
|
| 31 |
set.seed(1) |
| 32 |
result <- round(calinhara(iriss,rep(1, length(km$cluster)),cn=2),digits=2) |
| 33 |
expect_identical(result, 0) |
| 34 |
}) |
| 35 |
|
| 36 |
|
| 37 |
test_that("distcritmulti works", { |
| 38 |
suppressWarnings(RNGversion("3.5.0")) |
| 39 |
set.seed(20000) |
| 40 |
|
| 41 |
options(digits=3) |
| 42 |
face <- fpc::rFace(50,dMoNo=2,dNoEy=0,p=2) |
| 43 |
clustering <- as.integer(attr(face,"grouping")) |
| 44 |
|
| 45 |
capture.output( |
| 46 |
result <- distcritmulti(face,clustering,ns=3,seed=100000,count=T,criterion="asw") |
| 47 |
) |
| 48 |
expect_equal(result$subsets[[1]], c(34, 24, 32, 45, 5, 22, 8, 18, 41, 4, 30, 11, 13, 1, 27, 40)) |
| 49 |
|
| 50 |
result <- distcritmulti(face,clustering,ns=3,seed=100000,fun="daisy",criterion="pearsongamma") |
| 51 |
expect_equal(result$subsets[[1]], c(34, 24, 32, 45, 5, 22, 8, 18, 41, 4, 30, 11, 13, 1, 27, 40)) |
| 52 |
}) |
| 53 |
|
| 54 |
|
| 55 |
test_that("cluster.stats works", { |
| 56 |
suppressWarnings(RNGversion("3.5.0")) |
| 57 |
set.seed(20000) |
| 58 |
if (!exists("silhouette")) silhouette <<- cluster::silhouette |
| 59 |
|
| 60 |
face <- fpc::rFace(200,dMoNo=2,dNoEy=0,p=2) |
| 61 |
dface <- dist(face) |
| 62 |
complete3 <- cutree(hclust(dface),3) |
| 63 |
|
| 64 |
result <- cluster.stats(dface,complete3,alt.clustering=as.integer(attr(face,"grouping")),noisecluster=T,silhouette=T,G2=T,G3=T,wgap=T,sepindex=T) |
| 65 |
expect_equal( |
| 66 |
round(result$entropy, 3), |
| 67 |
0.702 |
| 68 |
) |
| 69 |
result <- cluster.stats(dface,complete3,alt.clustering=as.integer(attr(face,"grouping")),noisecluster=F,silhouette=T,aggregateonly=T) |
| 70 |
expect_equal( |
| 71 |
round(result$entropy, 3), |
| 72 |
0.702 |
| 73 |
) |
| 74 |
result <- cluster.stats(dface,complete3,alt.clustering=as.integer(attr(face,"grouping")),noisecluster=F,silhouette=T) |
| 75 |
expect_equal( |
| 76 |
round(result$entropy, 3), |
| 77 |
0.702 |
| 78 |
) |
| 79 |
result <- cluster.stats(dface,complete3,alt.clustering=as.integer(attr(face,"grouping")),noisecluster=T,sepwithnoise=F) |
| 80 |
expect_equal( |
| 81 |
round(result$entropy, 3), |
| 82 |
0.702 |
| 83 |
) |
| 84 |
|
| 85 |
# if number of clusters != index of clusters in clustering parameter |
| 86 |
expect_warning( |
| 87 |
cluster.stats(dface,complete3-1,alt.clustering=as.integer(attr(face,"grouping"))-1,compareonly=T) |
| 88 |
) |
| 89 |
|
| 90 |
}) |
| 91 |
|
| 92 |
|
| 93 |
test_that("pamk works", { |
| 94 |
suppressWarnings(RNGversion("3.5.0")) |
| 95 |
set.seed(20000) |
| 96 |
|
| 97 |
face <- fpc::rFace(50,dMoNo=2,dNoEy=0,p=2) |
| 98 |
|
| 99 |
# setting diss = T raises warnings |
| 100 |
suppressWarnings(capture.output( |
| 101 |
pk <- pamk(face,krange=1:5,criterion="asw",critout=TRUE,diss=T) |
| 102 |
)) |
| 103 |
expect_equal( |
| 104 |
round(pk$crit, 3), c(0.000, 0.972, 0.913, 0.877, 0.877) |
| 105 |
) |
| 106 |
|
| 107 |
pk <- pamk(face,krange=1:5,criterion="asw",scaling=T,usepam=F,diss=F) |
| 108 |
expect_equal( |
| 109 |
round(pk$crit, 3), c(0.069, 0.529, 0.614, 0.404, 0.420) |
| 110 |
) |
| 111 |
|
| 112 |
}) |
| 113 |
|
| 114 |
|
| 115 |
test_that("find_k works", { |
| 116 |
suppressWarnings(RNGversion("3.5.0")) |
| 117 |
set.seed(1) |
| 118 |
|
| 119 |
hc <- USArrests[1:10, ] %>% |
| 120 |
dist() %>% |
| 121 |
hclust() |
| 122 |
dend <- as.dendrogram(hc) |
| 123 |
|
| 124 |
k <- find_k(dend)$k |
| 125 |
expect_equal(k, 3) |
| 126 |
|
| 127 |
}) |
| 128 |
|
| 129 |
|
| 130 |
test_that("plot.find_k works", { |
| 131 |
suppressWarnings(RNGversion("3.5.0")) |
| 132 |
set.seed(1) |
| 133 |
|
| 134 |
hc <- USArrests[1:10, ] %>% |
| 135 |
dist() %>% |
| 136 |
hclust() |
| 137 |
dend <- as.dendrogram(hc) |
| 138 |
|
| 139 |
x <- find_k(dend) |
| 140 |
expect_no_error( |
| 141 |
plot.find_k(x) |
| 142 |
) |
| 143 |
}) |