Creates a SupervisedPreprocessorConfig object: the preprocessing a train
run can fit, which is setup_Preprocessor without the four operations a
fitted preprocessor cannot carry.
setup_SupervisedPreprocessor(
missingness = FALSE,
impute = FALSE,
impute_type = c("meanMode", "missRanger"),
impute_missRanger_params = list(pmm.k = 3L, maxiter = 10L, num.trees = 500L),
impute_numeric = c("mean", "median"),
impute_categorical = "mode",
impute_numeric_values = NULL,
impute_level_values = NULL,
integer2factor = FALSE,
integer2numeric = FALSE,
logical2factor = FALSE,
logical2numeric = FALSE,
numeric2factor = FALSE,
numeric2factor_levels = NULL,
numeric_cut_n = 0L,
numeric_cut_labels = FALSE,
numeric_quant_n = 0L,
numeric_quant_NAonly = FALSE,
unique_len2factor = 0L,
character2factor = FALSE,
factorNA2missing = FALSE,
factorNA2missing_level = "missing",
factor2integer = FALSE,
factor2integer_startat0 = TRUE,
factor2integer_levels = NULL,
scale = FALSE,
center = scale,
scale_centers = NULL,
scale_coefficients = NULL,
remove_constants = FALSE,
remove_constants_skip_missing = TRUE,
remove_features = NULL,
one_hot = FALSE,
one_hot_levels = NULL,
add_date_features = FALSE,
date_features = c("weekday", "month", "year"),
add_holidays = FALSE,
holidays = c("USLaborDay", "NewYearsDay", "ChristmasDay"),
exclude = NULL
)SupervisedPreprocessorConfig object.
Logical: If TRUE, generate new boolean columns for each feature with missing values, indicating which cases were missing data.
Logical: If TRUE, impute missing cases. See impute_type.
Character {"meanMode", "missRanger"}: Imputation
method. "meanMode" imputes the values that impute_numeric and
impute_categorical compute on the training data. "missRanger" fits one
random forest per feature with missing values, applied to validation, test
and new data with predict(); it multiplies the memory and computation of
a train run. See the Imputation section.
List: Parameters passed to
missRanger::missRanger, e.g. "pmm.k", "maxiter", "num.trees". pmm.k
greater than 0 results in predictive mean matching. Reduce num.trees for
faster imputation especially in large datasets. Set pmm.k = 0 to
disable predictive mean matching.
Character {"mean", "median"}: Statistic imputed into
numeric and logical features for impute_type = "meanMode", and into such
features with no missing training values for impute_type = "missRanger".
Integer and bit64::integer64 features take the statistic rounded half to
even, so they remain integer. Logical features take the statistic of their
0/1 values rounded half to even: the more frequent value, FALSE on a tie.
Integer features that code categories belong in factors: see
integer2factor.
Character {"mode"}: Statistic imputed into factor
and character features for impute_type = "meanMode", and into such
features with no missing training values for impute_type = "missRanger".
"mode" is the most frequent value. A tie goes to the first tied level of a
factor and to the first tied value in Unicode code point order of a
character feature.
Optional Named vector: Value imputed into
numeric or logical features, keyed by feature name; a logical feature
takes 1 as TRUE and 0 as FALSE. Covers every such feature for
impute_type = "meanMode" and features with no missing training values
for impute_type = "missRanger". Used in place of the value
impute_numeric computes. A value for an integer feature must be a whole
number, and a value for a logical feature 0 or 1. preprocess stores the
values it uses in the Preprocessor, and apply_preprocessor imputes new
data with them.
Optional Named vector: Value imputed into factor
or character features, keyed by feature name. Covers every such feature
for impute_type = "meanMode" and features with no missing training
values for impute_type = "missRanger". Used in place of the value
impute_categorical computes. preprocess stores the values it uses in the
Preprocessor, and apply_preprocessor imputes new data with them.
Logical: If TRUE, convert all integers to factors. This includes
bit64::integer64 columns.
Logical: If TRUE, convert all integers to numeric
(will only work if integer2factor = FALSE). This includes
bit64::integer64 columns.
Logical: If TRUE, convert all logical variables to factors.
Logical: If TRUE, convert all logical variables to numeric.
Logical: If TRUE, convert all numeric variables to factors.
Optional Character vector: Will be passed to
levels arg of factor() if numeric2factor = TRUE. For advanced/
specific use cases; need to know unique values of numeric vector(s) and given all
numeric vars have same unique values.
Integer [0, Inf): If > 0, convert all numeric variables to factors by
binning using base::cut with breaks equal to this number.
Logical: The labels argument of base::cut.
Integer [0, Inf): If > 0, convert all numeric
variables to factors by binning using base::cut with breaks equal to
this number of quantiles, produced using stats::quantile. 0 disables; 1
is a single break, which bounds no bin, and is rejected.
Logical: If TRUE, only bin numeric variables with missing values.
Integer [0, Inf): Convert all variables with less
than or equal to this number of unique values to factors. 0 and 1 both
disable it: a feature with one unique value is a constant, which
remove_constants covers.
For example, unique_len2factor = 2 converts binary variables encoded as
1, 2 to factors.
Logical: If TRUE, convert all character variables to factors.
Logical: If TRUE, make NA values in factors be of
level factorNA2missing_level. This step runs before imputation, so one
preprocess call can code missing values in categorical variables as a
level and impute numeric variables.
Character: Name of level if
factorNA2missing = TRUE.
Logical: If TRUE, convert all factors to integers.
Logical: If TRUE, start integer coding at 0.
Optional List: Named list of the form
"feature_name" = "levels". Used to code factors identically to the training
data when applying factor2integer to validation or test data using
Preprocessor. Values whose level is absent take the single index above the
known levels.
Logical: If TRUE, scale columns of x.
Logical: If TRUE, center columns of x. If unset, follows scale.
Optional Named vector: Centering values for each feature.
Optional Named vector: Scaling values for each feature.
Logical: If TRUE, remove constant columns.
Logical: If TRUE, skip missing values, before checking if feature is constant.
Optional Character vector: Features to remove.
Logical: If TRUE, convert all factors using one-hot encoding.
Optional List: Named list of the form "feature_name" = "levels". Used when applying
one-hot encoding to validation or test data using Preprocessor.
Logical: If TRUE, extract date features from date columns.
Character {"weekday", "month", "year"} vector: Features to extract from dates.
Logical: If TRUE, extract holidays from date columns.
Character vector: Holidays to flag when add_holidays is
TRUE, named as timeDate holiday functions, e.g. "USLaborDay",
"ChristmasDay". See timeDate::listHolidays() for the valid names. Note
that unprefixed names are not US holidays: "LaborDay" is May 1.
Optional Integer vector: Exclude these columns from preprocessing.
The preferred way to handle missing values is an algorithm that accepts
them, which needs no imputation: the tree-based CART, LightCART, LightGBM,
LightRF, LightRuleFit and Ranger. For an algorithm that requires complete
data, such as GLM or GLMNET, use impute_type = "meanMode", the default: it
stores one value per feature, computed on the training data, and adds
little memory or computation.
impute_type = "missRanger" fits one random forest per feature with missing
values. train fits the preprocessor on every training set it uses: once
per outer resample, and, when hyperparameters are tuned, once per tuning
resample and hyperparameter combination as well. For example, 10 outer
resamples with 5-fold tuning over 20 combinations fit missRanger
10 x (5 x 20 + 1) = 1010 times. Each fitted model, one per outer resample,
keeps its forests and a copy of its training data, which predict() draws
starting values from. Choose it only where a multivariate imputation is
needed, and reduce num.trees and maxiter in impute_missRanger_params
for large data.
A fitted missRanger imputer has three further properties:
It is an R object, held by the fitted model. A config or record written to JSON carries the method and its parameters, not the forests, so a model rebuilt from JSON cannot impute new data with them.
It has a forest only for features with missing values in the training
data. Other features take their impute_numeric or impute_categorical
value, computed on the training data, when they are missing in new data.
predict() stops on a factor level of an imputed feature that is absent
from the training data.
impute_type = "micePMM" imputes only the data 'mice' is run on and is
available in setup_Preprocessor only.
EDG
complete_cases, remove_duplicates and remove_cases_thres drop cases. A
preprocessor fitted by train is replayed on validation, test and whatever
stats::predict() is handed, and a prediction call has nothing it may drop:
asked for n rows it must return n predictions.
remove_features_thres decides which columns to drop from the data in front
of it. Under resampling each fold would learn that set from its own training
subset, so the folds would train over different feature spaces and their
metrics would not average into an estimate of one model. Name the columns
with remove_features instead, which drops the same ones in every fold.
impute_type = "micePMM" imputes only the data 'mice' is run on, so a
fitted preprocessor cannot apply it to validation, test or new data.
The default impute_type is "meanMode"; see the Imputation section.
All four operations and micePMM imputation are available in setup_Preprocessor, for preprocess on a dataset in hand, where the outcome is still attached and what was dropped is visible.
preproc_config <- setup_SupervisedPreprocessor(impute = TRUE)
preproc_config
Run the code above in your browser using DataLab