predict-workflow {workflows} | R Documentation |
Predict from a workflow
Description
This is the predict()
method for a fit workflow object. The nice thing
about predicting from a workflow is that it will:
Preprocess
new_data
using the preprocessing method specified when the workflow was created and fit. This is accomplished usinghardhat::forge()
, which will apply any formula preprocessing or callrecipes::bake()
if a recipe was supplied.Call
parsnip::predict.model_fit()
for you using the underlying fit parsnip model.
Usage
## S3 method for class 'workflow'
predict(object, new_data, type = NULL, opts = list(), ...)
Arguments
object |
A workflow that has been fit by |
new_data |
A data frame containing the new predictors to preprocess
and predict on. If using a recipe preprocessor, you should not call
|
type |
A single character value or |
opts |
A list of optional arguments to the underlying
predict function that will be used when |
... |
Additional
|
Value
A data frame of model predictions, with as many rows as new_data
has.
Examples
library(parsnip)
library(recipes)
library(magrittr)
training <- mtcars[1:20, ]
testing <- mtcars[21:32, ]
model <- linear_reg() %>%
set_engine("lm")
workflow <- workflow() %>%
add_model(model)
recipe <- recipe(mpg ~ cyl + disp, training) %>%
step_log(disp)
workflow <- add_recipe(workflow, recipe)
fit_workflow <- fit(workflow, training)
# This will automatically `bake()` the recipe on `testing`,
# applying the log step to `disp`, and then fit the regression.
predict(fit_workflow, testing)