diff --git a/R/rescale_weights.R b/R/rescale_weights.R index 1f528c77d..ec0c75616 100644 --- a/R/rescale_weights.R +++ b/R/rescale_weights.R @@ -60,30 +60,32 @@ #' - Asparouhov T. (2006). General Multi-Level Modeling with Sampling #' Weights. Communications in Statistics - Theory and Methods 35: 439-460 #' -#' @examplesIf insight::check_if_installed("lme4", quietly = TRUE) -#' data(nhanes_sample) -#' head(rescale_weights(nhanes_sample, "SDMVSTRA", "WTINT2YR")) +#' @examples +#' if (require("lme4")) { +#' data(nhanes_sample) +#' head(rescale_weights(nhanes_sample, "SDMVSTRA", "WTINT2YR")) #' -#' # also works with multiple group-variables -#' head(rescale_weights(nhanes_sample, c("SDMVSTRA", "SDMVPSU"), "WTINT2YR")) +#' # also works with multiple group-variables +#' head(rescale_weights(nhanes_sample, c("SDMVSTRA", "SDMVPSU"), "WTINT2YR")) #' -#' # or nested structures. -#' x <- rescale_weights( -#' data = nhanes_sample, -#' by = c("SDMVSTRA", "SDMVPSU"), -#' probability_weights = "WTINT2YR", -#' nest = TRUE -#' ) -#' head(x) +#' # or nested structures. +#' x <- rescale_weights( +#' data = nhanes_sample, +#' by = c("SDMVSTRA", "SDMVPSU"), +#' probability_weights = "WTINT2YR", +#' nest = TRUE +#' ) +#' head(x) #' -#' nhanes_sample <- rescale_weights(nhanes_sample, "SDMVSTRA", "WTINT2YR") +#' nhanes_sample <- rescale_weights(nhanes_sample, "SDMVSTRA", "WTINT2YR") #' -#' lme4::glmer( -#' total ~ factor(RIAGENDR) * (log(age) + factor(RIDRETH1)) + (1 | SDMVPSU), -#' family = poisson(), -#' data = nhanes_sample, -#' weights = pweights_a -#' ) +#' glmer( +#' total ~ factor(RIAGENDR) * (log(age) + factor(RIDRETH1)) + (1 | SDMVPSU), +#' family = poisson(), +#' data = nhanes_sample, +#' weights = pweights_a +#' ) +#' } #' @export rescale_weights <- function(data, by, probability_weights, nest = FALSE) { if (inherits(by, "formula")) { diff --git a/man/rescale_weights.Rd b/man/rescale_weights.Rd index d2eddc894..d9651decb 100644 --- a/man/rescale_weights.Rd +++ b/man/rescale_weights.Rd @@ -63,31 +63,31 @@ design, or group and PSU as varying intercepts), depending on the survey design that should be mimicked. } \examples{ -\dontshow{if (insight::check_if_installed("lme4", quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} -data(nhanes_sample) -head(rescale_weights(nhanes_sample, "SDMVSTRA", "WTINT2YR")) +if (require("lme4")) { + data(nhanes_sample) + head(rescale_weights(nhanes_sample, "SDMVSTRA", "WTINT2YR")) -# also works with multiple group-variables -head(rescale_weights(nhanes_sample, c("SDMVSTRA", "SDMVPSU"), "WTINT2YR")) + # also works with multiple group-variables + head(rescale_weights(nhanes_sample, c("SDMVSTRA", "SDMVPSU"), "WTINT2YR")) -# or nested structures. -x <- rescale_weights( - data = nhanes_sample, - by = c("SDMVSTRA", "SDMVPSU"), - probability_weights = "WTINT2YR", - nest = TRUE -) -head(x) + # or nested structures. + x <- rescale_weights( + data = nhanes_sample, + by = c("SDMVSTRA", "SDMVPSU"), + probability_weights = "WTINT2YR", + nest = TRUE + ) + head(x) -nhanes_sample <- rescale_weights(nhanes_sample, "SDMVSTRA", "WTINT2YR") + nhanes_sample <- rescale_weights(nhanes_sample, "SDMVSTRA", "WTINT2YR") -lme4::glmer( - total ~ factor(RIAGENDR) * (log(age) + factor(RIDRETH1)) + (1 | SDMVPSU), - family = poisson(), - data = nhanes_sample, - weights = pweights_a -) -\dontshow{\}) # examplesIf} + glmer( + total ~ factor(RIAGENDR) * (log(age) + factor(RIDRETH1)) + (1 | SDMVPSU), + family = poisson(), + data = nhanes_sample, + weights = pweights_a + ) +} } \references{ \itemize{