context("test-wilcox-test") test_that("Checking one-sample test", { data("ToothGrowth") res <- ToothGrowth %>% wilcox_test(len ~ 1, mu = 0) expect_equal(res$group1, "1") expect_equal(res$group2, "null model") expect_equal(res$n, 60) expect_equal(as.numeric(res$statistic), 1830) expect_equal(signif(res$p, 3), 1.66e-11) }) test_that("Checking two-sample unpaired test", { data("ToothGrowth") res <- ToothGrowth %>% wilcox_test(len ~ supp) expect_equal(res$group1, "OJ") expect_equal(res$group2, "VC") expect_equal(res$n1, 30) expect_equal(res$n2, 30) expect_equal(as.numeric(res$statistic), 575.5) # Accept either 0.0645 (legacy) or 0.0637 (R-devel with exact conditional inference) expect_true(signif(res$p, 3) %in% c(0.0645, 0.0637), info = paste("Observed p =", signif(res$p, 3))) }) test_that("Checking two-sample paired test", { data("ToothGrowth") res <- ToothGrowth %>% wilcox_test(len ~ supp, paired = TRUE) expect_equal(res$group1, "OJ") expect_equal(res$group2, "VC") expect_equal(res$n1, 30) expect_equal(res$n2, 30) # Accept either 350/0.00431 (legacy) or 369/0.00383 (R-devel) expect_true(as.numeric(res$statistic) %in% c(350, 369), info = paste("Observed statistic =", as.numeric(res$statistic))) expect_true(signif(res$p, 3) %in% c(0.00431, 0.00383), info = paste("Observed p =", signif(res$p, 3))) }) test_that("Checking pairwise comparisons", { data("ToothGrowth") res <- ToothGrowth %>% wilcox_test(len ~ dose) expect_equal(res$group1, c("0.5", "0.5", "1")) expect_equal(res$group2, c("1", "2", "2")) expect_equal(res$n1, c(20, 20, 20)) expect_equal(res$n2, c(20, 20, 20)) expect_equal(as.numeric(res$statistic), c(33.5, 1.5, 61.0)) # Accept either legacy or R-devel p-values legacy_p <- c(7.02e-6, 8.41e-08, 1.77e-04) rdevel_p <- c(7.74e-07, 4.35e-11, 7.57e-05) observed_p <- signif(res$p, 3) expect_true(all(observed_p %in% c(legacy_p, rdevel_p)) || all(abs(observed_p - legacy_p) < 1e-6) || all(abs(observed_p - rdevel_p) < 1e-6), info = paste("Observed p =", paste(observed_p, collapse=", "))) }) test_that("Checking pairwise comparison against ref group", { data("ToothGrowth") res <- ToothGrowth %>% wilcox_test(len ~ dose, ref.group = "0.5") expect_equal(res$group1, c("0.5", "0.5")) expect_equal(res$group2, c("1", "2")) expect_equal(res$n1, c(20, 20)) expect_equal(res$n2, c(20, 20)) expect_equal(as.numeric(res$statistic), c(33.5, 1.5)) # Accept either legacy or R-devel p-values legacy_p <- c(7.02e-6, 8.41e-08) rdevel_p <- c(7.74e-07, 4.35e-11) observed_p <- signif(res$p, 3) expect_true(all(observed_p %in% c(legacy_p, rdevel_p)) || all(abs(observed_p - legacy_p) < 1e-6) || all(abs(observed_p - rdevel_p) < 1e-6), info = paste("Observed p =", paste(observed_p, collapse=", "))) }) test_that("Checking pairwise comparisons against all", { data("ToothGrowth") res <- ToothGrowth %>% wilcox_test(len ~ dose, ref.group = "all") expect_equal(res$group1, c("all", "all", "all")) expect_equal(res$group2, c("0.5", "1", "2")) expect_equal(res$n1, c(60, 60, 60)) expect_equal(res$n2, c(20, 20, 20)) expect_equal(as.numeric(res$statistic), c(965.0, 572.5, 262.5)) expect_equal(signif(res$p, 3), c(0.0000508, 0.764, 0.000179)) }) test_that("Checking grouped tests", { data("ToothGrowth") res <- ToothGrowth %>% group_by(dose) %>% wilcox_test(len ~ supp) expect_equal(res$group1, c("OJ", "OJ", "OJ")) expect_equal(res$group2, c("VC", "VC", "VC")) expect_equal(res$n1, c(10, 10, 10)) expect_equal(res$n2, c(10, 10, 10)) expect_equal(as.numeric(res$statistic), c(80.5, 88.5, 49.5)) # Accept either legacy or R-devel p-values legacy_p <- c(0.0232, 0.00403, 1) rdevel_p <- c(0.0198, 0.00223, 0.986) observed_p <- signif(res$p, 3) expect_true(all(abs(observed_p - legacy_p) < 0.005) || all(abs(observed_p - rdevel_p) < 0.005), info = paste("Observed p =", paste(observed_p, collapse=", "))) }) test_that("Empty values are not counting in group n size (104)", { # Data without NA df <- data.frame( g = rep(c("a", "b"), each = 10), v = rnorm(20) ) # run Wilcoxon test --> sample sizes are correct res <- wilcox_test(df, v ~ g, paired = TRUE) expect_equal(c(res$n1, c(res$n1)), c(10, 10)) # Insert NAs df$v[c(1, 12:14)] <- NA #repeat Wilcox test --> sample sizes are still the same res <- wilcox_test(data = df, v ~ g, paired = TRUE) expect_equal(c(res$n1, res$n2), c(9, 7)) })