• add_model() adds a parsnip model to the workflow.

  • remove_model() removes the model specification as well as any fitted model object. Any extra formulas are also removed.

  • update_model() first removes the model then adds the new specification to the workflow.

add_model(x, spec, formula = NULL)

remove_model(x)

update_model(x, spec, formula = NULL)

Arguments

x

A workflow.

spec

A parsnip model specification.

formula

An optional formula override to specify the terms of the model. Typically, the terms are extracted from the formula or recipe preprocessing methods. However, some models (like survival and bayesian models) use the formula not to preprocess, but to specify the structure of the model. In those cases, a formula specifying the model structure must be passed unchanged into the model call itself. This argument is used for those purposes.

Value

x, updated with either a new or removed model.

Details

add_model() is a required step to construct a minimal workflow.

Examples

library(parsnip) lm_model <- linear_reg() lm_model <- set_engine(lm_model, "lm") regularized_model <- set_engine(lm_model, "glmnet") workflow <- workflow() workflow <- add_model(workflow, lm_model) workflow
#> ══ Workflow ════════════════════════════════════════════════════════════════════ #> Preprocessor: None #> Model: linear_reg() #> #> ── Model ─────────────────────────────────────────────────────────────────────── #> Linear Regression Model Specification (regression) #> #> Computational engine: lm #>
workflow <- add_formula(workflow, mpg ~ .) workflow
#> ══ Workflow ════════════════════════════════════════════════════════════════════ #> Preprocessor: Formula #> Model: linear_reg() #> #> ── Preprocessor ──────────────────────────────────────────────────────────────── #> mpg ~ . #> #> ── Model ─────────────────────────────────────────────────────────────────────── #> Linear Regression Model Specification (regression) #> #> Computational engine: lm #>
remove_model(workflow)
#> ══ Workflow ════════════════════════════════════════════════════════════════════ #> Preprocessor: Formula #> Model: None #> #> ── Preprocessor ──────────────────────────────────────────────────────────────── #> mpg ~ .
fitted <- fit(workflow, data = mtcars) fitted
#> ══ Workflow [trained] ══════════════════════════════════════════════════════════ #> Preprocessor: Formula #> Model: linear_reg() #> #> ── Preprocessor ──────────────────────────────────────────────────────────────── #> mpg ~ . #> #> ── Model ─────────────────────────────────────────────────────────────────────── #> #> Call: #> stats::lm(formula = formula, data = data) #> #> Coefficients: #> (Intercept) cyl disp hp drat wt #> 12.30337 -0.11144 0.01334 -0.02148 0.78711 -3.71530 #> qsec vs am gear carb #> 0.82104 0.31776 2.52023 0.65541 -0.19942 #>
remove_model(fitted)
#> ══ Workflow ════════════════════════════════════════════════════════════════════ #> Preprocessor: Formula #> Model: None #> #> ── Preprocessor ──────────────────────────────────────────────────────────────── #> mpg ~ .
remove_model(workflow)
#> ══ Workflow ════════════════════════════════════════════════════════════════════ #> Preprocessor: Formula #> Model: None #> #> ── Preprocessor ──────────────────────────────────────────────────────────────── #> mpg ~ .
update_model(workflow, regularized_model)
#> ══ Workflow ════════════════════════════════════════════════════════════════════ #> Preprocessor: Formula #> Model: linear_reg() #> #> ── Preprocessor ──────────────────────────────────────────────────────────────── #> mpg ~ . #> #> ── Model ─────────────────────────────────────────────────────────────────────── #> Linear Regression Model Specification (regression) #> #> Computational engine: glmnet #>
update_model(fitted, regularized_model)
#> ══ Workflow ════════════════════════════════════════════════════════════════════ #> Preprocessor: Formula #> Model: linear_reg() #> #> ── Preprocessor ──────────────────────────────────────────────────────────────── #> mpg ~ . #> #> ── Model ─────────────────────────────────────────────────────────────────────── #> Linear Regression Model Specification (regression) #> #> Computational engine: glmnet #>