From 5e83f05f1b556516786d5d57c24e1e30cb66111a Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 12 May 2024 20:11:25 +0200 Subject: [PATCH] add tests --- tests/testthat/test-chi_squared_test.R | 2 ++ tests/testthat/test-mann_whitney_test.R | 28 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/testthat/test-mann_whitney_test.R diff --git a/tests/testthat/test-chi_squared_test.R b/tests/testthat/test-chi_squared_test.R index 2823e7af..38310348 100644 --- a/tests/testthat/test-chi_squared_test.R +++ b/tests/testthat/test-chi_squared_test.R @@ -1,4 +1,6 @@ skip_if_not_installed("effectsize") +skip_if_not_installed("datawizard") + test_that("chi_squared_test", { data(efc) set.seed(123) diff --git a/tests/testthat/test-mann_whitney_test.R b/tests/testthat/test-mann_whitney_test.R new file mode 100644 index 00000000..96e3f87f --- /dev/null +++ b/tests/testthat/test-mann_whitney_test.R @@ -0,0 +1,28 @@ +skip_if_not_installed("coin") +skip_if_not_installed("survey") +skip_if_not_installed("datawizard") + +test_that("mann_whitney_test", { + data(efc) + set.seed(123) + efc$weight <- abs(rnorm(nrow(efc), 1, 0.3)) + out1 <- mann_whitney_test(efc, "e17age", by = "e16sex") + out2 <- wilcox.test(e17age ~ e16sex, data = efc) + expect_equal(out1$w, out2$statistic, tolerance = 1e-4, ignore_attr = TRUE) + expect_equal(out1$p, out2$p.value, tolerance = 1e-4, ignore_attr = TRUE) + expect_equal(out1$estimate, -1561, tolerance = 1e-4, ignore_attr = TRUE) + expect_equal(out1$r, 0.2571254, tolerance = 1e-4, ignore_attr = TRUE) + + set.seed(123) + wide_data <- data.frame(scale1 = runif(20), scale2 = runif(20)) + out1 <- mann_whitney_test(wide_data, select = c("scale1", "scale2")) + out2 <- wilcox.test(wide_data$scale1, wide_data$scale2) + expect_equal(out1$w, out2$statistic, tolerance = 1e-4, ignore_attr = TRUE) + expect_equal(out1$p, out2$p.value, tolerance = 1e-4, ignore_attr = TRUE) + expect_equal(out1$r, 0.05132394, tolerance = 1e-4, ignore_attr = TRUE) + + out <- mann_whitney_test(efc, "e17age", by = "e16sex", weights = "weight") + expect_equal(out$p, 1.976729e-14, tolerance = 1e-4, ignore_attr = TRUE) + expect_equal(out$estimate, 0.1594972, tolerance = 1e-4, ignore_attr = TRUE) + expect_equal(out$r, 0.2599877, tolerance = 1e-4, ignore_attr = TRUE) +})