Hello. I am working with the vignette of the new
rsample package located here. This package is used in conjunction with
recipes as part of Max Kuhn's tidy modeling approach. Is there a way to extract the model formula from a
recipe class object?
In the code block below, taken from the vignetted linked to above, the model formula is first specified in
rec <- recipe(Sale_Price ~ Neighborhood + House_Style + Year_Sold + Lot_Area, data = ames) and then later respecified in
map(bt_samples$recipes, fit_lm, Sale_Price ~ .) (the last line). It would be great to be able to pull the formula straight from the
recipe class object and map to
lm for fitting, instead of specifying the same thing twice. Thinking something like
extract_formula(rec), but I cannot find the formula in the
rec object as it is currently defined.
Any thoughts would be appreciated!
library(rsample) library(recipes) library(AmesHousing) ames <- make_ames() set.seed(7712) bt_samples <- bootstraps(ames) rec <- recipe(Sale_Price ~ Neighborhood + House_Style + Year_Sold + Lot_Area, data = ames) %>% step_log(Sale_Price, base = 10) %>% step_other(Neighborhood, House_Style, threshold = 0.05) %>% step_dummy(all_nominal()) %>% step_BoxCox(Lot_Area) %>% step_center(all_predictors()) %>% step_scale(all_predictors()) fit_lm <- function(rec_obj, ...) lm(..., data = juice(rec_obj, everything())) bt_samples$recipes <- map(bt_samples$splits, prepper, recipe = rec, retain = TRUE, verbose = FALSE) bt_samples$lm_mod <- map(bt_samples$recipes, fit_lm, Sale_Price ~ .)