context("Clusterability Test and Print") library(clusterability) test_that("clusterabilitytest examples", { # Testing clusterability of normals1, a dataset constructed to be nonclusterable. data(normals1) normals1 <- normals1[, -3] expect_gte(clusterabilitytest(normals1, "dip")$pvalue, 0.05) expect_gte(clusterabilitytest(normals1, "dip", distance_standardize = "NONE", reduction = "distance")$pvalue, 0.05) expect_gte(clusterabilitytest(normals1, "silverman", s_setseed = 123)$pvalue, 0.05) expect_gte(clusterabilitytest(normals1, "silverman", distance_standardize = "NONE", reduction = "distance", s_setseed = 123)$pvalue, 0.05) expect_gte(clusterabilitytest(normals1, "dip", reduction = "spca", spca_method = "EN")$pvalue, 0.05) expect_gte(clusterabilitytest(normals1, "dip", reduction = "spca", spca_method = "VP")$pvalue, 0.05) expect_gte(clusterabilitytest(normals1, "silverman", s_setseed = 123, reduction = "spca", spca_method = "EN")$pvalue, 0.05) expect_gte(clusterabilitytest(normals1, "silverman", s_setseed = 123, reduction = "spca", spca_method = "VP")$pvalue, 0.05) # Testing clusterability of normals2 data(normals2) normals2 <- normals2[, -3] expect_lte(clusterabilitytest(normals2, "dip")$pvalue, 0.05) expect_lte(clusterabilitytest(normals2, "dip", reduction = "distance", distance_standardize = "NONE")$pvalue, 0.05) expect_lte(clusterabilitytest(normals2, "silverman", s_setseed = 123)$pvalue, 0.05) expect_lte(clusterabilitytest(normals2, "silverman", reduction = "distance", distance_standardize = "NONE", s_setseed = 123)$pvalue, 0.05) expect_lte(clusterabilitytest(normals2, "dip", reduction = "spca", spca_method = "EN")$pvalue, 0.05) expect_lte(clusterabilitytest(normals2, "dip", reduction = "spca", spca_method = "VP")$pvalue, 0.05) expect_lte(clusterabilitytest(normals2, "silverman", s_setseed = 123, reduction = "spca", spca_method = "EN")$pvalue, 0.05) expect_lte(clusterabilitytest(normals2, "silverman", s_setseed = 123, reduction = "spca", spca_method = "VP")$pvalue, 0.05) # Testing clusterability of normals3 normals3 <- normals3[, -3] expect_no_error(clusterabilitytest(normals3, "dip")) expect_no_error(clusterabilitytest(normals3, "dip", reduction = "distance", distance_standardize = "NONE")) expect_no_error(clusterabilitytest(normals3, "silverman", s_setseed = 123)) expect_no_error(clusterabilitytest(normals3, "silverman", reduction = "distance", distance_standardize = "NONE", s_setseed = 123)) expect_no_error(clusterabilitytest(normals3, "dip", reduction = "spca", spca_method = "EN")) expect_no_error(clusterabilitytest(normals3, "dip", reduction = "spca", spca_method = "VP")) expect_no_error(clusterabilitytest(normals3, "silverman", s_setseed = 123, reduction = "spca", spca_method = "EN")) expect_no_error(clusterabilitytest(normals3, "silverman", s_setseed = 123, reduction = "spca", spca_method = "VP")) # Testing clusterability of normals4 data(normals4) normals4 <- normals4[, -4] expect_no_error(clusterabilitytest(normals4, "dip")) expect_no_error(clusterabilitytest(normals4, "dip", reduction = "distance", distance_standardize = "NONE")) expect_no_error(clusterabilitytest(normals4, "silverman", s_setseed = 123)) expect_no_error(clusterabilitytest(normals4, "silverman", reduction = "distance", distance_standardize = "NONE", s_setseed = 123)) expect_no_error(clusterabilitytest(normals4, "dip", reduction = "spca", spca_method = "EN")) expect_no_error(clusterabilitytest(normals4, "dip", reduction = "spca", spca_method = "VP")) expect_no_error(clusterabilitytest(normals4, "silverman", s_setseed = 123, reduction = "spca", spca_method = "EN")) expect_no_error(clusterabilitytest(normals4, "silverman", s_setseed = 123, reduction = "spca", spca_method = "VP")) # Testing clusterability of normals5 data(normals5) normals5 <- normals5[, -4] expect_lte(clusterabilitytest(normals5, "dip")$pvalue, 0.05) expect_lte(clusterabilitytest(normals5, "dip", reduction = "distance", distance_standardize = "NONE")$pvalue, 0.05) expect_lte(clusterabilitytest(normals5, "silverman", s_setseed = 123, s_digits = 20)$pvalue, 0.05) expect_lte(clusterabilitytest(normals5, "silverman", reduction = "distance", distance_standardize = "NONE", s_setseed = 123)$pvalue, 0.05) expect_lte(clusterabilitytest(normals5, "dip", reduction = "spca", spca_method = "EN")$pvalue, 0.05) expect_lte(clusterabilitytest(normals5, "dip", reduction = "spca", spca_method = "VP")$pvalue, 0.05) expect_lte(clusterabilitytest(normals5, "silverman", s_setseed = 123, reduction = "spca", spca_method = "EN")$pvalue, 0.05) expect_lte(clusterabilitytest(normals5, "silverman", s_setseed = 123, reduction = "spca", spca_method = "VP")$pvalue, 0.05) # Testing clusterability of iris data(iris) newiris <- iris[, c(1:4)] expect_lte(clusterabilitytest(newiris, "dip")$pvalue, 0.05) expect_lte(clusterabilitytest(newiris, "dip", reduction = "distance", distance_standardize = "NONE")$pvalue, 0.05) expect_lte(clusterabilitytest(newiris, "silverman", s_setseed = 123)$pvalue, 0.05) expect_lte(clusterabilitytest(newiris, "silverman", reduction = "distance", distance_standardize = "NONE", s_setseed = 123)$pvalue, 0.05) expect_lte(clusterabilitytest(newiris, "dip", reduction = "spca", spca_method = "EN")$pvalue, 0.05) expect_lte(clusterabilitytest(newiris, "dip", reduction = "spca", spca_method = "VP")$pvalue, 0.05) expect_lte(clusterabilitytest(newiris, "silverman", s_setseed = 123, reduction = "spca", spca_method = "EN")$pvalue, 0.05) expect_lte(clusterabilitytest(newiris, "silverman", s_setseed = 123, reduction = "spca", spca_method = "VP")$pvalue, 0.05) # Testing clusterability of cars data(cars) expect_gte(clusterabilitytest(cars, "dip")$pvalue, 0.05) expect_gte(clusterabilitytest(cars, "dip", reduction = "distance", distance_standardize = "NONE")$pvalue, 0.05) expect_gte(clusterabilitytest(cars, "silverman", s_setseed = 123)$pvalue, 0.05) expect_gte(clusterabilitytest(cars, "silverman", reduction = "distance", distance_standardize = "NONE", s_setseed = 123)$pvalue, 0.05) expect_gte(clusterabilitytest(cars, "dip", reduction = "spca", spca_method = "EN")$pvalue, 0.05) expect_gte(clusterabilitytest(cars, "dip", reduction = "spca", spca_method = "VP")$pvalue, 0.05) expect_gte(clusterabilitytest(cars, "silverman", s_setseed = 123, reduction = "spca", spca_method = "EN")$pvalue, 0.05) expect_gte(clusterabilitytest(cars, "silverman", s_setseed = 123, reduction = "spca", spca_method = "VP")$pvalue, 0.05) }) test_that("mixed data", { mixed_data <- data.frame(rnorm(50), as.factor(rbinom(50, 1, 0.5))) clusterability_result <- clusterabilitytest(mixed_data, test = "dip", reduction = "distance", distance_metric = "gower", distance_standardize = "none") expect_output(print.clusterability(clusterability_result, "gower")) expect_error(clusterabilitytest(mixed_data, test = "silverman"), "non-numeric", info = "Expect error for mixed data without using gower metric + distances") }) test_that("print - errors", { iris1 <- data(iris) iris1 <- iris[, 1:4] cresult <- clusterabilitytest(iris1, test = "Dip", reduction = "PCA") expect_error(print.clusterability(iris1), "clusterability", info = "Expect error for incorrect class types") }) test_that("print - conditionals", { iris1 <- data(iris) iris1 <- iris[, 1:4] miss <- matrix(c(1, 5, 8, NA, 2, 3, 8, 7, 9, 4, 2, 4, 6, 9, 11, 4, 13, 5, 6, 1, 0), ncol = 3) cresult <- clusterabilitytest(iris1, test = "Dip", reduction = "PCA", d_simulatepvalue = TRUE, d_reps = 500) missresult <- clusterabilitytest(miss, test = "Silverman", reduction = "distance", distance_metric = "euclidean", distance_standardize = "STD", s_m = 1422, s_digits = 7, s_setseed = 1234, s_outseed = TRUE, completecase = TRUE) basicdip <- clusterabilitytest(iris1, test = "dip") basicsilv <- clusterabilitytest(iris1, test = "silverman") # Missing or No Missing expect_output(print.clusterability(missresult), "missing at least one") expect_output(print.clusterability(cresult), "is complete") # Data reduction technique expect_output(print.clusterability(cresult), "PCA") expect_output(print.clusterability(missresult), "Distance") expect_output(print.clusterability(missresult), "euclidean") expect_output(print.clusterability(missresult), "STD") # Descriptions of data reduction expect_output(print.clusterability(missresult), "has mean 0") # Test name expect_output(print.clusterability(cresult), "Dip Test") expect_output(print.clusterability(missresult), "Silverman") # Null and Alternative expect_output(print.clusterability(cresult), "Null Hypothesis") expect_output(print.clusterability(cresult), "Alternative Hypothesis") expect_output(print.clusterability(cresult), "p-value") expect_output(print.clusterability(cresult), "Dip statistic") expect_output(print.clusterability(missresult), "Null Hypothesis") expect_output(print.clusterability(missresult), "Alternative Hypothesis") expect_output(print.clusterability(missresult), "p-value") expect_output(print.clusterability(missresult), "Critical bandwidth") # Test options expect_output(print.clusterability(cresult), "Test Options") expect_output(print.clusterability(missresult), "Test Options") # Dip expect_output(print.clusterability(cresult), "Monte Carlo") expect_output(print.clusterability(cresult), "500") expect_output(print.clusterability(basicdip), "Default") # Silverman expect_output(print.clusterability(missresult), "Seed set") expect_output(print.clusterability(missresult), "1234") expect_output(print.clusterability(missresult), "1422 bootstrap") expect_output(print.clusterability(missresult), "Hall and York") expect_output(print.clusterability(missresult), "7 digits") })