spsanderson / healthyR.ai

healthyR.ai - AI package for the healthyverse
http://www.spsanderson.com/healthyR.ai/
Other
16 stars 6 forks source link

hai_auto_glmnet() #248

Closed spsanderson closed 2 years ago

spsanderson commented 2 years ago

Function:

#' Boilerplate Workflow
#'
#' @family Boiler_Plate
#' @family glmnet
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @details
#' This uses the `parsnip::multinom_reg()` with the `engine` set to `glmnet`
#'
#' @description This is a boilerplate function to create automatically the following:
#' -  recipe
#' -  model specification
#' -  workflow
#' -  tuned model (grid ect)
#'
#' @param .data The data being passed to the function. The time-series object.
#' @param .rec_obj This is the recipe object you want to use. You can use
#' `hai_glmnet_data_prepper()` an automatic recipe_object.
#' @param .splits_obj NULL is the default, when NULL then one will be created.
#' @param .rsamp_obj NULL is the default, when NULL then one will be created. It
#' will default to creating an [rsample::mc_cv()] object.
#' @param .tune Default is TRUE, this will create a tuning grid and tuned workflow
#' @param .grid_size Default is 10
#' @param .num_cores Default is 1
#' @param .best_metric Default is "f_meas". You can choose a metric depending on the
#' model_type used. If `regression` then see [healthyR.ai::hai_default_regression_metric_set()],
#' if `classification` then see [healthyR.ai::hai_default_classification_metric_set()].
#' @param .model_type Default is `classification`, can also be `regression`.
#'
#' @examples
#' \dontrun{
#' data <- iris
#'
#' rec_obj <- hai_glmnet_data_prepper(data, Species ~ .)
#'
#' auto_glm <- hai_auto_glmnet(
#'   .data = data,
#'   .rec_obj = rec_obj,
#'   .best_metric = "f_meas",
#'   .model_type = "classification"
#' )
#'
#' auto_glm$recipe_info
#' }
#'
#' @return
#' A list
#'
#' @export
#'

hai_auto_glmnet <- function(.data, .rec_obj, .splits_obj = NULL, .rsamp_obj = NULL,
                         .tune = TRUE, .grid_size = 10, .num_cores = 1,
                         .best_metric = "f_meas", .model_type = "classification"){

  # Tidyeval ----
  grid_size <- as.numeric(.grid_size)
  num_cores <- as.numeric(.num_cores)
  best_metric <- as.character(.best_metric)

  data_tbl <- dplyr::as_tibble(.data)

  splits <- .splits_obj
  rec_obj <- .rec_obj
  rsamp_obj <- .rsamp_obj
  model_type <- as.character(.model_type)

  # Checks ----
  if (!inherits(x = splits, what = "rsplit") && !is.null(splits)){
    rlang::abort(
      message = "'.splits_obj' must have a class of 'rsplit', use the rsample package.",
      use_cli_format = TRUE
    )
  }

  if (!inherits(x = rec_obj, what = "recipe")){
    rlang::abort(
      message = "'.rec_obj' must have a class of 'recipe'."
    )
  }

  if (!model_type %in% c("regression","classification")){
    rlang::abort(
      message = paste0(
        "You chose a mode of: '",
        model_type,
        "' this is unsupported. Choose from either 'regression' or 'classification'."
      ),
      use_cli_format = TRUE
    )
  }

  if (!inherits(x = rsamp_obj, what = "rset") && !is.null(rsamp_obj)){
    rlang::abort(
      message = "The '.rsamp_obj' argument must either be NULL or an object of
      calss 'rset'.",
      use_cli_format = TRUE
    )
  }

  if (!inherits(x = splits, what = "rsplit") && !is.null(splits)){
    rlang::abort(
      message = "The '.splits_obj' argument must either be NULL or an object of
      class 'rsplit'",
      use_cli_format = TRUE
    )
  }

  # Set default metric set ----
  if (model_type == "classification"){
    ms <- healthyR.ai::hai_default_classification_metric_set()
  } else {
    ms <- healthyR.ai::hai_default_regression_metric_set()
  }

  # Get splits if not then create
  if (is.null(splits)){
    splits <- rsample::initial_split(data = data_tbl)
  } else {
    splits <- splits
  }

  # Tune/Spec ----
  if (.tune){
    # Model Specification
    model_spec <- parsnip::multinom_reg(
      penalty = tune::tune(),
      mixture = tune::tune()
    )
  } else {
    model_spec <- parsnip::multinom_reg()
  }

  # Model Specification ----
  model_spec <- model_spec %>%
    parsnip::set_mode(mode = model_type) %>%
    parsnip::set_engine(engine = "glmnet")

  # Workflow ----
  wflw <- workflows::workflow() %>%
    workflows::add_recipe(rec_obj) %>%
    workflows::add_model(model_spec)

  # Tuning Grid ---
  if (.tune){

    # Make tuning grid
    tuning_grid_spec <- dials::grid_latin_hypercube(
      hardhat::extract_parameter_set_dials(model_spec),
      size = grid_size
    )

    # Cross validation object
    if (is.null(rsamp_obj)){
      cv_obj <- rsample::mc_cv(
        data = rsample::training(splits)
      )
    } else {
      cv_obj <- rsamp_obj
    }

    # Tune the workflow
    # Start parallel backed
    modeltime::parallel_start(num_cores)

    tuned_results <- wflw %>%
      tune::tune_grid(
        resamples = cv_obj,
        grid      = tuning_grid_spec,
        metrics   = ms
      )

    modeltime::parallel_stop()

    # Get the best result set by a specified metric
    best_result_set <- tuned_results %>%
      tune::show_best(metric = best_metric, n = 1)

    # Plot results
    tune_results_plt <- tuned_results %>%
      tune::autoplot() +
      ggplot2::theme_minimal() +
      ggplot2::geom_smooth(se = FALSE) +
      ggplot2::theme(legend.position = "bottom")

    # Make final workflow
    wflw_fit <- wflw %>%
      tune::finalize_workflow(
        tuned_results %>%
          tune::show_best(metric = best_metric, n = 1)
      ) %>%
      parsnip::fit(rsample::training(splits))

  } else {
    wflw_fit <- wflw %>%
      parsnip::fit(rsample::training(splits))
  }

  # Return ----
  output <- list(
    recipe_info = rec_obj,
    model_info = list(
      model_spec  = model_spec,
      wflw        = wflw,
      fitted_wflw = wflw_fit,
      was_tuned   = ifelse(.tune, "tuned", "not_tuned")
    )
  )

  if (.tune){
    output$tuned_info = list(
      tuning_grid      = tuning_grid_spec,
      cv_obj           = cv_obj,
      tuned_results    = tuned_results,
      grid_size        = grid_size,
      best_metric      = best_metric,
      best_result_set  = best_result_set,
      tuning_grid_plot = tune_results_plt,
      plotly_grid_plot = plotly::ggplotly(tune_results_plt)
    )
  }

  attr(output, "function_type") <- "boilerplate"
  attr(output, ".grid_size") <- .grid_size
  attr(output, ".tune") <- .tune
  attr(output, ".best_metric") <- .best_metric
  attr(output, ".model_type") <- .model_type

  return(invisible(output))

}

