Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Dec 31, 2024
1 parent edce1bf commit 1f9b8b7
Showing 3 changed files with 34 additions and 16 deletions.
23 changes: 15 additions & 8 deletions R/rescale_weights.R
Original file line number Diff line number Diff line change
@@ -21,18 +21,20 @@
#' @param probability_weights Variable indicating the probability (design or
#' sampling) weights of the survey data (level-1-weight), provided as character
#' string or formula.
#' @param nest Logical, if `TRUE` and `by` indicates at least two
#' group variables, then groups are "nested", i.e. groups are now a
#' combination from each group level of the variables in `by`.
#' @param nest Logical, if `TRUE` and `by` indicates at least two group
#' variables, then groups are "nested", i.e. groups are now a combination from
#' each group level of the variables in `by`. This argument is not used when
#' `method = "kish"`.
#' @param method String, indicating which rescale-method is used for rescaling
#' weights. Can be either `"carle"` (default) or `"kish"`. See 'Details'. If
#' `method = "carle"`, the `by` argument is required.
#'
#' @return `data`, including the new weighting variable(s). For
#' `method = "carle"`, new columns `rescaled_weights_a` and `rescaled_weights_b`
#' are returned, and for `method = "kish"`, the returned data contains a column
#' `rescaled_weights`. These represent the rescaled design weights to use in
#' multilevel models (use these variables for the `weights` argument).
#' @return
#' `data`, including the new weighting variable(s). For `method = "carle"`, new
#' columns `rescaled_weights_a` and `rescaled_weights_b` are returned, and for
#' `method = "kish"`, the returned data contains a column `rescaled_weights`.
#' These represent the rescaled design weights to use in multilevel models (use
#' these variables for the `weights` argument).
#'
#' @details
#' - `method = "carle"`
@@ -214,6 +216,11 @@ rescale_weights <- function(data,
# sort id
data_tmp$.bamboozled <- seq_len(nrow(data_tmp))

# `nest` is currently ignored
if (isTRUE(nest)) {
insight::format_warning("Argument `nest` is ignored for `method = \"kish\"`.")
}

# check by argument
if (!is.null(by) && !all(by %in% colnames(data_tmp))) {
dont_exist <- setdiff(by, colnames(data_tmp))
17 changes: 9 additions & 8 deletions man/rescale_weights.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/test-rescale_weights.R
Original file line number Diff line number Diff line change
@@ -91,6 +91,16 @@ test_that("rescale_weights errors and warnings", {
),
regex = "The following variable(s)"
)
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),

0 comments on commit 1f9b8b7

Please sign in to comment.