Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed May 12, 2024
1 parent c00243d commit 4b4e2fe
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
49 changes: 49 additions & 0 deletions tests/testthat/test-kruskal_wallis_test.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
skip_if_not_installed("survey")
skip_if_not_installed("datawizard")

test_that("kruskal_wallis_test", {
#' data(efc)
#' # Kruskal-Wallis test for elder's age by education
#' kruskal_wallis_test(efc, "e17age", by = "c172code")
#'
#' # when data is in wide-format, specify all relevant continuous
#' # variables in `select` and omit `by`
#' set.seed(123)
#' wide_data <- data.frame(
#' scale1 = runif(20),
#' scale2 = runif(20),
#' scale3 = runif(20)
#' )
#' kruskal_wallis_test(wide_data, select = c("scale1", "scale2", "scale3"))
#'
#' # same as if we had data in long format, with grouping variable
#' long_data <- data.frame(
#' scales = c(wide_data$scale1, wide_data$scale2, wide_data$scale3),
#' groups = rep(c("A", "B", "C"), each = 20)
#' )
#' kruskal_wallis_test(long_data, select = "scales", by = "groups")
#' # base R equivalent
#' kruskal.test(scales ~ groups, data = long_data)
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)
})
8 changes: 4 additions & 4 deletions tests/testthat/test-mann_whitney_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ test_that("mann_whitney_test", {

test_that("mann_whitney_test, sanity checks", {
data(efc)
expect_errpr(mann_whitney_test(efc, "e17age", by = "c172code"), regex = "Only two groups are")
expect_errpr(mann_whitney_test(efc, c("e17age", "c172code", "e16sex")), regex = "You may only specify")
expect_errpr(mann_whitney_test(efc, c("e17age", "c172code"), by = "e17age"), regex = "If `select` specifies more")
expect_errpr(mann_whitney_test(efc, "e17age"), regex = "Only one variable provided")
expect_error(mann_whitney_test(efc, "e17age", by = "c172code"), regex = "Only two groups are")
expect_error(mann_whitney_test(efc, c("e17age", "c172code", "e16sex")), regex = "You may only specify")
expect_error(mann_whitney_test(efc, c("e17age", "c172code"), by = "e17age"), regex = "If `select` specifies more")
expect_error(mann_whitney_test(efc, "e17age"), regex = "Only one variable provided")
})

0 comments on commit 4b4e2fe

Please sign in to comment.