Example:

data <- iris

rec_obj <- hai_glmnet_data_prepper(data, Species ~ .)

auto_glm <- hai_auto_glmnet(
  .data = data,
  .rec_obj = rec_obj,
  .best_metric = "f_meas",
  .model_type = "classification"
)

> auto_glm
$recipe_info
Recipe

Inputs:

      role #variables
   outcome          1
 predictor          4

Operations:

Factor variables from tidyselect::vars_select_helpers$where(is.character)
Novel factor level assignment for recipes::all_nominal_predictors()
Dummy variables from recipes::all_nominal_predictors()
Zero variance filter on recipes::all_predictors()
Centering and scaling for recipes::all_numeric_predictors()

$model_info
$model_info$model_spec
Multinomial Regression Model Specification (classification)

Main Arguments:
  penalty = tune::tune()
  mixture = tune::tune()

Computational engine: glmnet 

$model_info$wflw
== Workflow ===============================================================================
Preprocessor: Recipe
Model: multinom_reg()

-- Preprocessor ---------------------------------------------------------------------------
5 Recipe Steps

* step_string2factor()
* step_novel()
* step_dummy()
* step_zv()
* step_normalize()

-- Model ----------------------------------------------------------------------------------
Multinomial Regression Model Specification (classification)

Main Arguments:
  penalty = tune::tune()
  mixture = tune::tune()

Computational engine: glmnet 

$model_info$fitted_wflw
== Workflow [trained] =====================================================================
Preprocessor: Recipe
Model: multinom_reg()

-- Preprocessor ---------------------------------------------------------------------------
5 Recipe Steps

* step_string2factor()
* step_novel()
* step_dummy()
* step_zv()
* step_normalize()

-- Model ----------------------------------------------------------------------------------

Call:  glmnet::glmnet(x = maybe_matrix(x), y = y, family = "multinomial",      alpha = ~0.584753754525445) 

    Df  %Dev  Lambda
