case_weights: Generate sample weights for imbalanced responses

View source: R/case_weights.R

case_weightsR Documentation

Generate sample weights for imbalanced responses

Description

Computes case weights adding to one for response variables of these types:

  • numeric binomial (1 and 0).

  • logical (TRUE and FALSE): converted to numeric internally.

  • categorical (character or factor)

Values NA, Inf, -Inf, and NaN are invalid for numeric and logical variables and will result in errors. For categorical variables, these are converted to their respective categories ("NA", "Inf", "-Inf", and "NaN") with their assigned case weights.

All returned weights sum to one.

Usage

case_weights(x = NULL, ...)

Arguments

x

(required, integer, character, or factor vector) Values of a binomial, categorical, or factor variable. Default: NULL

...

(optional) Internal args (e.g. function_name for validate_arg_function_name, a precomputed correlation matrix m, or cross-validation args for preference_order).

Value

numeric vector: case weights

See Also

Other modelling_tools: model_formula(), score_auc(), score_cramer(), score_r2()

Examples

 #numeric vector
 y <- case_weights(
   x = c(0, 0, 1, 1)
   )

 #logical vector
 y <- case_weights(
   x = c(TRUE, TRUE, FALSE, FALSE)
   )

 #character vector
 y <- case_weights(
   x = c("a", "a", "b", "c")
   )

collinear documentation built on Dec. 8, 2025, 5:06 p.m.