Chapter 6. Fitting Models with parsnip

The parsnip package is one of the R packages that is part of the tidymodels metapackage. It provides a fluent and standardized interface for a variety of different models. In this chapter, we give some motivation for why a common interface is beneficial for understanding and building models in practice and show how to use the parsnip package.

Specifically, we will focus on how to fit() and predict() directly with a parsnip object, which may be a good fit for some straightforward modeling problems. Chapter 7 illustrates a better approach for many modeling tasks by combining models and preprocessors together into something called a workflow object.

Create a Model

Once the data have been encoded in a format ready for a modeling algorithm, such as a numeric matrix, they can be used in the model building process.

Suppose that a linear regression model was our initial choice. This is equivalent to specifying that the outcome data are numeric and that the predictors are related to the outcome in terms of simple slopes and intercepts:

y i = β 0 + β 1 x 1i + ... + β p x pi

A variety of methods can be used to estimate the model parameters:

  • Ordinary linear regression uses the traditional method of least squares to solve for the model parameters.

  • Regularized linear regression adds a penalty to the least squares method to encourage simplicity by removing predictors and/or shrinking their coefficients toward zero. This can be executed ...

Get Tidy Modeling with R now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.