extract-parsnip: Extract elements of a parsnip model object

extract-parsnipR Documentation

Extract elements of a parsnip model object

Description

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

  • extract_spec_parsnip() returns the parsnip model specification.

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

  • extract_parameter_dials() returns a single dials parameter object.

  • extract_parameter_set_dials() returns a set of dials parameter objects.

Usage

## S3 method for class 'model_fit'
extract_spec_parsnip(x, ...)

## S3 method for class 'model_fit'
extract_fit_engine(x, ...)

## S3 method for class 'model_spec'
extract_parameter_set_dials(x, ...)

## S3 method for class 'model_spec'
extract_parameter_dials(x, parameter, ...)

Arguments

x

A parsnip model_fit object or a parsnip model_spec object.

...

Not currently used.

parameter

A single string for the parameter ID.

Details

Extracting the underlying engine fit can be helpful for describing the model (via print(), summary(), plot(), etc.) or for variable importance/explainers.

However, users should not invoke the predict() method on an extracted model. There may be preprocessing operations that parsnip has executed on the data prior to giving it to the model. Bypassing these can lead to errors or silently generating incorrect predictions.

Good:

   parsnip_fit %>% predict(new_data)

Bad:

   parsnip_fit %>% extract_fit_engine() %>% predict(new_data)

Value

The extracted value from the parsnip object, x, as described in the description section.

Examples


lm_spec <- linear_reg() %>% set_engine("lm")
lm_fit <- fit(lm_spec, mpg ~ ., data = mtcars)

lm_spec
extract_spec_parsnip(lm_fit)

extract_fit_engine(lm_fit)
lm(mpg ~ ., data = mtcars)


parsnip documentation built on Aug. 18, 2023, 1:07 a.m.