1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
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)
expect_snapshot(head(rescale_weights(nhanes_sample, "WTINT2YR", "SDMVSTRA")))
expect_snapshot(head(rescale_weights(nhanes_sample, "WTINT2YR", c("SDMVSTRA", "SDMVPSU"))))
expect_snapshot(head(rescale_weights(nhanes_sample, probability_weights = "WTINT2YR", method = "kish")))
out <- rescale_weights(nhanes_sample, "WTINT2YR", "SDMVSTRA")
expect_equal(sum(out$rescaled_weights_a), 2992, tolerance = 1e-3)
expect_equal(sum(out$rescaled_weights_b), 2244.71451, tolerance = 1e-3)
out <- rescale_weights(nhanes_sample, "WTINT2YR", method = "kish")
expect_equal(sum(out$rescaled_weights), 2162.53961, tolerance = 1e-3)
out <- rescale_weights(nhanes_sample, "WTINT2YR", by = "SDMVPSU", method = "kish")
expect_equal(sum(out$rescaled_weights), 2163.3657, tolerance = 1e-3)
})
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
nhanes_sample <- as.data.frame(nhanes_sample)
expect_snapshot(
rescale_weights(
data = head(nhanes_sample, n = 30),
by = c("SDMVSTRA", "SDMVPSU"),
probability_weights = "WTINT2YR",
nest = TRUE
)
)
expect_warning(
{
x <- rescale_weights(
data = head(nhanes_sample),
by = "SDMVPSU",
probability_weights = "WTINT2YR",
nest = TRUE
)
},
"Only one group variable selected"
)
expect_identical(
x,
rescale_weights(
data = head(nhanes_sample),
by = "SDMVPSU",
probability_weights = "WTINT2YR"
)
)
})
test_that("rescale_weights errors and warnings", {
data(nhanes_sample)
expect_error(
rescale_weights(
data = head(nhanes_sample, n = 30),
by = c("a", "SDMVSTRA", "c"),
probability_weights = "WTINT2YR"
),
regex = "The following"
)
expect_error(
rescale_weights(
data = head(nhanes_sample, n = 30),
by = "SDMVSTRA",
probability_weights = NULL
),
regex = "is missing, but required"
)
expect_error(
rescale_weights(
data = head(nhanes_sample, n = 30),
by = NULL,
probability_weights = "WTINT2YR"
),
regex = "must be specified"
)
expect_error(
rescale_weights(
data = head(nhanes_sample, n = 30),
by = "abc",
probability_weights = "WTINT2YR",
method = "kish"
),
regex = "The following variable"
)
expect_warning(
rescale_weights(
data = head(nhanes_sample, n = 30),
by = "SDMVSTRA",
probability_weights = "WTINT2YR",
nest = TRUE,
method = "kish"
),
regex = "is ignored"
)
expect_error(
rescale_weights(
data = head(nhanes_sample, n = 30),
probability_weights = "WTINT2YR",
method = "dish"
),
regex = "Invalid option for argument"
)
nhanes_sample$rescaled_weights_a <- 1
expect_warning(
{
out <- rescale_weights(
data = head(nhanes_sample, n = 30),
by = "SDMVSTRA",
probability_weights = "WTINT2YR"
)
},
regex = "The variable name"
)
expect_named(
out,
c(
"total", "age", "RIAGENDR", "RIDRETH1", "SDMVPSU", "SDMVSTRA",
"WTINT2YR", "rescaled_weights_a", "rescaled_weights_a_1", "rescaled_weights_b"
)
)
})
|