Skip to main content

8  Preprocessing

Preprocessing can change values, encode predictors, or select rows and columns. General data preparation and model-fitted transformations have different roles in an analysis.

8.1 Prepare data

setup_Preprocessor() configures standalone preprocessing. preprocess() returns a Preprocessor; preprocessed() extracts its data.

x <- iris[, 1:4]
x[c(5L, 20L), 1L] <- NA_real_
preprocessor <- preprocess(
  x,
  config = setup_Preprocessor(impute = TRUE, impute_type = "meanMode", scale = TRUE)
)
2026-10-25 14:46:56 Imputing missing values using the mean (numeric) and mode (categorical)... [preprocess_data]
2026-10-25 14:46:56 Scaling and centering 4 numeric features... [preprocess_data]
2026-10-25 14:46:56 Preprocessing done. [preprocess_data]
head(preprocessed(preprocessor))
   Sepal.Length Sepal.Width Petal.Length Petal.Width
1 -9.163825e-01  1.01560199    -1.335752   -1.311052
2 -1.159437e+00 -0.13153881    -1.335752   -1.311052
3 -1.402492e+00  0.32731751    -1.392399   -1.311052
4 -1.524020e+00  0.09788935    -1.279104   -1.311052
5 -2.158761e-15  1.24503015    -1.335752   -1.311052
6 -5.518002e-01  1.93331463    -1.165809   -1.048667

preprocess() modifies a data.table by reference. Use data.table::copy() when the original table must remain available.

8.2 Apply learned transformations

A fitted preprocessor stores the quantities needed to transform new data.

new_x <- iris[1:5, 1:4]
new_preprocessed <- apply_preprocessor(preprocessor, new_x)
2026-10-25 14:46:56 Imputing missing values using the mean (numeric) and mode (categorical)... [preprocess_data]
2026-10-25 14:46:56 Scaling and centering 4 numeric features... [preprocess_data]
2026-10-25 14:46:56 Preprocessing done. [preprocess_data]
head(new_preprocessed)
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1   -0.9163825  1.01560199    -1.335752   -1.311052
2   -1.1594374 -0.13153881    -1.335752   -1.311052
3   -1.4024922  0.32731751    -1.392399   -1.311052
4   -1.5240196  0.09788935    -1.279104   -1.311052
5   -1.0379099  1.24503015    -1.335752   -1.311052
get_fitted_config(preprocessor)@scale_centers
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
    5.854054     3.057333     3.758000     1.199333 

The fitted configuration contains learned centers, scales, or encodings. Factor encodings follow the training levels.

8.3 Preprocess within model training

Data-dependent transformations belong inside the training procedure when estimating predictive performance. setup_SupervisedPreprocessor() describes transformations that preserve case alignment and the supervised feature contract. train() fits these operations within the relevant training splits and applies the fitted operations to validation and assessment data.

mod <- train(
  iris,
  preprocessor_config = setup_SupervisedPreprocessor(scale = TRUE),
  hyperparameters = setup_CART(),
  outer_resampling_config = setup_KFold(n_resamples = 3L, seed = 2026L),
  execution_config = setup_SerialExecution(seed = 2027L)
)
2026-10-25 14:46:56 Checking data is ready for training... ✔ [check_supervised]
2026-10-25 14:46:56 ▶ [train]
2026-10-25 14:46:56 Training set: 150 cases x 4 features. [summarize_supervised]
2026-10-25 14:46:56 // CPU | serial | 1 worker: algorithm 1 thread, tuning 1, outer resampling 1 [msg_resources]
2026-10-25 14:46:56 Shared memory (auto): not sharing training data (7.3 kB) -- this run transfers nothing to workers. [report_shared_memory]
2026-10-25 14:46:56 <> Training CART Classification using 3 independent folds... [train]
2026-10-25 14:46:56 ✔ Outer resamples 3/3 done in 0:00
2026-10-25 14:46:56 </> Outer resampling done. [train]
<Resampled Classification Model>
CART (CART decision tree (rpart))
▣ Preprocessed using centering, scaling.
⟳ Tested using 3 independent folds.

  <Resampled Classification Training Metrics>
  Aggregate Confusion Matrix across resamples.
                     Predicted
          Reference  setosa  versicolor  virginica  
             setosa     100           0          0
         versicolor       0          98          2
          virginica       0           1         99

  Showing mean (sd) across resamples.
  Balanced Accuracy: 0.990 (0.010)
                 F1: 0.990 (0.010)
           Accuracy: 0.990 (0.010)

  <Resampled Classification Test Metrics>
  Aggregate Confusion Matrix across resamples.
                     Predicted
          Reference  setosa  versicolor  virginica  
             setosa      50           0          0
         versicolor       0          48          2
          virginica       0           5         45

  Showing mean (sd) across resamples.
  Balanced Accuracy: 0.953 (0.030)
                 F1: 0.953 (0.030)
           Accuracy: 0.953 (0.030)

2026-10-25 14:46:56 Done in 0.44 seconds. [train]

Supported operations include imputation, missingness indicators, type conversion, one-hot encoding, scaling, and date features. Their applicability depends on the data and learner. Resolve record duplication and study-specific exclusions before defining resamples; preserve the exclusion criteria in the analysis record.

A decomposition_config adds a fitted dimensionality-reduction step to train(). That decomposition must support application to new cases; see decomposition.

© 2026 E.D. Gennatas