These functions extract various elements from a tune object. If they do not exist yet, an error is thrown.

  • extract_preprocessor() returns the formula, recipe, or variable expressions used for preprocessing.

  • extract_spec_parsnip() returns the parsnip model specification.

  • extract_fit_parsnip() returns the parsnip model fit object.

  • extract_fit_engine() returns the engine specific fit embedded within a parsnip model fit. For example, when using parsnip::linear_reg() with the "lm" engine, this returns the underlying lm object.

  • extract_mold() returns the preprocessed "mold" object returned from hardhat::mold(). It contains information about the preprocessing, including either the prepped recipe, the formula terms object, or variable selectors.

  • extract_recipe() returns the recipe. The estimated argument specifies whether the fitted or original recipe is returned.

  • extract_workflow() returns the workflow object if the control option save_workflow = TRUE was used. The workflow will only have been estimated for objects produced by last_fit().

# S3 method for last_fit
extract_workflow(x, ...)

# S3 method for tune_results
extract_workflow(x, ...)

# S3 method for tune_results
extract_spec_parsnip(x, ...)

# S3 method for tune_results
extract_recipe(x, ..., estimated = TRUE)

# S3 method for tune_results
extract_fit_parsnip(x, ...)

# S3 method for tune_results
extract_fit_engine(x, ...)

# S3 method for tune_results
extract_mold(x, ...)

# S3 method for tune_results
extract_preprocessor(x, ...)

Arguments

x

A tune_results object.

...

Not currently used.

estimated

A logical for whether the original (unfit) recipe or the fitted recipe should be returned.

Value

The extracted value from the tune tune_results, x, as described in the description section.

Details

These functions supersede extract_model().

Examples

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) extract_preprocessor(spline_res)
#> Data Recipe #> #> Inputs: #> #> role #variables #> outcome 1 #> predictor 10 #> #> Operations: #> #> Natural Splines on disp
# The `spec` is the parsnip spec before it has been fit. # The `fit` is the fitted parsnip model. extract_spec_parsnip(spline_res)
#> Linear Regression Model Specification (regression) #> #> Computational engine: lm #>
extract_fit_parsnip(spline_res)
#> parsnip model object #> #> Fit time: 4ms #> #> Call: #> stats::lm(formula = ..y ~ ., data = data) #> #> Coefficients: #> (Intercept) cyl hp drat wt qsec #> 23.087028 0.326218 0.005969 -0.009576 -0.902839 0.185826 #> vs am gear carb disp_ns_1 disp_ns_2 #> 1.492756 4.101555 0.174875 -1.278962 -15.149506 -4.905087 #>
extract_fit_engine(spline_res)
#> #> Call: #> stats::lm(formula = ..y ~ ., data = data) #> #> Coefficients: #> (Intercept) cyl hp drat wt qsec #> 23.087028 0.326218 0.005969 -0.009576 -0.902839 0.185826 #> vs am gear carb disp_ns_1 disp_ns_2 #> 1.492756 4.101555 0.174875 -1.278962 -15.149506 -4.905087 #>
# The mold is returned from `hardhat::mold()`, and contains the # predictors, outcomes, and information about the preprocessing # for use on new data at `predict()` time. extract_mold(spline_res)
#> $predictors #> # A tibble: 24 x 11 #> cyl hp drat wt qsec vs am gear carb disp_ns_1 disp_ns_2 #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 8 205 2.93 5.25 18.0 0 0 3 4 0.341 0.774 #> 2 4 95 3.92 3.15 22.9 1 0 4 2 0.260 -0.165 #> 3 6 175 3.62 2.77 15.5 0 1 5 6 0.274 -0.173 #> 4 8 245 3.73 3.84 15.4 0 0 3 4 0.554 0.0663 #> 5 4 52 4.93 1.62 18.5 1 1 4 2 0.0177 -0.0117 #> 6 8 180 3.07 3.78 18 0 0 3 3 0.571 -0.197 #> 7 8 215 3 5.42 17.8 0 0 3 4 0.366 0.698 #> 8 8 175 3.15 3.44 17.0 0 0 3 2 0.542 0.115 #> 9 8 180 3.07 4.07 17.4 0 0 3 3 0.571 -0.197 #> 10 6 110 3.9 2.88 17.0 0 1 4 4 0.324 -0.200 #> # … with 14 more rows #> #> $outcomes #> # A tibble: 24 x 1 #> mpg #> <dbl> #> 1 10.4 #> 2 22.8 #> 3 19.7 #> 4 13.3 #> 5 30.4 #> 6 15.2 #> 7 10.4 #> 8 18.7 #> 9 16.4 #> 10 21 #> # … with 14 more rows #> #> $blueprint #> Recipe blueprint: #> #> # Predictors: 10 #> # Outcomes: 1 #> Intercept: FALSE #> Novel Levels: FALSE #> Composition: tibble #> #> $extras #> $extras$roles #> NULL #> #>
# A useful shortcut is to extract the fitted recipe from the workflow extract_recipe(spline_res)
#> Data Recipe #> #> Inputs: #> #> role #variables #> outcome 1 #> predictor 10 #> #> Training data contained 24 data points and no missing data. #> #> Operations: #> #> Natural Splines on disp [trained]
# That is identical to identical( extract_mold(spline_res)$blueprint$recipe, extract_recipe(spline_res) )
#> [1] TRUE