From 1f9b8b7f707f27c189013d0a1d961dfa35b73072 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 31 Dec 2024 17:12:17 +0100 Subject: [PATCH] tests --- R/rescale_weights.R | 23 +++++++++++++++-------- man/rescale_weights.Rd | 17 +++++++++-------- tests/testthat/test-rescale_weights.R | 10 ++++++++++ 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/R/rescale_weights.R b/R/rescale_weights.R index aa0d260be..35e1bba98 100644 --- a/R/rescale_weights.R +++ b/R/rescale_weights.R @@ -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)) diff --git a/man/rescale_weights.Rd b/man/rescale_weights.Rd index 646ae1da4..85ffae0b9 100644 --- a/man/rescale_weights.Rd +++ b/man/rescale_weights.Rd @@ -26,20 +26,21 @@ variables; in such cases, each created weighting variable will be suffixed by the name of the group variable. This argument is required for \code{method = "carle"}, but optional for \code{method = "kish"}.} -\item{nest}{Logical, if \code{TRUE} and \code{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 \code{by}.} +\item{nest}{Logical, if \code{TRUE} and \code{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 \code{by}. This argument is not used when +\code{method = "kish"}.} \item{method}{String, indicating which rescale-method is used for rescaling weights. Can be either \code{"carle"} (default) or \code{"kish"}. See 'Details'. If \code{method = "carle"}, the \code{by} argument is required.} } \value{ -\code{data}, including the new weighting variable(s). For -\code{method = "carle"}, new columns \code{rescaled_weights_a} and \code{rescaled_weights_b} -are returned, and for \code{method = "kish"}, the returned data contains a column -\code{rescaled_weights}. These represent the rescaled design weights to use in -multilevel models (use these variables for the \code{weights} argument). +\code{data}, including the new weighting variable(s). For \code{method = "carle"}, new +columns \code{rescaled_weights_a} and \code{rescaled_weights_b} are returned, and for +\code{method = "kish"}, the returned data contains a column \code{rescaled_weights}. +These represent the rescaled design weights to use in multilevel models (use +these variables for the \code{weights} argument). } \description{ Most functions to fit multilevel and mixed effects models only diff --git a/tests/testthat/test-rescale_weights.R b/tests/testthat/test-rescale_weights.R index 1c204057e..2800024cb 100644 --- a/tests/testthat/test-rescale_weights.R +++ b/tests/testthat/test-rescale_weights.R @@ -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),