last_fit: Fit the final best model to the training set and evaluate the...

Description Usage Arguments Details Value Examples

View source: R/last_fit.R

Description

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.

Usage

1
2
3
4
5
6
7
last_fit(object, ...)

## S3 method for class 'model_spec'
last_fit(object, preprocessor, split, ..., metrics = NULL)

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

Arguments

object

A parsnip model specification or a workflows::workflow(). No tuning parameters are allowed.

...

Currently unused.

preprocessor

A traditional model formula or a recipe created using recipes::recipe().

split

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

metrics

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

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.

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.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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(lin_mod, spline_rec, split = tr_te_split)
spline_res

# test set results
spline_res$.metrics[[1]]

# 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)

tune documentation built on July 21, 2021, 5:09 p.m.