stacks R package

repository·main·Indexed 43 days ago

https://github.com/tidymodels/stacks

An R package for model stacking that integrates with the tidymodels ecosystem. It enables the creation of ensembles by combining predictions from multiple candidate models using a regularized linear model (metalearning) to determine optimal weights. The core workflow involves initializing a data_stack via stacks(), adding candidates with add_candidates(), fitting a meta-learner with blend_predictions(), and producing a final model_stack using fit_members().

Tokens
1K
Snippets
1
Records
6
Agent score
5%

What's inside stacks

  1. Extensibility of stacks

    main

    The stacks package is designed to be generalized and works with the broader tidymodels ecosystem:

    • Model Types: Any model implemented in parsnip or its extension packages.
    • Cross-validation: Any resampling algorithm implemented in rsample or its extension packages.
    • Error Metrics: Any metric function implemented in yardstick or its extension packages used for evaluating the stack and its members.
  2. Core abstractions: Model Definitions, Candidates, and Stacks

    main

    The stacks package uses a specific hierarchy of objects to build an ensemble:

    • Model Definitions: An instance of a minimal workflow containing a parsnip model specification and an optional recipes preprocessor. These specify the form of potential members.
    • Candidate Members: Generated from model definitions and a shared rsample rset. These are the actual models trained on resamples. They are collected in a data_stack object.
    • data_stack: A specialized tibble where columns represent the true outcome and the predictions from each candidate member. It also carries attributes to track the original model definitions.
    • model_stack: The final object produced by fit_members(). It is a list subclass that contains the stacking coefficients and the specific members (those with non-zero coefficients) trained on the full training set. This object is used for final predictions.
  3. How model stacking works in stacks

    main

    Model stacking is an ensemble technique where a meta-learner is trained to combine the predictions of multiple constituent models (called "members").

    In {stacks}, the process follows a specific lifecycle:

    1. Model Definitions: You create instructions using {parsnip}, {recipes}, and {workflows}. Each instruction is a candidate member.
    2. Data Stack Initialization: You initialize a data_stack object using stacks().
    3. Candidate Collation: You add model definitions to the stack using add_candidates(). This function uses a shared resampling object (from {rsample}) to collect predictions from each candidate on an assessment set.
    4. Meta-learner Fitting: You use blend_predictions() to fit an elastic net model on the data stack. This determines the stacking coefficients (weights) for each candidate.
    5. Member Fitting: Candidates with non-zero coefficients are identified as members. You use fit_members() to fit these members on the entire training set, resulting in a model_stack object ready for prediction.
  4. Core workflow for building a model stack

    main

    To build a stacked ensemble model, follow this sequence of functions:

    1. stacks(): Initialize a new data_stack object.
    2. add_candidates(): Iteratively add model definitions to the stack. All definitions must share the same resampling object (e.g., cross-validation folds).
    3. blend_predictions(): Fit the meta-learner (elastic net) to determine the weights (stacking coefficients) for the candidates.
    4. fit_members(): Fit the selected members (those with non-zero coefficients) on the full training set to produce a model_stack object.

    Helper functions:

    • collect_parameters(): View model parameters alongside their calculated stacking coefficients.
    • autoplot(): Generate diagnostic visualizations of the model using {ggplot2}.
  5. Install the stacks package

    main

    You can install the stable version of stacks from CRAN or the development version from GitHub using pak.

    # Install stable version
    install.packages("stacks")
    
    # Install development version
    # install.packages("pak")
    pak::pak("tidymodels/stacks")
  6. Reference of core stacks functions

    main

    The following functions form the primary API for the {stacks} workflow:

    • stacks(): Initializes the data_stack object.
    • add_candidates(): Adds model definitions to the stack and collates assessment set predictions.
    • blend_predictions(): Fits the meta-learner (elastic net) to the data stack.
    • fit_members(): Fits the high-weight candidate members on the full training set to create a model_stack.
    • collect_parameters(): Juxtaposes model parameters with stacking coefficients.
    • autoplot(): S3 method for model diagnostic visualizations.