Model Specifications with parsnip
Specify model types and engines independently from the training step.
What Is parsnip?
parsnip provides a unified interface to hundreds of model engines. Instead of learning the unique syntax of each package (glm(), randomForest(), e1071::svm()), you write one consistent specification and tell parsnip which engine to use underneath.
This means you can swap engines — e.g. from glm to stan for Bayesian logistic regression — by changing one argument.
library(parsnip)
# The same interface, different engines
logistic_reg() |> set_engine('glm') # base R
logistic_reg() |> set_engine('glmnet') # regularized
logistic_reg() |> set_engine('stan') # Bayesianlogistic_reg() for Classification
logistic_reg() specifies a logistic regression model for binary classification. Chain set_engine('glm') to use R's built-in GLM engine, and set_mode('classification') to explicitly declare the task type.
The mode ('classification' or 'regression') is required for model types that support both.
log_spec <- logistic_reg() |>
set_engine('glm') |>
set_mode('classification')
print(log_spec)
# Logistic Regression Model Specification (classification)
# Computational engine: glmAll lessons in this course
- Feature Engineering with recipes
- Model Specifications with parsnip
- Workflows: Combining Recipes and Models
- Resampling and Cross-Validation with rsample