| Title: | Machine Learning Feature Engineering with R |
|---|---|
| Description: | Utilities for scaling and transformation of features in the context of machine learning regression problems. |
| Authors: | Thomas Petzoldt [aut, cre] (ORCID: <https://orcid.org/0000-0002-4951-6468>) |
| Maintainer: | Thomas Petzoldt <[email protected]> |
| License: | GPL (>= 2) |
| Version: | 0.4.3 |
| Built: | 2026-10-06 10:52:23 UTC |
| Source: | https://github.com/tpetzoldt/mlfeaturer |
This package provides preprocessing tools using S4 classes for data transformation, scaling, and inverse transformation.
Maintainer: Thomas Petzoldt [email protected] (ORCID)
Authors:
Thomas Petzoldt [email protected] (ORCID)
Useful links:
Coerces a 'feature_data' object to a 'feature_data_list' object.
Coerces a 'feature_data' object to a standard R 'list' object.
from |
A 'feature_data' object. |
A 'feature_data_list' object.
A 'list' object.
Coerces a 'feature_data' object to a 'list' object.
## S4 method for signature 'feature_data' as.list(x)## S4 method for signature 'feature_data' as.list(x)
x |
A 'feature_data' object. |
A 'list' object.
Constructs a 'feature_data' object, calculates scaling parameters, and optionally applies initial data transformations.
create_preprocessed_data( data, target_col, id_col = NULL, split_col = NULL, scale_option = c("train", "test", "all"), scale_method = c("zscore", "minmax", "none"), fun_transform = NULL, fun_inverse = NULL )create_preprocessed_data( data, target_col, id_col = NULL, split_col = NULL, scale_option = c("train", "test", "all"), scale_method = c("zscore", "minmax", "none"), fun_transform = NULL, fun_inverse = NULL )
data |
A data frame containing the raw data. |
target_col |
Character vector specifying the name of the target variable column. |
id_col |
Character vector specifying the name of the ID column (optional). |
split_col |
Character vector specifying the name of the column used for train/test split. |
scale_option |
Character string specifying the scaling option. Must be one of "train", "test", or "all". Defaults to "train". |
scale_method |
Character string specifying the scaling method. Must be one of "zscore" (standardization) or "minmax" (normalization). Defaults to "zscore". |
fun_transform |
A named list of functions to apply to the data *before* scaling. The names of the list elements should correspond to the columns to transform. |
fun_inverse |
A named list of functions representing the inverse transformations of 'fun_transform'. These are applied during the 'inverse_transform' method. |
This function calculates scaling parameters (mean, standard deviation, min, max) based on the specified 'scale_option' and 'scale_method'. It then creates a 'feature_data' object, storing the original data and the calculated parameters. If 'fun_transform' is provided, it applies the specified transformations to the data before creating the object.
If no 'id_col' is given, an additional column 'id' with row numers is created.
If a 'split_col' is missing, an random 'split' column is created.
A 'feature_data' object.
# Example usage (replace with your actual data and column names) df <- data.frame( id = 1:10, x = runif(10), y = rnorm(10), z = 1:10, split = sample(c(TRUE, FALSE), 10, replace = TRUE) ) transformations <- list( x = log, y = \(y) sqrt(5 + y), z = \(z) z^2 ) inverse_transformations <- list( x = exp, y = \(y) y^2 - 5, z = sqrt ) prep_data <- create_preprocessed_data( df, id_col = "id", target_col = "y", split_col = "split", fun_transform = transformations, fun_inverse = inverse_transformations) print(prep_data)# Example usage (replace with your actual data and column names) df <- data.frame( id = 1:10, x = runif(10), y = rnorm(10), z = 1:10, split = sample(c(TRUE, FALSE), 10, replace = TRUE) ) transformations <- list( x = log, y = \(y) sqrt(5 + y), z = \(z) z^2 ) inverse_transformations <- list( x = exp, y = \(y) y^2 - 5, z = sqrt ) prep_data <- create_preprocessed_data( df, id_col = "id", target_col = "y", split_col = "split", fun_transform = transformations, fun_inverse = inverse_transformations) print(prep_data)
Growthrates of four different phytoplankton species, dependent on temperature and light.
Data frame with the following columns:
Phytoplankton species.
Temperature in °C.
Photosynthetic active radiation in µ E m-2s-1.
Population growth rate in d-1.
The example data were taken manually from the figures of Dauta et al. 1990.
Dauta, A, Devaux, J, Piquemal, F and Boumnich, L (1990) Growth Rate of Four Algae in Relation to Light and Temperature. Hydrobiologia 207: 221–26. doi:10.1007/BF00041459
An S4 class to represent the preprocessed data as a list.
x_trainTraining data (X).
x_testTest data (X).
x_allAll data (X).
y_trainTraining data (Y).
y_testTest data (Y).
y_allAll data (Y).
x_train_scaledScaled training data (X).
x_test_scaledScaled test data (X).
x_all_scaledScaled all data (X).
An S4 class to store the preprocessed data and its associated parameters.
dataData frame containing the preprocessed data.
params'feature_params' object storing the parameters used in preprocessing.
An S4 class to store the parameters used in the preprocessing pipeline.
id_colCharacter vector specifying the name of the ID column.
target_colCharacter vector specifying the name of the target variable column.
split_colCharacter vector specifying the name of the column used for train/test split.
scale_optionCharacter string specifying the scaling option ("train", "test", "all").
scale_methodCharacter string specifying the scaling method ("scale", "norm").
mean_valsNumeric vector storing the mean values for z-score scaling.
sd_valsNumeric vector storing the standard deviation values for z-score scaling.
min_valsNumeric vector storing the minimum values for minmax normalization.
max_valsNumeric vector storing the maximum values for minmax normalization.
t_mean_valsNumeric vector storing the mean of transformed values for z-score scaling.
t_sd_valsNumeric vector storing the standard deviation of transformed values for z-score scaling.
t_min_valsNumeric vector storing the minimum values of transformed for minmax normalization.
t_max_valsNumeric vector storing the maximum values of transformed for minmax normalization.
fun_transformList of functions for data transformation.
fun_inverseList of functions for inverse data transformation.
transformedBoolean value, 'TRUE' if object contains transformed values, 'NA' if it is the result of an inverse transformation.
Access complete data set from the preprocessed data.
get_data(object, ...) ## S4 method for signature 'feature_data' get_data( object, prep = c("both", "scale", "transform", "none"), as_matrix = TRUE )get_data(object, ...) ## S4 method for signature 'feature_data' get_data( object, prep = c("both", "scale", "transform", "none"), as_matrix = TRUE )
object |
A 'feature_data' object. |
... |
Additional arguments (currently not used). |
prep |
Character argument if transformed data ("transform"), scaled data ("scale" ), transformed and scaled data ("both") or original raw data ("none") will be returned. |
as_matrix |
logical TRUE if the function should return matrix, or a data frame or tibble otherwise. |
A data frame or matrix containing all data, including 'id_col' and 'split_col'.
get_data(feature_data): Method for extracting all data.
Extracts ID columns (ID) from the preprocessed data.
get_id_all(object, ...) ## S4 method for signature 'feature_data' get_id_all(object, as_matrix = TRUE)get_id_all(object, ...) ## S4 method for signature 'feature_data' get_id_all(object, as_matrix = TRUE)
object |
A 'feature_data' object. |
... |
Additional arguments (currently not used). |
as_matrix |
logical TRUE if the function should return matrix, or a data frame or tibble otherwise. |
A data frame or matrix containing the ID column(s).
get_id_all(feature_data): Method extracting id columns for all data
Extracts ID columns (ID) from the preprocessed data.
get_id_test(object, ...) ## S4 method for signature 'feature_data' get_id_test(object, as_matrix = TRUE)get_id_test(object, ...) ## S4 method for signature 'feature_data' get_id_test(object, as_matrix = TRUE)
object |
A 'feature_data' object. |
... |
Additional arguments (currently not used). |
as_matrix |
logical TRUE if the function should return matrix, or a data frame or tibble otherwise. |
A data frame or matrix containing the ID column(s).
get_id_test(feature_data): Method for extracting id columns for the test data
Extracts ID columns (ID) from the preprocessed data.
get_id_train(object, ...) ## S4 method for signature 'feature_data' get_id_train(object, as_matrix = TRUE)get_id_train(object, ...) ## S4 method for signature 'feature_data' get_id_train(object, as_matrix = TRUE)
object |
A 'feature_data' object. |
... |
Additional arguments (currently not used). |
as_matrix |
logical TRUE if the function should return matrix, or a data frame or tibble otherwise. |
A data frame or matrix containing the ID column(s).
get_id_train(feature_data): Method for extracting id columns for the training data
Extracts all the data (X) from the preprocessed data.
get_x_all(object, ...) ## S4 method for signature 'feature_data' get_x_all( object, prep = c("both", "scale", "transform", "none"), as_matrix = TRUE )get_x_all(object, ...) ## S4 method for signature 'feature_data' get_x_all( object, prep = c("both", "scale", "transform", "none"), as_matrix = TRUE )
object |
A 'feature_data' object. |
... |
Additional arguments (currently not used). |
prep |
Character argument if transformed data ("transform"), scaled data ("scale" ), transformed and scaled data ("both") or original raw data ("none") will be returned. |
as_matrix |
logical TRUE if the function should return matrix, or a data frame or tibble otherwise. |
A data frame or matrix containing all the data (X).
get_x_all(feature_data): Method for extracting all data (X).
Extracts the test data (X) from the preprocessed data.
get_x_test(object, ...) ## S4 method for signature 'feature_data' get_x_test( object, prep = c("both", "scale", "transform", "none"), as_matrix = TRUE )get_x_test(object, ...) ## S4 method for signature 'feature_data' get_x_test( object, prep = c("both", "scale", "transform", "none"), as_matrix = TRUE )
object |
A 'feature_data' object. |
... |
Additional arguments (currently not used). |
prep |
Character argument if transformed data ("transform"), scaled data ("scale" ), transformed and scaled data ("both") or original raw data ("none") will be returned. |
as_matrix |
logical TRUE if the function should return matrix, or a data frame or tibble otherwise. |
A data frame or matrix containing the test data (X).
get_x_test(feature_data): Method for extracting test data (X).
Extracts the training data (X) from the preprocessed data.
get_x_train(object, ...) ## S4 method for signature 'feature_data' get_x_train( object, prep = c("both", "scale", "transform", "none"), as_matrix = TRUE )get_x_train(object, ...) ## S4 method for signature 'feature_data' get_x_train( object, prep = c("both", "scale", "transform", "none"), as_matrix = TRUE )
object |
A 'feature_data' object. |
... |
Additional arguments (currently not used). |
prep |
Character argument if transformed data ("transform"), scaled data ("scale" ), transformed and scaled data ("both") or original raw data ("none") will be returned. |
as_matrix |
logical TRUE if the function should return matrix, or a data frame or tibble otherwise. |
A data frame or matrix containing the training data (X).
get_x_train(feature_data): Method for extracting training data (X).
Extracts all the data (Y) from the preprocessed data.
get_y_all(object, ...) ## S4 method for signature 'feature_data' get_y_all( object, prep = c("both", "scale", "transform", "none"), as_matrix = TRUE )get_y_all(object, ...) ## S4 method for signature 'feature_data' get_y_all( object, prep = c("both", "scale", "transform", "none"), as_matrix = TRUE )
object |
A 'feature_data' object. |
... |
Additional arguments (currently not used). |
prep |
Character argument if transformed data ("transform"), scaled data ("scale" ), transformed and scaled data ("both") or original raw data ("none") will be returned. |
as_matrix |
logical TRUE if the function should return matrix, or a data frame or tibble otherwise. |
A data frame or matrix containing all the data (Y).
get_y_all(feature_data): Method for extracting all data (Y).
Extracts the test data (Y) from the preprocessed data.
get_y_test(object, ...) ## S4 method for signature 'feature_data' get_y_test( object, prep = c("both", "scale", "transform", "none"), as_matrix = TRUE )get_y_test(object, ...) ## S4 method for signature 'feature_data' get_y_test( object, prep = c("both", "scale", "transform", "none"), as_matrix = TRUE )
object |
A 'feature_data' object. |
... |
Additional arguments (currently not used). |
prep |
Character argument if transformed data ("transform"), scaled data ("scale" ), transformed and scaled data ("both") or original raw data ("none") will be returned. |
as_matrix |
logical TRUE if the function should return matrix, or a data frame or tibble otherwise. |
A data frame or matrix containing the test data (Y).
get_y_test(feature_data): Method for extracting test data (Y).
Extracts the training data (Y) from the preprocessed data.
get_y_train(object, ...) ## S4 method for signature 'feature_data' get_y_train( object, prep = c("both", "scale", "transform", "none"), as_matrix = TRUE )get_y_train(object, ...) ## S4 method for signature 'feature_data' get_y_train( object, prep = c("both", "scale", "transform", "none"), as_matrix = TRUE )
object |
A 'feature_data' object. |
... |
Additional arguments (currently not used). |
prep |
Character argument if transformed data ("transform"), scaled data ("scale" ), transformed and scaled data ("both") or original raw data ("none") will be returned. |
as_matrix |
logical TRUE if the function should return matrix, or a data frame or tibble otherwise. |
A data frame or matrix containing the training data (Y).
get_y_train(feature_data): Method for extracting training data (Y).
Applies the inverse transformation to the data.
inv_transform_data(object, params, funs, ...) ## S4 method for signature 'feature_data,ANY,ANY' inv_transform_data(object, params, funs, ...) ## S4 method for signature 'feature_data,missing,list' inv_transform_data(object, params, funs, ...) ## S4 method for signature 'data.frame,feature_params,missing' inv_transform_data(object, params, funs, ...)inv_transform_data(object, params, funs, ...) ## S4 method for signature 'feature_data,ANY,ANY' inv_transform_data(object, params, funs, ...) ## S4 method for signature 'feature_data,missing,list' inv_transform_data(object, params, funs, ...) ## S4 method for signature 'data.frame,feature_params,missing' inv_transform_data(object, params, funs, ...)
object |
A 'feature_data' object. |
params |
A 'feature_params' object with transformation and scaling parameters |
funs |
A list of inverse data transformation functions. |
... |
Additional arguments (currently not used). |
A 'feature_data' object or a 'data.frame' with the inverse transformed data.
inv_transform_data(object = feature_data, params = ANY, funs = ANY): Method for inverse transforming data in a 'feature_data' object.
inv_transform_data(object = feature_data, params = missing, funs = list): Method for transforming data in a 'feature_data' object and user-defined functions (deprecated).
inv_transform_data(
object = data.frame,
params = feature_params,
funs = missing
): Method for a 'data frame'.
Calculates goodness-of-fit metrics (MSE, RMSE, MAE, R2) on preprocessed data, supporting fine-grained control over feature and target preprocessing states.
ml_evaluate(object, ...) ## S4 method for signature 'feature_data' ml_evaluate( object, model, subset = c("all", "test", "train"), xprep = c("both", "scale", "transform", "none"), yprep = c("both", "scale", "transform", "none"), to_original_scale = FALSE, ... )ml_evaluate(object, ...) ## S4 method for signature 'feature_data' ml_evaluate( object, model, subset = c("all", "test", "train"), xprep = c("both", "scale", "transform", "none"), yprep = c("both", "scale", "transform", "none"), to_original_scale = FALSE, ... )
object |
A 'feature_data' object. |
... |
Additional arguments passed to 'predict()'. |
model |
A fitted machine learning model (e.g., Keras, nnet, lm). |
subset |
Data subset to evaluate ('"all"', '"train"', or '"test"'). |
xprep |
Preprocessing state for predictor features ('"both"', '"scale"', '"transform"', '"none"'). |
yprep |
Preprocessing state for target variable ('"both"', '"scale"', '"transform"', '"none"'). |
to_original_scale |
Logical; if 'TRUE', evaluates performance on original physical units (overrides 'yprep' to '"none"' for target comparison). |
Bias (Mean Error) is calculated as mean(observed - predicted). Positive values indicate systematic underprediction by the model.
A named numeric vector of evaluation metrics (MSE, RMSE, MAE, R2).
ml_evaluate(feature_data): Evaluate performance metrics for a 'feature_data' object.
Makes predictions from a machine learning model with a 'feature_data' object.
## S4 method for signature 'feature_data' predict( object, model, subset = c("all", "test", "train"), xprep = c("both", "scale", "transform", "none"), yprep = c("both", "scale", "transform", "none"), to_original_scale = FALSE, as_matrix = TRUE, ... )## S4 method for signature 'feature_data' predict( object, model, subset = c("all", "test", "train"), xprep = c("both", "scale", "transform", "none"), yprep = c("both", "scale", "transform", "none"), to_original_scale = FALSE, as_matrix = TRUE, ... )
object |
A 'feature_data' object. |
model |
A fitted model object. |
subset |
Subset of the 'preproc' data set. |
xprep |
Character argument if transformed data ("transform"), scaled data ("scale" ), transformed and scaled data ("both") or original raw data ("none") are used for x. |
yprep |
Character argument if transformed data ("transform"), scaled data ("scale" ), transformed and scaled data ("both") or original raw data ("none") are used for y. |
to_original_scale |
Logical value to force re-transformation of predictions to the original scale. |
as_matrix |
logical TRUE if the function should return matrix, or a data frame or tibble otherwise. |
... |
Additional arguments (currently not used). |
Currently only certain Keras models can be used as fitted models.
A matrix or vector with the predictions.
Extract residuals from a machine learning model compared to a 'feature_data' object.
## S4 method for signature 'feature_data' residuals( object, model, subset = c("all", "test", "train"), xprep = c("both", "scale", "transform", "none"), yprep = c("both", "scale", "transform", "none"), to_original_scale = FALSE, as_matrix = TRUE, ... )## S4 method for signature 'feature_data' residuals( object, model, subset = c("all", "test", "train"), xprep = c("both", "scale", "transform", "none"), yprep = c("both", "scale", "transform", "none"), to_original_scale = FALSE, as_matrix = TRUE, ... )
object |
A 'feature_data' object. |
model |
A fitted model object. |
subset |
Subset of the 'preproc' data set. |
xprep |
Character argument if transformed data ("transform"), scaled data ("scale" ), transformed and scaled data ("both") or original raw data ("none") are used for x. |
yprep |
Character argument if transformed data ("transform"), scaled data ("scale" ), transformed and scaled data ("both") or original raw data ("none") are used for y. |
to_original_scale |
Logical value to force re-transformation of predictions to the original scale. |
as_matrix |
logical TRUE if the function should return matrix, or a data frame or tibble otherwise. |
... |
Additional arguments (currently not used). |
Currently only certain Keras models can be used as fitted models.
A matrix or vector with the residuals.
Estimates the nonlinear R2 from a machine learning model compared to a 'feature_data' object.
rsquared(object, ...) ## S4 method for signature 'feature_data' rsquared( object, model, subset = c("all", "test", "train"), prep = c("both", "scale", "transform", "none"), ... )rsquared(object, ...) ## S4 method for signature 'feature_data' rsquared( object, model, subset = c("all", "test", "train"), prep = c("both", "scale", "transform", "none"), ... )
object |
A 'feature_data' object. |
... |
Additional arguments passed down to 'predict()'. |
model |
A fitted model object. |
subset |
Subset of the 'preproc' dataset ('"all"', '"train"', or '"test"'). |
prep |
Character argument if transformed data ("transform"), scaled data ("scale"), transformed and scaled data ("both"), or original raw data ("none") will be returned. |
The coefficient of determination as an un-named scalar numeric value.
rsquared(feature_data): Method for estimating the coefficient of determination of
a model for a 'feature_data' object.
Scales the data using the stored parameters.
scale_data(object, params, ...) ## S4 method for signature 'feature_data,missing' scale_data(object, params, ...) ## S4 method for signature 'data.frame,feature_params' scale_data(object, params, transformed = FALSE, ...)scale_data(object, params, ...) ## S4 method for signature 'feature_data,missing' scale_data(object, params, ...) ## S4 method for signature 'data.frame,feature_params' scale_data(object, params, transformed = FALSE, ...)
object |
A 'feature_data' object or a data frame. |
params |
n 'feature_params' object with scaling parameters |
... |
Additional arguments (currently not used). |
transformed |
'TRUE' to use parameters of transformed data. |
A 'feature_data' object with the scaled data.
scale_data(object = feature_data, params = missing): Method for scaling data in a 'feature_data' object.
scale_data(object = data.frame, params = feature_params): Method for scaling data in a 'feature_data' object.
Sets the transformation functions for a 'feature_data' object.
set_transformations(object, fun_transform = NULL, fun_inverse = NULL) ## S4 method for signature 'feature_data' set_transformations(object, fun_transform = NULL, fun_inverse = NULL)set_transformations(object, fun_transform = NULL, fun_inverse = NULL) ## S4 method for signature 'feature_data' set_transformations(object, fun_transform = NULL, fun_inverse = NULL)
object |
A 'feature_data' object. |
fun_transform |
A named list of functions to apply to the data. T he names should correspond to the columns, and the values should be the functions. |
fun_inverse |
A named list of functions representing the inverse transformations. The structure should be the same as 'fun_transform'. |
A 'feature_data' object with the updated transformation functions.
set_transformations(feature_data): Method for setting transformations in a 'feature_data' object.
Transforms the data using the provided functions.
transform_data(object, params, funs, ...) ## S4 method for signature 'feature_data,ANY,ANY' transform_data(object, params, funs, ...) ## S4 method for signature 'feature_data,missing,list' transform_data(object, params, funs, ...) ## S4 method for signature 'data.frame,feature_params,missing' transform_data(object, params, funs, ...) ## S4 method for signature 'data.frame,missing,list' transform_data(object, params, funs, ...)transform_data(object, params, funs, ...) ## S4 method for signature 'feature_data,ANY,ANY' transform_data(object, params, funs, ...) ## S4 method for signature 'feature_data,missing,list' transform_data(object, params, funs, ...) ## S4 method for signature 'data.frame,feature_params,missing' transform_data(object, params, funs, ...) ## S4 method for signature 'data.frame,missing,list' transform_data(object, params, funs, ...)
object |
A 'feature_data' object or a data frame. |
params |
A 'feature_params' object with transformation and scaling parameters |
funs |
A list of data transformation functions . |
... |
Additional arguments (currently not used). |
A 'feature_data' object or a 'data.frame' with the transformed data.
transform_data(object = feature_data, params = ANY, funs = ANY): Method for transforming data in a 'feature_data' object.
transform_data(object = feature_data, params = missing, funs = list): Method for transforming data in a 'feature_data' object and user-defined functions (deprecated).
transform_data(object = data.frame, params = feature_params, funs = missing): Method for a 'data frame'.
transform_data(object = data.frame, params = missing, funs = list): Method for a 'data frame'.