View source: R/090_Preprocessor.R
| setup_SupervisedPreprocessor | R Documentation |
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
)
missingness |
Logical: If TRUE, generate new boolean columns for each feature with missing values, indicating which cases were missing data. |
impute |
Logical: If TRUE, impute missing cases. See |
impute_type |
Character {"meanMode", "missRanger"}: Imputation
method. "meanMode" imputes the values that |
impute_missRanger_params |
List: Parameters passed to
|
impute_numeric |
Character {"mean", "median"}: Statistic imputed into
numeric and logical features for |
impute_categorical |
Character {"mode"}: Statistic imputed into factor
and character features for |
impute_numeric_values |
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_level_values |
Optional Named vector: Value imputed into factor
or character features, keyed by feature name. Covers every such feature
for |
integer2factor |
Logical: If TRUE, convert all integers to factors. This includes
|
integer2numeric |
Logical: If TRUE, convert all integers to numeric
(will only work if |
logical2factor |
Logical: If TRUE, convert all logical variables to factors. |
logical2numeric |
Logical: If TRUE, convert all logical variables to numeric. |
numeric2factor |
Logical: If TRUE, convert all numeric variables to factors. |
numeric2factor_levels |
Optional Character vector: Will be passed to
|
numeric_cut_n |
Integer [0, Inf): If > 0, convert all numeric variables to factors by
binning using |
numeric_cut_labels |
Logical: The |
numeric_quant_n |
Integer [0, Inf): If > 0, convert all numeric
variables to factors by binning using |
numeric_quant_NAonly |
Logical: If TRUE, only bin numeric variables with missing values. |
unique_len2factor |
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
|
character2factor |
Logical: If TRUE, convert all character variables to factors. |
factorNA2missing |
Logical: If TRUE, make NA values in factors be of
level |
factorNA2missing_level |
Character: Name of level if
|
factor2integer |
Logical: If TRUE, convert all factors to integers. |
factor2integer_startat0 |
Logical: If TRUE, start integer coding at 0. |
factor2integer_levels |
Optional List: Named list of the form
"feature_name" = "levels". Used to code factors identically to the training
data when applying |
scale |
Logical: If TRUE, scale columns of |
center |
Logical: If TRUE, center columns of |
scale_centers |
Optional Named vector: Centering values for each feature. |
scale_coefficients |
Optional Named vector: Scaling values for each feature. |
remove_constants |
Logical: If TRUE, remove constant columns. |
remove_constants_skip_missing |
Logical: If TRUE, skip missing values, before checking if feature is constant. |
remove_features |
Optional Character vector: Features to remove. |
one_hot |
Logical: If TRUE, convert all factors using one-hot encoding. |
one_hot_levels |
Optional List: Named list of the form "feature_name" = "levels". Used when applying
one-hot encoding to validation or test data using |
add_date_features |
Logical: If TRUE, extract date features from date columns. |
date_features |
Character {"weekday", "month", "year"} vector: Features to extract from dates. |
add_holidays |
Logical: If TRUE, extract holidays from date columns. |
holidays |
Character vector: Holidays to flag when |
exclude |
Optional Integer vector: Exclude these columns from preprocessing. |
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.
SupervisedPreprocessorConfig object.
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
preproc_config <- setup_SupervisedPreprocessor(impute = TRUE)
preproc_config
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.