parsnip

repository·main·Indexed 20 days ago

https://github.com/tidymodels/parsnip

A part of the tidymodels ecosystem that provides a unified, tidy interface for various modeling implementations in R. It decouples model definitions from underlying package-specific arguments using three core components: Model Type (e.g., rand_forest, linear_reg), Model Mode (classification, regression, or censored regression), and Model Engine (the computational implementation, such as ranger, spark, or h2o).

Tokens
72.4K
Snippets
245
Records
483
Agent score
67%

What's inside parsnip

  1. Understand classification probability estimation in svm_rbf

    main

    The kernlab engine does not natively estimate class probabilities. When set_mode("classification") is used with a prediction type of prob, parsnip uses Platt scaling.

    Platt scaling works by fitting an additional model on top of the SVM model to convert decision values into probabilities.

    Important: The Platt scaling process uses random numbers that are not controlled by R's standard random number stream, which may affect reproducibility.

  2. Preprocessing and weights for randomForestSRC

    main

    Preprocessing

    This engine does not require special encoding of predictors. Categorical predictors can be partitioned into groups of factor levels (e.g., {a, c} vs {b, d}) during node splitting. Dummy variables are not required.

    Case Weights

    You can utilize case weights during model fitting. The fit() and fit_xy() functions accept a case_weights argument which expects a vector of weights.

  3. Preprocessing and Case Weights for h2o random forest

    main

    Preprocessing

    No special encoding is required for predictors. Categorical predictors can be partitioned into groups of factor levels during node splitting; dummy variables are not required.

    Case Weights

    This engine supports case weights. The fit() and fit_xy() functions accept a case_weights argument which expects a vector of weights.

  4. Understand the parsnip modeling abstractions

    main

    parsnip uses three core concepts to describe models, which allows for a unified interface across different underlying R packages:

    1. Model Type: Specifies the mathematical structure of the model.
      • Example: linear_reg() for models predicting a numeric outcome using a linear combination of predictors.
    2. Model Mode: Reflects the type of prediction outcome.
      • Values: "classification", "regression", and "censored regression".
    3. Model Engine: A designation of how the model should be fit. This is typically an R package or function name.
      • Example: "ranger" for the ranger package.

    Extension packages can define new engines for existing model types (e.g., the poissonreg package providing engines for poisson_reg()).

  5. Use case weights and sparse data with ranger

    main

    Case Weights

    fit() and fit_xy() accept a case_weights argument. In the ranger engine, these act as sampling weights: observations with larger weights are selected with higher probability during the bootstrap/subsampling process.

    Sparse Data

    This engine supports sparse data inputs, including dgCMatrix (from the Matrix package) and sparse tibbles (from the sparsevctrs package). The engine treats sparse data similarly to dense data, so no manual conversion is necessary.

  6. Use case weights with the `glm` engine

    main

    The glm engine supports case weights via the case_weights argument in fit() and fit_xy().

    Note that stats::glm() interprets weights as follows:

    • Non-NULL weights indicate different dispersions (values are inversely proportional to dispersion).
    • Positive integer weights w_i indicate that each response y_i is the mean of w_i unit-weight observations.
    • For binomial GLMs, prior weights are used to specify the number of trials.
  7. Preprocessing and weights for mboost boost_tree

    main

    Preprocessing

    This engine does not require special encoding. Categorical predictors can be partitioned into groups of factor levels (e.g., {a, c} vs {b, d}) during node splitting. Dummy variables are not required.

    Case weights

    While the underlying implementation allows for case weights during model fitting, case weights are not enabled for this engine in parsnip and are not supported for prediction.

  8. How `mtry` is interpreted in `boost_tree`

    main

    In parsnip, the mtry argument typically represents the count of predictors sampled at each split. However, some engines (like xgboost, xrf, and lightgbm) can interpret mtry as a proportion (a value between 0 and 1).

    To switch from count to proportion for engines that support it, pass counts = FALSE to set_engine(). For the h2o engine, mtry is treated as a count by default.

  9. Preprocessing requirements for h2o logistic regression

    main

    When using the h2o engine for logistic regression, keep the following preprocessing requirements in mind:

    1. Categorical Predictors: Factor/categorical predictors must be converted to numeric values (e.g., dummy or indicator variables). If using the formula method via fit(), parsnip will automatically convert factor columns to indicators.
    2. Scaling: Predictors should be on the same scale. While you can manually center and scale predictors, h2o::h2o.glm() uses standardize = TRUE by default to center and scale all numeric columns.
  10. Enable bagging in lightgbm

    main

    To enable bagging, you must provide a sample_size argument.

    Note that LightGBM requires the bagging_freq argument to be non-zero to actually perform bagging. bonsai automatically sets bagging_freq = 1 if sample_size is not equal to 1. You can override this behavior by manually setting bagging_freq within set_engine().

  11. Use case weights and sparse data with the glmnet engine

    main

    The glmnet engine supports advanced data formats and weighting:

    • Case Weights: You can utilize case weights during model fitting. The fit() and fit_xy() functions accept a case_weights argument which expects a vector of weights.
    • Sparse Data: The engine supports sparse data structures for both fitting and prediction, including dgCMatrix from the Matrix package and sparse tibbles from the sparsevctrs package.
  12. How parsnip model specifications work

    main

    parsnip provides a unified interface to various modeling packages by decoupling the model definition from its implementation. A model specification is composed of three main components:

    1. Type: The general class of model (e.g., rand_forest for random forest).
    2. Mode: The type of problem being solved, such as regression or classification (set via set_mode()).
    3. Engine: The underlying computational implementation or R package (set via set_engine()), such as ranger or spark.

    This abstraction allows you to harmonize argument names (e.g., using trees instead of ntrees or num.trees) and switch between different engines without changing your model definition logic.

    library(parsnip)
    
    # Define a specification
    rand_forest(mtry = 10, trees = 2000) |>
      set_engine("ranger", importance = "impurity") |>
      set_mode("regression")