case_weights: Generate case weights for imbalanced binary data

View source: R/case_weights.R

case_weightsR Documentation

Generate case weights for imbalanced binary data

Description

Generates case weights to balance binary response variables for use with ranger models. Used internally by rf().

Usage

case_weights(data = NULL, dependent.variable.name = NULL)

Arguments

data

Data frame containing the response variable. Default: NULL.

dependent.variable.name

Character string specifying the response variable name. Must be a column in data. Default: NULL.

Details

The weighting scheme assigns higher weights to the minority class to balance training:

  • Cases with value 0: weight = 1 / n_zeros

  • Cases with value 1: weight = 1 / n_ones

This ensures both classes contribute equally to model training regardless of class imbalance.

Value

Numeric vector of length nrow(data) with case weights. Each weight is the inverse of the class frequency: 1/n_zeros for 0s and 1/n_ones for 1s.

See Also

Other preprocessing: auto_cor(), auto_vif(), default_distance_thresholds(), double_center_distance_matrix(), is_binary(), make_spatial_fold(), make_spatial_folds(), the_feature_engineer(), weights_from_distance_matrix()

Examples

# Imbalanced dataset: 3 zeros, 2 ones
weights <- case_weights(
  data = data.frame(
    response = c(0, 0, 0, 1, 1)
  ),
  dependent.variable.name = "response"
)

weights
# Returns: 0.333, 0.333, 0.333, 0.5, 0.5
# Zeros get weight 1/3, ones get weight 1/2


spatialRF documentation built on Dec. 20, 2025, 1:07 a.m.