This is the predict() method for a fit workflow object. The nice thing
about predicting from a workflow is that it will:
Preprocess
new_datausing 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.
Arguments
- object
A workflow that has been fit by
fit.workflow()- new_data
A data frame containing the new predictors to preprocess and predict on. If using a recipe preprocessor, you should not call
recipes::bake()onnew_databefore passing to this function.- type
A single character value or
NULL. Possible values are"numeric","class","prob","conf_int","pred_int","quantile","time","hazard","survival", or"raw". WhenNULL,predict()will choose an appropriate value based on the model's mode.- opts
A list of optional arguments to the underlying predict function that will be used when
type = "raw". The list should not include options for the model object or the new data being predicted.- ...
Additional
parsnip-related options, depending on the value oftype. Arguments to the underlying model's prediction function cannot be passed here (use theoptsargument instead). Possible arguments are:interval: fortypeequal to"survival"or"quantile", should interval estimates be added, if available? Options are"none"and"confidence".level: fortypeequal to"conf_int","pred_int", or"survival", this is the parameter for the tail area of the intervals (e.g. confidence level for confidence intervals). Default value is0.95.std_error: fortypeequal to"conf_int"or"pred_int", add the standard error of fit or prediction (on the scale of the linear predictors). Default value isFALSE.quantile: fortypeequal toquantile, the quantiles of the distribution. Default is(1:9)/10.eval_time: fortypeequal to"survival"or"hazard", the time points at which the survival probability or hazard is estimated.
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)
#> # A tibble: 12 × 1
#> .pred
#> <dbl>
#> 1 25.4
#> 2 15.4
#> 3 15.8
#> 4 14.4
#> 5 13.2
#> 6 29.4
#> 7 25.4
#> 8 27.6
#> 9 14.4
#> 10 23.2
#> 11 15.9
#> 12 25.3
