♣24♣Towards a Tidy Modelling Cycle with modelr

The package modelr provides a layer around R's base functions that allows not only to work with models using the pipe %>% command, but also provides some functions that are more intuitive to work with. modelr is not part of the core-tidyverse, so, we need to load it separately.

In the next sections, we will use the library modelr to create predictions, perform cross validations, etc. While it is possible to learn it as you read through the next chapters, it is also useful to have an overview of what modelr can do for you. Therefore, we briefly introduce the methods that modelr provides and later use them in Chapter 25 “Model Validation” on page 475.

To present the functionality, we will focus on a simplemodel based on the well-known dataset mtcars from the package datasets. As usual, we willmodel themiles per gallon of the different car models.

While we show the functionality on a linear model, you can use virtually any model and modelr will take care of the rest.

24.1 Adding Predictions

Each model leads to predictions and predictions can be used to test the quality of the model. So adding predictions will be the work of every modeller for every model. Therefore it is most important to have a standardised way of doing this. modelr's function add_predictions() provides this.

Get The Big R-Book now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.