| Title: | Unified S3 Interface to Machine Learning Models |
|---|---|
| Description: | Provides a unified and consistent S3 interface for training and predicting with a variety of machine learning models in R. The package wraps popular algorithms (e.g., from 'glmnet', 'lightgbm', 'ranger', 'e1071', and 'caret') under a common workflow based on simple wrap_*() and predict() functions, allowing users to switch between models without changing their code structure. It supports both classification and regression tasks and facilitates rapid experimentation, benchmarking, and comparison of models. By abstracting away package-specific APIs while preserving flexibility in parameter specification, the package streamlines machine learning workflows and promotes reproducibility. |
| Authors: | T. Moudiki [aut, cre] |
| Maintainer: | T. Moudiki <[email protected]> |
| License: | GPL-3 |
| Version: | 0.1.1 |
| Built: | 2026-05-27 22:36:14 UTC |
| Source: | https://github.com/Techtonique/mlS3 |
Predict method for mlS3 caret wrapper
## S3 method for class 'wrap_caret' predict(object, newx, type = NULL, ...)## S3 method for class 'wrap_caret' predict(object, newx, type = NULL, ...)
object |
Object from wrap_caret |
newx |
New features (matrix or data frame) |
type |
Prediction type: "raw" (default), "class", "prob", or NULL |
... |
Additional arguments to caret::predict.train |
Vector or matrix of predictions
# Only runs if caret is installed data(mtcars) # Prepare data X_reg <- mtcars[, -1] # All except mpg y_reg <- mtcars$mpg # Target variable # Split into train/test set.seed(123) idx_reg <- sample(nrow(X_reg), 0.7 * nrow(X_reg)) X_train <- X_reg[idx_reg, ] y_train <- y_reg[idx_reg] X_test <- X_reg[-idx_reg, ] y_test <- y_reg[-idx_reg] mod <- wrap_caret(X_train, y_train, method = "rf", mtry = 3) (pred <- predict(mod, X_test))# Only runs if caret is installed data(mtcars) # Prepare data X_reg <- mtcars[, -1] # All except mpg y_reg <- mtcars$mpg # Target variable # Split into train/test set.seed(123) idx_reg <- sample(nrow(X_reg), 0.7 * nrow(X_reg)) X_train <- X_reg[idx_reg, ] y_train <- y_reg[idx_reg] X_test <- X_reg[-idx_reg, ] y_test <- y_reg[-idx_reg] mod <- wrap_caret(X_train, y_train, method = "rf", mtry = 3) (pred <- predict(mod, X_test))
Print method for wrap_caret objects
## S3 method for class 'wrap_caret' print(x, ...)## S3 method for class 'wrap_caret' print(x, ...)
x |
Object from wrap_caret |
... |
Additional arguments |
Minimal wrapper around caret::train with no tuning. Hyperparameters can be passed via ... as named arguments.
wrap_caret(x, y, method = "rf", ...)wrap_caret(x, y, method = "rf", ...)
x |
Feature matrix or data frame |
y |
Response vector |
method |
caret model method (default "rf") |
... |
Named hyperparameters (e.g., mtry = 3, ntree = 500) |
Object with class "mlS3_caret"
# Only runs if caret is installed data(mtcars) # Prepare data X_reg <- mtcars[, -1] # All except mpg y_reg <- mtcars$mpg # Target variable # Split into train/test set.seed(123) idx_reg <- sample(nrow(X_reg), 0.7 * nrow(X_reg)) X_train <- X_reg[idx_reg, ] y_train <- y_reg[idx_reg] X_test <- X_reg[-idx_reg, ] y_test <- y_reg[-idx_reg] mod <- wrap_caret(X_train, y_train, method = "rf", mtry = 3) (pred <- predict(mod, X_test))# Only runs if caret is installed data(mtcars) # Prepare data X_reg <- mtcars[, -1] # All except mpg y_reg <- mtcars$mpg # Target variable # Split into train/test set.seed(123) idx_reg <- sample(nrow(X_reg), 0.7 * nrow(X_reg)) X_train <- X_reg[idx_reg, ] y_train <- y_reg[idx_reg] X_test <- X_reg[-idx_reg, ] y_test <- y_reg[-idx_reg] mod <- wrap_caret(X_train, y_train, method = "rf", mtry = 3) (pred <- predict(mod, X_test))
Fits a 'glmnet' penalized regression model with a consistent interface. Supports regression and binary classification.
wrap_glmnet(x, y, ...) ## S3 method for class 'wrap_glmnet' predict(object, newx, type = c("class", "prob"), s = NULL, ...) ## S3 method for class 'wrap_glmnet' print(x, ...)wrap_glmnet(x, y, ...) ## S3 method for class 'wrap_glmnet' predict(object, newx, type = c("class", "prob"), s = NULL, ...) ## S3 method for class 'wrap_glmnet' print(x, ...)
x |
A matrix or data.frame of features. |
y |
A factor or character vector for classification, numeric for regression. |
... |
Additional arguments passed to [glmnet::glmnet()]. Pass 'family = "binomial"' for binary classification. |
object |
A fitted 'wrap_glmnet' object. |
newx |
A matrix or data.frame of new observations. |
type |
'"class"' (default) for class labels, '"prob"' for a probability matrix. Ignored for regression. |
s |
Lambda value for prediction. Defaults to the midpoint of the lambda path. Pass 's = cv_fit$lambda.min' if using [glmnet::cv.glmnet()]. |
An object of class 'wrap_glmnet' with fields:
fit |
The fitted glmnet model. |
levels |
Class levels (NULL for regression). |
task |
"classification" or "regression". |
Multiclass ('family = "multinomial"') is not yet supported. For lambda selection, a specific 's' value can be passed to 'predict()'. By default the midpoint of the lambda path is used. For optimal lambda, use [glmnet::cv.glmnet()] externally and pass 's = fit$lambda.min'.
X <- iris[iris$Species != "virginica", 1:4] y <- droplevels(iris[iris$Species != "virginica", "Species"]) mod <- wrap_glmnet(X, y, family = "binomial") predict(mod, newx = X, type = "class") predict(mod, newx = X, type = "prob") X <- iris[iris$Species != "virginica", 1:4] y <- droplevels(iris[iris$Species != "virginica", "Species"]) mod <- wrap_glmnet(X, y, family = "binomial") predict(mod, newx = X, type = "class") predict(mod, newx = X, type = "prob")X <- iris[iris$Species != "virginica", 1:4] y <- droplevels(iris[iris$Species != "virginica", "Species"]) mod <- wrap_glmnet(X, y, family = "binomial") predict(mod, newx = X, type = "class") predict(mod, newx = X, type = "prob") X <- iris[iris$Species != "virginica", 1:4] y <- droplevels(iris[iris$Species != "virginica", "Species"]) mod <- wrap_glmnet(X, y, family = "binomial") predict(mod, newx = X, type = "class") predict(mod, newx = X, type = "prob")
Fits a 'lightgbm' model with a consistent interface. Supports binary classification, multiclass classification, and regression.
wrap_lightgbm(x, y, ...) ## S3 method for class 'wrap_lightgbm' predict(object, newx, type = c("class", "prob"), ...) ## S3 method for class 'wrap_lightgbm' print(x, ...)wrap_lightgbm(x, y, ...) ## S3 method for class 'wrap_lightgbm' predict(object, newx, type = c("class", "prob"), ...) ## S3 method for class 'wrap_lightgbm' print(x, ...)
x |
A matrix or data.frame of features. |
y |
A factor or character vector for classification, numeric for regression. |
... |
Additional arguments passed to [lightgbm::lgb.train()]. Pass 'params = list(objective = "binary")' for binary classification, 'params = list(objective = "multiclass", num_class = k)' for multiclass, or 'params = list(objective = "regression")' for regression. |
object |
A fitted 'wrap_lightgbm' object. |
newx |
A matrix or data.frame of new observations. |
type |
'"class"' (default) for class labels, '"prob"' for a probability matrix. Ignored for regression. |
An object of class 'wrap_lightgbm' with fields:
fit |
The fitted lgb.Booster model. |
levels |
Class levels (NULL for regression). |
task |
"classification" or "regression". |
objective |
The lightgbm objective string, stored at fit time. |
## Not run: library(mlS3) X <- iris[, 1:4] y <- iris$Species mod <- wrap_lightgbm(X, y, params = list(objective = "multiclass", num_class = 3, verbose = -1), nrounds = 50) predict(mod, newx = X, type = "class") predict(mod, newx = X, type = "prob") ## End(Not run) ## Not run: library(mlS3) X <- iris[, 1:4] y <- iris$Species mod <- wrap_lightgbm(X, y, params = list(objective = "multiclass", num_class = 3, verbose = -1), nrounds = 50) predict(mod, newx = X, type = "class") predict(mod, newx = X, type = "prob") ## End(Not run)## Not run: library(mlS3) X <- iris[, 1:4] y <- iris$Species mod <- wrap_lightgbm(X, y, params = list(objective = "multiclass", num_class = 3, verbose = -1), nrounds = 50) predict(mod, newx = X, type = "class") predict(mod, newx = X, type = "prob") ## End(Not run) ## Not run: library(mlS3) X <- iris[, 1:4] y <- iris$Species mod <- wrap_lightgbm(X, y, params = list(objective = "multiclass", num_class = 3, verbose = -1), nrounds = 50) predict(mod, newx = X, type = "class") predict(mod, newx = X, type = "prob") ## End(Not run)
Fits a 'ranger' random forest with a consistent interface. Supports both classification (factor 'y') and regression (numeric 'y').
wrap_ranger(x, y, ...) ## S3 method for class 'wrap_ranger' predict(object, newx, type = c("class", "prob"), ...) ## S3 method for class 'wrap_ranger' print(x, ...)wrap_ranger(x, y, ...) ## S3 method for class 'wrap_ranger' predict(object, newx, type = c("class", "prob"), ...) ## S3 method for class 'wrap_ranger' print(x, ...)
x |
A matrix or data.frame of features. |
y |
A factor or character vector for classification, numeric for regression. |
... |
Additional arguments passed to [ranger::ranger()]. |
object |
A fitted 'wrap_ranger' object. |
newx |
A matrix or data.frame of new observations. |
type |
'"class"' (default) for class labels, '"prob"' for a probability matrix. Ignored for regression. |
An object of class 'wrap_ranger' with fields:
fit |
The fitted ranger model. |
levels |
Class levels (NULL for regression). |
task |
"classification" or "regression". |
X <- as.matrix(iris[, 1:4]) y <- iris$Species mod <- wrap_ranger(X, y, num.trees = 100L) predict(mod, newx = X, type = "class") predict(mod, newx = X, type = "prob") X <- as.matrix(iris[, 1:4]) y <- iris$Species mod <- wrap_ranger(X, y, num.trees = 100L) predict(mod, newx = X, type = "class") predict(mod, newx = X, type = "prob")X <- as.matrix(iris[, 1:4]) y <- iris$Species mod <- wrap_ranger(X, y, num.trees = 100L) predict(mod, newx = X, type = "class") predict(mod, newx = X, type = "prob") X <- as.matrix(iris[, 1:4]) y <- iris$Species mod <- wrap_ranger(X, y, num.trees = 100L) predict(mod, newx = X, type = "class") predict(mod, newx = X, type = "prob")
Fits an 'e1071' support vector machine with a consistent interface. Supports classification and regression.
wrap_svm(x, y, ...) ## S3 method for class 'wrap_svm' predict(object, newx, type = c("class", "prob"), ...) ## S3 method for class 'wrap_svm' print(x, ...)wrap_svm(x, y, ...) ## S3 method for class 'wrap_svm' predict(object, newx, type = c("class", "prob"), ...) ## S3 method for class 'wrap_svm' print(x, ...)
x |
A matrix or data.frame of features. |
y |
A factor or character vector for classification, numeric for regression. |
... |
Additional arguments passed to [e1071::svm()]. 'probability = TRUE' is set automatically for classification; do not override this if you need 'type = "prob"' predictions. |
object |
A fitted 'wrap_svm' object. |
newx |
A matrix or data.frame of new observations. |
type |
'"class"' (default) for class labels, '"prob"' for a probability matrix. Ignored for regression. |
An object of class 'wrap_svm' with fields:
fit |
The fitted svm model. |
levels |
Class levels (NULL for regression). |
task |
"classification" or "regression". |
X <- as.matrix(iris[, 1:4]) y <- iris$Species mod <- wrap_svm(X, y, kernel = "radial") predict(mod, newx = X, type = "class") predict(mod, newx = X, type = "prob") X <- as.matrix(iris[, 1:4]) y <- iris$Species mod <- wrap_svm(X, y, kernel = "radial") predict(mod, newx = X, type = "class") predict(mod, newx = X, type = "prob")X <- as.matrix(iris[, 1:4]) y <- iris$Species mod <- wrap_svm(X, y, kernel = "radial") predict(mod, newx = X, type = "class") predict(mod, newx = X, type = "prob") X <- as.matrix(iris[, 1:4]) y <- iris$Species mod <- wrap_svm(X, y, kernel = "radial") predict(mod, newx = X, type = "class") predict(mod, newx = X, type = "prob")