1    0  0.00 0.75580
2    2  3.19 0.68870
3    2  7.04 0.62750
4    2 10.60 0.57180
5    2 14.24 0.52100
6    2 18.10 0.47470
7    2 21.67 0.43250
8    2 24.99 0.39410
9    3 28.30 0.35910
10   4 31.55 0.32720
11   4 34.62 0.29810
12   4 37.78 0.27160
13   4 40.69 0.24750
14   4 43.38 0.22550
15   4 45.87 0.20550
16   4 48.19 0.18720
17   4 50.35 0.17060
18   4 52.38 0.15540
19   4 54.28 0.14160
20   4 56.08 0.12900
21   4 57.78 0.11760
22   4 59.41 0.10710
23   4 60.98 0.09762
24   4 62.49 0.08895
25   4 63.96 0.08105
26   4 65.38 0.07385
27   4 66.77 0.06729
28   4 68.12 0.06131
29   4 69.45 0.05586
30   4 70.73 0.05090
31   4 71.98 0.04638
32   4 73.19 0.04226
33   4 74.35 0.03850
34   4 75.47 0.03508
35   4 76.53 0.03197
36   4 77.55 0.02913
37   4 78.46 0.02654
38   4 79.35 0.02418
39   4 80.19 0.02203
40   4 81.07 0.02008
41   4 81.94 0.01829
42   4 82.76 0.01667
43   4 83.53 0.01519
44   4 84.25 0.01384
45   4 84.93 0.01261
46   4 85.57 0.01149

...
and 54 more lines.

$model_info$was_tuned
[1] "tuned"

$tuned_info
$tuned_info$tuning_grid
# A tibble: 10 x 2
    penalty mixture
      <dbl>   <dbl>
 1 5.98e- 5 0.00996
 2 2.99e- 3 0.585  
 3 4.04e-10 0.926  
 4 5.80e- 1 0.156  
 5 6.95e- 6 0.609  
 6 4.73e- 9 0.258  
 7 2.27e- 8 0.411  
 8 7.54e- 2 0.767  
 9 1.42e- 4 0.372  
10 1.94e- 7 0.893  

$tuned_info$cv_obj
# Monte Carlo cross-validation (0.75/0.25) with 25 resamples  
# A tibble: 25 x 2
   splits          id        
   <list>          <chr>     
 1 <split [84/28]> Resample01
 2 <split [84/28]> Resample02
 3 <split [84/28]> Resample03
 4 <split [84/28]> Resample04
 5 <split [84/28]> Resample05
 6 <split [84/28]> Resample06
 7 <split [84/28]> Resample07
 8 <split [84/28]> Resample08
 9 <split [84/28]> Resample09
10 <split [84/28]> Resample10
# ... with 15 more rows

$tuned_info$tuned_results
# Tuning results
# Monte Carlo cross-validation (0.75/0.25) with 25 resamples  
# A tibble: 25 x 4
   splits          id         .metrics           .notes          
   <list>          <chr>      <list>             <list>          
 1 <split [84/28]> Resample01 <tibble [110 x 6]> <tibble [0 x 3]>
 2 <split [84/28]> Resample02 <tibble [110 x 6]> <tibble [0 x 3]>
 3 <split [84/28]> Resample03 <tibble [110 x 6]> <tibble [0 x 3]>
 4 <split [84/28]> Resample04 <tibble [110 x 6]> <tibble [0 x 3]>
 5 <split [84/28]> Resample05 <tibble [110 x 6]> <tibble [0 x 3]>
 6 <split [84/28]> Resample06 <tibble [110 x 6]> <tibble [0 x 3]>
 7 <split [84/28]> Resample07 <tibble [110 x 6]> <tibble [0 x 3]>
 8 <split [84/28]> Resample08 <tibble [110 x 6]> <tibble [0 x 3]>
 9 <split [84/28]> Resample09 <tibble [110 x 6]> <tibble [0 x 3]>
10 <split [84/28]> Resample10 <tibble [110 x 6]> <tibble [0 x 3]>
# ... with 15 more rows

$tuned_info$grid_size
[1] 10

$tuned_info$best_metric
[1] "f_meas"

$tuned_info$best_result_set
# A tibble: 1 x 8
  penalty mixture .metric .estimator  mean     n std_err .config              
    <dbl>   <dbl> <chr>   <chr>      <dbl> <int>   <dbl> <chr>                
1 0.00299   0.585 f_meas  macro      0.954    25 0.00769 Preprocessor1_Model06

$tuned_info$tuning_grid_plot
`geom_smooth()` using method = 'loess' and formula 'y ~ x'

$tuned_info$plotly_grid_plot

attr(,"function_type")
[1] "boilerplate"
attr(,".grid_size")
[1] 10
attr(,".tune")
[1] TRUE
attr(,".best_metric")
[1] "f_meas"
attr(,".model_type")
[1] "classification"