0Pricing
R Academy · Lesson

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')   # Bayesian

logistic_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: glm

All lessons in this course

  1. Feature Engineering with recipes
  2. Model Specifications with parsnip
  3. Workflows: Combining Recipes and Models
  4. Resampling and Cross-Validation with rsample
← Back to R Academy