Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Dec 31, 2024
1 parent 1f9b8b7 commit 475117e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
75 changes: 75 additions & 0 deletions tests/testthat/_snaps/rescale_weights.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,81 @@
5 1 2.00 2 1 1 41 14746.45 0.2108234
6 6 2.20 2 4 1 38 28232.10 0.4036216

---

Code
rescale_weights(nhanes_sample, "WTINT2YR", "SDMVSTRA")
Output
total age RIAGENDR RIDRETH1 SDMVPSU SDMVSTRA WTINT2YR rescaled_weights_a
1 1 2.20 1 3 2 31 97593.68 1.0000000
2 7 2.08 2 3 1 29 39599.36 0.5819119
3 3 1.48 2 1 2 42 NA NA
4 4 1.32 2 4 2 33 34998.53 0.6766764
5 1 2.00 2 1 1 41 14746.45 0.7471696
6 6 2.20 2 4 1 38 28232.10 1.0000000
7 350 1.60 1 3 2 33 93162.43 1.8012419
8 NA 1.48 2 3 1 29 82275.99 1.2090441
9 3 2.28 2 4 1 41 24726.39 1.2528304
10 30 0.84 1 3 2 35 NA NA
11 70 1.24 1 4 2 33 27002.70 0.5220817
12 5 1.68 2 1 2 39 18792.03 1.0000000
13 60 2.20 1 3 2 30 76894.56 1.0000000
14 2 1.48 2 3 1 29 NA NA
15 8 2.36 2 3 2 39 NA NA
16 3 2.04 2 3 2 36 98200.91 1.0000000
17 1 2.08 1 3 1 40 87786.09 1.0000000
18 7 1.00 1 3 2 32 90803.16 1.0000000
19 9 2.28 2 3 2 34 NA NA
20 2 1.24 2 3 1 29 82275.99 1.2090441
rescaled_weights_b
1 1.0000000
2 0.5351412
3 NA
4 0.5107078
5 0.7022777
6 1.0000000
7 1.3594509
8 1.1118681
9 1.1775572
10 NA
11 0.3940306
12 1.0000000
13 1.0000000
14 NA
15 NA
16 1.0000000
17 1.0000000
18 1.0000000
19 NA
20 1.1118681

---

Code
rescale_weights(nhanes_sample, "WTINT2YR", method = "kish")
Output
total age RIAGENDR RIDRETH1 SDMVPSU SDMVSTRA WTINT2YR rescaled_weights
1 1 2.20 1 3 2 31 97593.68 1.2734329
2 7 2.08 2 3 1 29 39599.36 0.5167049
3 3 1.48 2 1 2 42 NA NA
4 4 1.32 2 4 2 33 34998.53 0.4566718
5 1 2.00 2 1 1 41 14746.45 0.1924164
6 6 2.20 2 4 1 38 28232.10 0.3683813
7 350 1.60 1 3 2 33 93162.43 1.2156126
8 NA 1.48 2 3 1 29 82275.99 1.0735629
9 3 2.28 2 4 1 41 24726.39 0.3226377
10 30 0.84 1 3 2 35 NA NA
11 70 1.24 1 4 2 33 27002.70 0.3523397
12 5 1.68 2 1 2 39 18792.03 0.2452044
13 60 2.20 1 3 2 30 76894.56 1.0033444
14 2 1.48 2 3 1 29 NA NA
15 8 2.36 2 3 2 39 NA NA
16 3 2.04 2 3 2 36 98200.91 1.2813563
17 1 2.08 1 3 1 40 87786.09 1.1454605
18 7 1.00 1 3 2 32 90803.16 1.1848281
19 9 2.28 2 3 2 34 NA NA
20 2 1.24 2 3 1 29 82275.99 1.0735629

# rescale_weights nested works as expected

Code
Expand Down
16 changes: 15 additions & 1 deletion tests/testthat/test-rescale_weights.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ test_that("rescale_weights works as expected", {
})


test_that("rescale_weights works as expected", {
data(nhanes_sample)
# convert tibble into data frame, so check-hard GHA works
nhanes_sample <- as.data.frame(nhanes_sample)[1:20, ]

# add NAs
set.seed(123)
nhanes_sample$WTINT2YR[sample.int(nrow(nhanes_sample), 5)] <- NA

expect_snapshot(rescale_weights(nhanes_sample, "WTINT2YR", "SDMVSTRA"))
expect_snapshot(rescale_weights(nhanes_sample, "WTINT2YR", method = "kish"))
})


test_that("rescale_weights nested works as expected", {
data(nhanes_sample)
# convert tibble into data frame, so check-hard GHA works
Expand Down Expand Up @@ -89,7 +103,7 @@ test_that("rescale_weights errors and warnings", {
probability_weights = "WTINT2YR",
method = "kish"
),
regex = "The following variable(s)"
regex = "The following variable"
)
expect_warning(
rescale_weights(
Expand Down

0 comments on commit 475117e

Please sign in to comment.