add_case_weights {workflows} | R Documentation |
Add case weights to a workflow
Description
This family of functions revolves around selecting a column of data
to use
for case weights. This column must be one of the allowed case weight types,
such as hardhat::frequency_weights()
or hardhat::importance_weights()
.
Specifically, it must return TRUE
from hardhat::is_case_weights()
. The
underlying model will decide whether or not the type of case weights you have
supplied are applicable or not.
-
add_case_weights()
specifies the column that will be interpreted as case weights in the model. This column must be present in thedata
supplied to fit(). -
remove_case_weights()
removes the case weights. Additionally, if the model has already been fit, then the fit is removed. -
update_case_weights()
first removes the case weights, then replaces them with the new ones.
Usage
add_case_weights(x, col)
remove_case_weights(x)
update_case_weights(x, col)
Arguments
x |
A workflow |
col |
A single unquoted column name specifying the case weights for
the model. This must be a classed case weights column, as determined by
|
Details
For formula and variable preprocessors, the case weights col
is removed
from the data before the preprocessor is evaluated. This allows you to use
formulas like y ~ .
or tidyselection like everything()
without fear of
accidentally selecting the case weights column.
For recipe preprocessors, the case weights col
is not removed and is
passed along to the recipe. Typically, your recipe will include steps that
can utilize case weights.
Examples
library(parsnip)
library(magrittr)
library(hardhat)
mtcars2 <- mtcars
mtcars2$gear <- frequency_weights(mtcars2$gear)
spec <- linear_reg() %>%
set_engine("lm")
wf <- workflow() %>%
add_case_weights(gear) %>%
add_formula(mpg ~ .) %>%
add_model(spec)
wf <- fit(wf, mtcars2)
# Notice that the case weights (gear) aren't included in the predictors
extract_mold(wf)$predictors
# Strip them out of the workflow, which also resets the model
remove_case_weights(wf)