I have a fitted model object and a tuned model object. With the fitted model, I am able to extract model information spec, engine, class, etc. See example below using a fitted Xgboost model called fit_xgboost
. Using the commands like class()
, fit_xgboost$trained
, etc, I can see the model information like class, spec and computational engine.
> class(fit_xgboost)
[1] "workflow"
> fit_xgboost$trained
[1] TRUE
> fit_xgboost$fit$actions$model$spec
Boosted Tree Model Specification (regression)
Computational engine: xgboost
> fit_xgboost$fit$fit$spec$engine
[1] "xgboost"
How do I get the same information when the model object has been tuned? I can see the class of a tuned model is "tune_results" "tbl_df" "tbl" "data.frame"
, but is there a way to get the spec, computational engine, etc. For context; I'm creating some custom functions for tuned model objects and need a way to check the model information and provide feedback to the user based on certain conditions. Maybe this is not the right approach, so I'm happy to take feedback/better suggestions.
See code to produce similar model objects below -
# Libraries
library(tidyverse)
library(tidymodels)
# Data
mtcars_data <- mtcars %>% as_tibble()
# Resamples
set.seed(123)
mtcars_resamples <- vfold_cv(mtcars_data, v = 5)
# Recipe
recipe_spec <- recipe(mpg ~., data = mtcars_data)
# Model Spec
fit_xgboost <- workflow() %>%
add_model(
spec = boost_tree() %>%
set_mode("regression") %>%
set_engine("xgboost")
) %>%
add_recipe(recipe_spec) %>%
fit(mtcars_train)
# Tuning
xgboost_spec <-boost_tree(
trees = 500,
min_n = tune(),
tree_depth = tune(),
learn_rate = tune(),
loss_reduction = tune(),
sample_size = tune()
) %>%
set_mode("regression") %>%
set_engine("xgboost")
tune_xgboost <- tune_grid(
object = workflow() %>% add_recipe(recipe_spec) %>% add_model(xgboost_spec),
resamples = mtcars_resamples,
grid = grid_latin_hypercube(x = parameters(xgboost_spec), size = 5),
control = control_grid(save_pred = TRUE, verbose = FALSE),
metrics = metric_set(mae, rmse, rsq)
)