Provides a consistent interface for various machine learning models in R, with automatic detection of formula vs matrix interfaces, built-in cross-validation, model interpretability, and visualization.
An R6 class that provides a unified interface for regression and classification models with automatic interface detection, cross-validation, and interpretability features. The task type (regression vs classification) is automatically detected from the response variable type.
model_fnThe modeling function (e.g., glmnet::glmnet, randomForest::randomForest)
fittedThe fitted model object
taskType of task: "regression" or "classification" (automatically detected)
X_trainTraining features matrix
y_trainTraining target vector
fit()Fit the model to training data
Automatically detects task type (regression vs classification) based on the type of the response variable y. Numeric y -> regression, factor y -> classification.
predict()Generate predictions from fitted model
print()Print model information
summary()Compute numerical derivatives and statistical significance
Uses finite differences to compute approximate partial derivatives for each feature, providing model-agnostic interpretability.
hStep size for finite differences (default: 0.01)
alphaSignificance level for p-values (default: 0.05)
plot()Create partial dependence plot for a feature
Visualizes the relationship between a feature and the predicted outcome while holding other features at their mean values.
clone_model()Create a deep copy of the model
Useful for cross-validation and parallel processing where multiple independent model instances are needed.
if (FALSE) { # \dontrun{
# Regression example with glmnet
library(glmnet)
X <- matrix(rnorm(100), ncol = 4)
y <- 2*X[,1] - 1.5*X[,2] + rnorm(25) # numeric → regression
mod <- Model$new(glmnet::glmnet)
mod$fit(X, y, alpha = 0, lambda = 0.1)
mod$summary()
predictions <- mod$predict(X)
# Classification example
data(iris)
iris_binary <- iris[iris$Species %in% c("setosa", "versicolor"), ]
X_class <- as.matrix(iris_binary[, 1:4])
y_class <- iris_binary$Species # factor → classification
mod2 <- Model$new(e1071::svm)
mod2$fit(X_class, y_class, kernel = "radial")
mod2$summary()
# Cross-validation
cv_scores <- cross_val_score(mod, X, y, cv = 5)
} # }