From 7f4ddc0c9cb5a744dc06e38aa5269fa37880bb4a Mon Sep 17 00:00:00 2001 From: Josh Yamamoto Date: Wed, 27 Mar 2024 15:55:32 -0500 Subject: [PATCH] new test and bug fix for samp_by_grp --- R/utils.R | 4 +++- tests/testthat/test-saeczi.R | 16 +++++++++------- tests/testthat/test-samp_by_grp.R | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 tests/testthat/test-samp_by_grp.R diff --git a/R/utils.R b/R/utils.R index 5f76a19..9f72a5b 100644 --- a/R/utils.R +++ b/R/utils.R @@ -32,6 +32,8 @@ samp_by_grp <- function(samp, pop, dom_nm, B) { dplyr::mutate(map_args = list(list(n.x, n.y, add_to))) all_samps <- vector("list", length = B) + ord <- rep(setup[[dom_nm]], times = setup$n.x) + pop_ordered <- pop[match(ord, pop[[dom_nm]]), ] for (i in 1:B) { ids <- setup |> @@ -41,7 +43,7 @@ samp_by_grp <- function(samp, pop, dom_nm, B) { dplyr::pull(samps) |> unlist() - out <- pop[ids, ] + out <- pop_ordered[ids, ] all_samps[[i]] <- out } diff --git a/tests/testthat/test-saeczi.R b/tests/testthat/test-saeczi.R index 36272f1..784f609 100644 --- a/tests/testthat/test-saeczi.R +++ b/tests/testthat/test-saeczi.R @@ -8,13 +8,15 @@ samp <- samp |> set.seed(5) -result <- saeczi(samp, - pop, - lin_formula, - domain_level = "COUNTYFIPS", - mse_est = TRUE, - B = 10L, - parallel = FALSE) +suppressWarnings( + result <- saeczi(samp, + pop, + lin_formula, + domain_level = "COUNTYFIPS", + mse_est = TRUE, + B = 10L, + parallel = FALSE) +) test_that("result$res is a df", { expect_s3_class(result$res, "data.frame") diff --git a/tests/testthat/test-samp_by_grp.R b/tests/testthat/test-samp_by_grp.R new file mode 100644 index 0000000..5ea874e --- /dev/null +++ b/tests/testthat/test-samp_by_grp.R @@ -0,0 +1,16 @@ +data(samp) +data(pop) + +res <- samp_by_grp(samp, pop, "COUNTYFIPS", 10) + +test_that("plots per group are correct", { + out <- vector(mode = "logical", length = 10) + chk <- as.data.frame(table(samp$COUNTYFIPS)) + for (i in 1:10) { + cmp <- as.data.frame(table(res[[i]]$COUNTYFIPS)) + mtch <- all.equal(chk$Freq, cmp$Freq) + out[i] <- mtch + } + expect_true(all(out)) +}) +