R/case_weights.R

Defines functions case_weights

Documented in case_weights

#' @title Case Weights for Unbalanced Binomial or Categorical Responses
#'
#' @param x (required, integer, character, or factor vector) binomial, categorical, or factor response variable. Default: `NULL`
#' @return numeric vector: case weights
#' @examples
#'  case_weights(
#'    x = c(0, 0, 0, 1, 1)
#'    )
#'
#'  case_weights(
#'    x = c("a", "a", "b", "b", "c")
#'    )
#' @family modelling_tools
#' @autoglobal
#' @export
case_weights <- function(
    x = NULL
){

  if(is.null(x)){
    stop(
      "collinear::case_weights(): argument 'x' cannot be NULL.",
      call. = FALSE
    )
  }

  # weights per class
  # as inverse of the counts
  weights <- 1 / table(as.factor(x))

  # full vector of weights
  weights[as.character(x)]

}

Try the collinear package in your browser

Any scripts or data that you put into this service are public.

collinear documentation built on April 12, 2025, 1:36 a.m.