SuperLearner R Package

repository·master·Indexed 43 days ago

https://github.com/ecpolley/superlearner

An R package for optimal prediction model ensembling that uses cross-validation to combine multiple machine learning algorithms to minimize a target loss function. Features include the SuperLearner() function for automatic ensembles, CV.SuperLearner() for unbiased performance estimation via nested cross-validation, and create.Learner() for hyperparameter optimization.

Tokens
993
Snippets
4
Records
4
Agent score
5%

What's inside SuperLearner

  1. Install SuperLearner

    master

    You can install the current stable release from CRAN or the development version directly from GitHub using the remotes package.

    # Install the current release from CRAN
    install.packages("SuperLearner")
    
    # Install the development version from GitHub
    # install.packages("remotes")
    remotes::install_github("ecpolley/SuperLearner")
  2. Run an automatic ensemble with SuperLearner()

    master

    The SuperLearner() function fits multiple algorithms specified in SL.library and creates an optimal predictor ensemble via cross-validation.

    To use it:

    1. Define a vector of algorithm names (e.g., SL.xgboost, SL.glmnet) in SL.library.
    2. Provide the target variable Y and the feature matrix X.
    3. Inspect the result object to review the performance of individual algorithms and the weights assigned to them in the ensemble.
    data(Boston, package = "MASS")
    set.seed(1)
    
    # Define the library of algorithms
    sl_lib = c("SL.xgboost", "SL.randomForest", "SL.glmnet", "SL.nnet", "SL.ksvm",
               "SL.bartMachine", "SL.kernelKnn", "SL.rpartPrune", "SL.lm", "SL.mean")
    
    # Fit algorithms and create automatic ensemble
    result = SuperLearner(Y = Boston$medv, X = Boston[, -14], SL.library = sl_lib)
    
    # Review performance and weights
    result
  3. Perform hyperparameter optimization with create.Learner()

    master

    You can optimize hyperparameters for specific algorithms by using create.Learner(). This allows you to define a grid of parameters to test for a single algorithm and then include those optimized versions in your SL.library.

    For example, to tune the alpha parameter in SL.glmnet (where 0 is ridge and 1 is lasso):

    # Fit elastic net with 5 different alphas: 0, 0.2, 0.4, 0.6, 0.8, 1.0
    enet = create.Learner("SL.glmnet", detailed_names = T,
                          tune = list(alpha = seq(0, 1, length.out = 5)))
    
    # Add the tuned learners to the library
    sl_lib2 = c("SL.mean", "SL.lm", enet$names)
    
    # Run SuperLearner with the tuned library
    enet_sl = SuperLearner(Y = Boston$medv, X = Boston[, -14], SL.library = sl_lib2)
    
    # Identify the best-performing alpha value or use the automatic ensemble
    enet_sl
  4. Estimate ensemble accuracy with CV.SuperLearner()

    master

    To obtain an unbiased estimate of the ensemble's performance, use CV.SuperLearner(). This performs external (nested) cross-validation. Note that this process is computationally intensive and may take a significant amount of time depending on the size of your data and the number of algorithms in SL.library.

    # Use external (aka nested) cross-validation to estimate ensemble accuracy
    result2 = CV.SuperLearner(Y = Boston$medv, X = Boston[, -14], SL.library = sl_lib)
    
    # Plot performance of individual algorithms vs the ensemble
    plot(result2) + theme_minimal()