Learn R Programming

spatialRF (version 1.1.5)

case_weights: 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)

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.

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.

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

Run this code
# 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

Run the code above in your browser using DataLab