Title: | A Framework for Creating, Managing and Training Multiple Caret Models |
---|---|
Description: | A minimalistic but flexible framework that facilitates the creation, management and training of multiple 'caret' models. A model grid consists of two components: (1) a set of settings that is shared by all models by default, and (2) specifications that apply only to the individual models. When the model grid is trained, model and training specifications are first consolidated from the shared and the model specific settings into complete 'caret' model configurations. These models are then trained with the 'train' function from the 'caret' package. |
Authors: | Lars Kjeldgaard [aut, cre] |
Maintainer: | Lars Kjeldgaard <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.1.1.0 |
Built: | 2025-03-04 04:19:16 UTC |
Source: | https://github.com/smaakage85/modelgrid |
Define and add an individual model (and model training) specification to an existing model grid.
add_model(model_grid, model_name = NULL, custom_control = NULL, ...)
add_model(model_grid, model_name = NULL, custom_control = NULL, ...)
model_grid |
|
model_name |
|
custom_control |
|
... |
All (optional) individual settings (including model training settings) that the user wishes to set for the new model. |
model_grid
with an additional individual model specification.
library(magrittr) # Pre-allocate empty model grid. mg <- model_grid() # Add 'random forest' model spec. mg <- mg %>% add_model(model_name = "Random Forest Test", method = "rf", tuneLength = 5)
library(magrittr) # Pre-allocate empty model grid. mg <- model_grid() # Add 'random forest' model spec. mg <- mg %>% add_model(model_name = "Random Forest Test", method = "rf", tuneLength = 5)
Consolidate model (and model training) settings from shared and model specific settings to one complete caret model specification. In case there is an overlap between the two, the model specific settings will apply.
consolidate_model(shared_settings, model)
consolidate_model(shared_settings, model)
shared_settings |
|
model |
|
list
, a complete model and training specification, that
can be trained with caret.
library(magrittr) library(dplyr) library(caret) # create model grid. mg <- model_grid() %>% share_settings(y = iris[["Species"]], x = iris %>% select(-Species), trControl = trainControl()) %>% add_model("FunkyForest", method = "rf", preProc = c("center", "scale", "pca"), custom_control = list(preProcOptions = list(thresh = 0.8))) # consolidate all settings to complete caret model specification. consolidate_model(mg$shared_settings, mg$models$FunkyForest)
library(magrittr) library(dplyr) library(caret) # create model grid. mg <- model_grid() %>% share_settings(y = iris[["Species"]], x = iris %>% select(-Species), trControl = trainControl()) %>% add_model("FunkyForest", method = "rf", preProc = c("center", "scale", "pca"), custom_control = list(preProcOptions = list(thresh = 0.8))) # consolidate all settings to complete caret model specification. consolidate_model(mg$shared_settings, mg$models$FunkyForest)
Modify an existing model (and training) specification in a model grid.
edit_model(model_grid, model_name, ...)
edit_model(model_grid, model_name, ...)
model_grid |
|
model_name |
|
... |
All the model (and model training) settings you want to modify for an existing model specification. |
model_grid
library(magrittr) # Create model grid and add random forest model. mg <- model_grid() %>% add_model(model_name = "Random Forest Test", method = "rf", tuneLength = 5) # Edit the size of tuning grid of the random forest model. edit_model(mg, model_name = "Random Forest Test", tuneLength = 10)
library(magrittr) # Create model grid and add random forest model. mg <- model_grid() %>% add_model(model_name = "Random Forest Test", method = "rf", tuneLength = 5) # Edit the size of tuning grid of the random forest model. edit_model(mg, model_name = "Random Forest Test", tuneLength = 10)
Constructor function that pre-allocates an empty model grid.
The model grid makes it easy to create, manage and train multiple caret models.
Define the settings that by default are to be shared by all of the models in
the model grid with share_settings
. Add the individual
specifications for the models you want to investigate with add_model
.
Train all of the models in the model grid with train
.
The S3 method of the train function for the 'model_grid' class consolidates all model (and training) configurations from a model grid and trains them with the train function from the caret package.
model_grid() ## S3 method for class 'model_grid' train(mg, train_all = FALSE, resample_seed = 123)
model_grid() ## S3 method for class 'model_grid' train(mg, train_all = FALSE, resample_seed = 123)
mg |
|
train_all |
|
resample_seed |
|
model_grid
model_grid
equipped with fitted models.
add_model
for how to add a model to a model grid,
edit_model
for how to edit an existing model within a model grid,
share_settings
for how to define the shared settings of models
within a model grid, consolidate_model
for how to consolidate
the shared settings of a model grid and the individual settings of a given
model into one complete caret model configuration and
remove_model
for how to remove a model from a model grid.
# Pre-allocate an empty model grid. model_grid() library(caret) library(magrittr) library(dplyr) data(GermanCredit) # Create model grid with two different Random Forest models. mg <- model_grid() %>% share_settings( y = GermanCredit[["Class"]], x = GermanCredit %>% select(-Class), metric = "ROC", trControl = trainControl( method = "cv", number = 2, summaryFunction = twoClassSummary, classProbs = TRUE ) ) %>% add_model( model_name = "RF", method = "rf", tuneLength = 3 ) %>% add_model( model_name = "RF NZV", method = "rf", preProc = "nzv", tuneGrid = data.frame(mtry = c(2, 10)) ) # Train all model configurations in model grid. train(mg)
# Pre-allocate an empty model grid. model_grid() library(caret) library(magrittr) library(dplyr) data(GermanCredit) # Create model grid with two different Random Forest models. mg <- model_grid() %>% share_settings( y = GermanCredit[["Class"]], x = GermanCredit %>% select(-Class), metric = "ROC", trControl = trainControl( method = "cv", number = 2, summaryFunction = twoClassSummary, classProbs = TRUE ) ) %>% add_model( model_name = "RF", method = "rf", tuneLength = 3 ) %>% add_model( model_name = "RF NZV", method = "rf", preProc = "nzv", tuneGrid = data.frame(mtry = c(2, 10)) ) # Train all model configurations in model grid. train(mg)
Removes an individual model specification from a model grid. If the model has been trained, the fitted model will also be deleted.
remove_model(model_grid, model_name)
remove_model(model_grid, model_name)
model_grid |
|
model_name |
|
model_grid
library(magrittr) # Pre-allocate empty model grid. mg <- model_grid() # Add random forest model. mg <- mg %>% add_model(model_name = "Random Forest Test", method = "rf", tuneLength = 5) # Remove random forest model again. remove_model(mg, model_name = "Random Forest Test")
library(magrittr) # Pre-allocate empty model grid. mg <- model_grid() # Add random forest model. mg <- mg %>% add_model(model_name = "Random Forest Test", method = "rf", tuneLength = 5) # Remove random forest model again. remove_model(mg, model_name = "Random Forest Test")