| case_weights | R Documentation |
Generates case weights to balance binary response variables for use with ranger models. Used internally by rf().
case_weights(data = NULL, dependent.variable.name = NULL)
data |
Data frame containing the response variable. Default: |
dependent.variable.name |
Character string specifying the response variable name. Must be a column in |
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.
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.
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()
# 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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.