library(autostats)
library(workflows)
library(dplyr)
library(tune)
library(rsample)
library(hardhat)
library(broom.mixed)
library(Ckmeans.1d.dp)
library(igraph)autostats provides convenient wrappers for modeling,
visualizing, and predicting using a tidy workflow. The emphasis is on
rapid iteration and quick results using an intuitive interface based off
the tibble and tidy_formula.
Set up the iris data set for modeling. Create dummies and any new columns before making the formula. This way the same formula can be use throughout the modeling and prediction process.
set.seed(34)
iris %>%
dplyr::as_tibble() %>%
framecleaner::create_dummies(remove_first_dummy = TRUE) -> iris1
#> 1 column(s) have become 2 dummy columns
iris1 %>%
tidy_formula(target = Petal.Length) -> petal_form
petal_form
#> Petal.Length ~ Sepal.Length + Sepal.Width + Petal.Width + species_versicolor +
#> species_virginica
#> <environment: 0x56377a04b268>Use the rsample package to split into train and validation sets.
Fit models to the training set using the formula to predict
Petal.Length. Variable importance using gain for each
xgboost model can be visualized.
auto_tune_xgboost returns a workflow object with tuned
parameters and requires some postprocessing to get a trained
xgb.Booster object like tidy_xgboost.
xgboost also can be tuned using a grid that is created
internally using dials::grid_max_entropy. The
n_iter parameter is passed to grid_size.
Parallelization is highly effective in this method, so the default
argument parallel = TRUE is recommended.
iris_train %>%
auto_tune_xgboost(formula = petal_form, n_iter = 5L,trees = 20L, loss_reduction = 2, mtry = 3, tune_method = "grid", parallel = FALSE, counts = TRUE) -> xgb_tuned_grid
#> i Fold1: preprocessor 1/1
#> i Fold1: preprocessor 1/1 (prediction data)
#> i Fold1: preprocessor 1/1, model 1/1
#> i Fold1: preprocessor 1/1, model 1/1 (predictions)
#> i Fold2: preprocessor 1/1
#> i Fold2: preprocessor 1/1 (prediction data)
#> i Fold2: preprocessor 1/1, model 1/1
#> i Fold2: preprocessor 1/1, model 1/1 (predictions)
#> i Fold3: preprocessor 1/1
#> i Fold3: preprocessor 1/1 (prediction data)
#> i Fold3: preprocessor 1/1, model 1/1
#> i Fold3: preprocessor 1/1, model 1/1 (predictions)
#> i Fold4: preprocessor 1/1
#> i Fold4: preprocessor 1/1 (prediction data)
#> i Fold4: preprocessor 1/1, model 1/1
#> i Fold4: preprocessor 1/1, model 1/1 (predictions)
#> i Fold5: preprocessor 1/1
#> i Fold5: preprocessor 1/1 (prediction data)
#> i Fold5: preprocessor 1/1, model 1/1
#> i Fold5: preprocessor 1/1, model 1/1 (predictions)
#> i Fold1: preprocessor 1/1
#> i Fold1: preprocessor 1/1 (prediction data)
#> i Fold1: preprocessor 1/1, model 1/1
#> i Fold1: preprocessor 1/1, model 1/1 (predictions)
#> i Fold2: preprocessor 1/1
#> i Fold2: preprocessor 1/1 (prediction data)
#> i Fold2: preprocessor 1/1, model 1/1
#> i Fold2: preprocessor 1/1, model 1/1 (predictions)
#> i Fold3: preprocessor 1/1
#> i Fold3: preprocessor 1/1 (prediction data)
#> i Fold3: preprocessor 1/1, model 1/1
#> i Fold3: preprocessor 1/1, model 1/1 (predictions)
#> i Fold4: preprocessor 1/1
#> i Fold4: preprocessor 1/1 (prediction data)
#> i Fold4: preprocessor 1/1, model 1/1
#> i Fold4: preprocessor 1/1, model 1/1 (predictions)
#> i Fold5: preprocessor 1/1
#> i Fold5: preprocessor 1/1 (prediction data)
#> i Fold5: preprocessor 1/1, model 1/1
#> i Fold5: preprocessor 1/1, model 1/1 (predictions)
#> i Fold1: preprocessor 1/1
#> i Fold1: preprocessor 1/1 (prediction data)
#> i Fold1: preprocessor 1/1, model 1/1
#> i Fold1: preprocessor 1/1, model 1/1 (predictions)
#> i Fold2: preprocessor 1/1
#> i Fold2: preprocessor 1/1 (prediction data)
#> i Fold2: preprocessor 1/1, model 1/1
#> i Fold2: preprocessor 1/1, model 1/1 (predictions)
#> i Fold3: preprocessor 1/1
#> i Fold3: preprocessor 1/1 (prediction data)
#> i Fold3: preprocessor 1/1, model 1/1
#> i Fold3: preprocessor 1/1, model 1/1 (predictions)
#> i Fold4: preprocessor 1/1
#> i Fold4: preprocessor 1/1 (prediction data)
#> i Fold4: preprocessor 1/1, model 1/1
#> i Fold4: preprocessor 1/1, model 1/1 (predictions)
#> i Fold5: preprocessor 1/1
#> i Fold5: preprocessor 1/1 (prediction data)
#> i Fold5: preprocessor 1/1, model 1/1
#> i Fold5: preprocessor 1/1, model 1/1 (predictions)
#> i Fold1: preprocessor 1/1
#> i Fold1: preprocessor 1/1 (prediction data)
#> i Fold1: preprocessor 1/1, model 1/1
#> i Fold1: preprocessor 1/1, model 1/1 (predictions)
#> i Fold2: preprocessor 1/1
#> i Fold2: preprocessor 1/1 (prediction data)
#> i Fold2: preprocessor 1/1, model 1/1
#> i Fold2: preprocessor 1/1, model 1/1 (predictions)
#> i Fold3: preprocessor 1/1
#> i Fold3: preprocessor 1/1 (prediction data)
#> i Fold3: preprocessor 1/1, model 1/1
#> i Fold3: preprocessor 1/1, model 1/1 (predictions)
#> i Fold4: preprocessor 1/1
#> i Fold4: preprocessor 1/1 (prediction data)
#> i Fold4: preprocessor 1/1, model 1/1
#> i Fold4: preprocessor 1/1, model 1/1 (predictions)
#> i Fold5: preprocessor 1/1
#> i Fold5: preprocessor 1/1 (prediction data)
#> i Fold5: preprocessor 1/1, model 1/1
#> i Fold5: preprocessor 1/1, model 1/1 (predictions)
#> i Fold1: preprocessor 1/1
#> i Fold1: preprocessor 1/1 (prediction data)
#> i Fold1: preprocessor 1/1, model 1/1
#> i Fold1: preprocessor 1/1, model 1/1 (predictions)
#> i Fold2: preprocessor 1/1
#> i Fold2: preprocessor 1/1 (prediction data)
#> i Fold2: preprocessor 1/1, model 1/1
#> i Fold2: preprocessor 1/1, model 1/1 (predictions)
#> i Fold3: preprocessor 1/1
#> i Fold3: preprocessor 1/1 (prediction data)
#> i Fold3: preprocessor 1/1, model 1/1
#> i Fold3: preprocessor 1/1, model 1/1 (predictions)
#> i Fold4: preprocessor 1/1
#> i Fold4: preprocessor 1/1 (prediction data)
#> i Fold4: preprocessor 1/1, model 1/1
#> i Fold4: preprocessor 1/1, model 1/1 (predictions)
#> i Fold5: preprocessor 1/1
#> i Fold5: preprocessor 1/1 (prediction data)
#> i Fold5: preprocessor 1/1, model 1/1
#> i Fold5: preprocessor 1/1, model 1/1 (predictions)
xgb_tuned_grid %>%
parsnip::fit(iris_train) %>%
parsnip::extract_fit_engine() -> xgb_tuned_fit_grid
xgb_tuned_fit_grid %>%
visualize_model()iris_train %>%
tidy_xgboost(formula = petal_form) -> xgb_base
#> accuracy tested on a validation set
#> # A tibble: 3 × 2
#> .metric .estimate
#> <chr> <dbl>
#> 1 ccc 0.688
#> 2 rmse 1.18
#> 3 rsq 0.528iris_train %>%
tidy_xgboost(petal_form,
trees = 250L,
tree_depth = 3L,
sample_size = .5,
mtry = .5,
min_n = 2) -> xgb_opt
#> accuracy tested on a validation set
#> # A tibble: 3 × 2
#> .metric .estimate
#> <chr> <dbl>
#> 1 ccc 0.960
#> 2 rmse 0.450
#> 3 rsq 0.954Predictions are iteratively added to the validation data frame. The name of the column is automatically created using the models name and the prediction target.
xgb_base %>%
tidy_predict(newdata = iris_val, form = petal_form) -> iris_val2
#> created the following column: Petal.Length_preds_xgb_base
xgb_opt %>%
tidy_predict(newdata = iris_val2, petal_form) -> iris_val3
#> created the following column: Petal.Length_preds_xgb_opt
iris_val3 %>%
names()
#> [1] "Sepal.Length" "Sepal.Width"
#> [3] "Petal.Length" "Petal.Width"
#> [5] "species_versicolor" "species_virginica"
#> [7] "Petal.Length_preds_xgb_base" "Petal.Length_preds_xgb_opt"Instead of evaluationg these prediction 1 by 1, This step is
automated with eval_preds. This function is specifically
designed to evaluate predicted columns with names given from
tidy_predict.
iris_val3 %>%
eval_preds()
#> # A tibble: 6 × 5
#> .metric .estimator .estimate model target
#> <chr> <chr> <dbl> <chr> <chr>
#> 1 ccc standard 0.979 xgb_base Petal.Length
#> 2 ccc standard 0.975 xgb_opt Petal.Length
#> 3 rmse standard 0.350 xgb_base Petal.Length
#> 4 rmse standard 0.376 xgb_opt Petal.Length
#> 5 rsq standard 0.972 xgb_base Petal.Length
#> 6 rsq standard 0.964 xgb_opt Petal.Lengthtidy_shap has similar syntax to
tidy_predict and can be used to get shapley values from
xgboost models on a validation set.
shap_list$shap_tbl
#> # A tibble: 38 × 6
#> Sepal.Length Sepal.Width Petal.Width species_versicolor species_virginica
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 -0.430 -0.0284 -1.61 0.0597 -0.103
#> 2 -0.430 -0.0284 -1.61 0.0597 -0.103
#> 3 0.0385 -0.0531 -1.60 0.0585 -0.122
#> 4 -0.430 -0.0284 -1.61 0.0597 -0.103
#> 5 -0.426 -0.0284 -1.61 0.0573 -0.103
#> 6 -0.430 -0.0284 -1.61 0.0597 -0.103
#> 7 -0.454 -0.0284 -1.56 0.0597 -0.0950
#> 8 -0.430 -0.0284 -1.61 0.0597 -0.103
#> 9 -0.430 -0.0284 -1.61 0.0597 -0.103
#> 10 -0.410 0.153 -1.56 0.0597 -0.103
#> # ℹ 28 more rows
#> # ℹ 1 more variable: `(Intercept)` <dbl>shap_list$shap_summary
#> # A tibble: 6 × 5
#> name cor var sum sum_abs
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 (Intercept) NA 0 139. 139.
#> 2 Petal.Width 0.954 1.43 6.95 42.3
#> 3 Sepal.Length 0.960 0.130 0.818 11.1
#> 4 species_virginica 0.998 0.0181 0.754 4.93
#> 5 species_versicolor -0.935 0.0116 1.95 4.25
#> 6 Sepal.Width -0.762 0.00238 0.134 1.11Overfittingin the base config may be related to growing deep trees.
Plot the first tree in the model. The small in terminal leaves suggests
overfitting in the base model.