last_fit() emulates the process where, after determining the best model, the final fit on the entire training set is needed and is then evaluated on the test set.

last_fit(object, ...)

# S3 method for recipe
last_fit(object, model, split, ..., metrics = NULL)

# S3 method for formula
last_fit(formula, model, split, ..., metrics = NULL)

# S3 method for workflow
last_fit(object, split, ..., metrics = NULL)

Arguments

object

A workflow, formula, or recipe. No tuning parameters are allowed.

...

Currently unused.

model

A parsnip model specification. No tuning parameters are allowed.

split

An rsplit object created from rsample::initial_split().

metrics

A yardstick::metric_set(), or NULL to compute a standard set of metrics.

formula

A formula specifying the terms of the model.

Value

A single row tibble that emulates the structure of fit_resamples(). However, a list column called .workflow is also attached with the fitted model (and recipe, if any) that used the training set.

Details

This function is intended to be used after fitting a variety of models and the final tuning parameters (if any) have been finalized. The next step would be to fit using the entire training set and verify performance using the test data.

Examples

# \donttest{ library(recipes) library(rsample) library(parsnip) set.seed(6735) tr_te_split <- initial_split(mtcars) spline_rec <- recipe(mpg ~ ., data = mtcars) %>% step_ns(disp) lin_mod <- linear_reg() %>% set_engine("lm") spline_res <- last_fit(spline_rec, lin_mod, split = tr_te_split) spline_res
#> # # Monte Carlo cross-validation (0.75/0.25) with 1 resamples #> # A tibble: 1 x 6 #> splits id .metrics .notes .predictions .workflow #> * <list> <chr> <list> <list> <list> <list> #> 1 <split [24/… train/test s… <tibble [2 ×<tibble [0 <tibble [8 × <workflo
# test set results spline_res$.metrics[[1]]
#> # A tibble: 2 x 3 #> .metric .estimator .estimate #> <chr> <chr> <dbl> #> 1 rmse standard 5.74 #> 2 rsq standard 0.503
# or use a workflow library(workflows) spline_wfl <- workflow() %>% add_recipe(spline_rec) %>% add_model(lin_mod) last_fit(spline_wfl, split = tr_te_split)
#> # # Monte Carlo cross-validation (0.75/0.25) with 1 resamples #> # A tibble: 1 x 6 #> splits id .metrics .notes .predictions .workflow #> * <list> <chr> <list> <list> <list> <list> #> 1 <split [24/… train/test s… <tibble [2 ×<tibble [0 <tibble [8 × <workflo
# }