Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make function fast_classification_parsnip_spec_tbl() #62

Closed
spsanderson opened this issue Dec 29, 2022 · 1 comment
Closed

Make function fast_classification_parsnip_spec_tbl() #62

spsanderson opened this issue Dec 29, 2022 · 1 comment
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@spsanderson
Copy link
Owner

spsanderson commented Dec 29, 2022

Function:

#' Utility Classification call to `parsnip`
#'
#' @family Utility
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @details Creates a tibble of parsnip classification model specifications. This will
#' create a tibble of 32 different classification model specifications which can be
#' filtered. The model specs are created first and then filtered out. This will
#' only create models for __classification__ problems. To find all of the supported
#' models in this package you can visit \url{https://www.tidymodels.org/find/parsnip/}
#'
#' @description Creates a tibble of parsnip classification model specifications.
#'
#' @param .parsnip_fns The default for this is set to `all`. This means that all
#' of the parsnip __classification__ functions will be used, for example `bag_mars()`,
#' or `bart()`. You can also choose to pass a c() vector like `c("barg_mars","bart")`
#' @param .parsnip_eng The default for this is set to `all`. This means that all
#' of the parsnip __classification engines__ will be used, for example `earth`, or
#' `dbarts`. You can also choose to pass a c() vector like `c('earth', 'dbarts')`
#'
#' @examples
#' fast_classification_parsnip_spec_tbl(.parsnip_fns = "logistic_reg")
#' fast_classification_parsnip_spec_tbl(.parsnip_eng = c("earth","dbarts"))
#'
#' @return
#' A tibble with an added class of 'fst_class_spec_tbl'
#'
#' @importFrom parsnip linear_reg cubist_rules poisson_reg survival_reg
#'
#' @name fast_classification_parsnip_spec_tbl
NULL

#' @export
#' @rdname fast_classification_parsnip_spec_tbl

fast_classification_parsnip_spec_tbl <- function(.parsnip_fns = "all",
                                             .parsnip_eng = "all") {
  
  # Thank you https://stackoverflow.com/questions/74691333/build-a-tibble-of-parsnip-model-calls-with-match-fun/74691529#74691529
  # Tidyeval ----
  call <- list(.parsnip_fns) %>%
    purrr::flatten_chr()
  engine <- list(.parsnip_eng) %>%
    purrr::flatten_chr()
  
  # Make tibble
  mod_tbl <- tibble::tribble(
    ~.parsnip_engine, ~.parsnip_mode, ~.parsnip_fns,
    "earth","classification","bag_mars",
    "earth","classification","discrim_flexible",
    "dbarts","classification","bart",
    "MASS","classification","discrim_linear",
    "mda","classification","discrim_linear",
    "sda","classification","discrim_linear",
    "sparsediscrim","classification","discrim_linear",
    "MASS","classification","discrim_quad",
    "sparsediscrim","classification","discrim_quad",
    "klaR","classification","discrim_regularized",
    "mgcv","classification","gen_additive_mod",
    "brulee","classification","logistic_reg",
    "gee","classification","logistic_reg",
    "glm","classification","logistic_reg",
    "glmer","classification","logistic_reg",
    "glmnet","classification","logistic_reg",
    "LiblineaR","classification","logistic_reg",
    "earth","classification","mars",
    "brulee","classification","mlp",
    "nnet","classification","mlp",
    "brulee","classification","multinom_reg",
    "glmnet","classification","multinom_reg",
    "nnet","classification","multinom_reg",
    "klaR","classification","naive_Bayes",
    "kknn","classification","nearest_neighbor",
    "mixOmics","classification","pls",
    "xrf","classification","rule_fit",
    "kernlab","classification","svm_linear",
    "LiblineaR","classification","svm_linear",
    "kernlab","classification","svm_poly",
    "kernlab","classification","svm_rbf",
    "liquidSVM","classification","svm_rbf"
  )
  
  # Filter ----
  if (!"all" %in% engine){
    mod_tbl <- mod_tbl %>%
      dplyr::filter(.parsnip_engine %in% engine)
  }
  
  if (!"all" %in% call){
    mod_tbl <- mod_tbl %>%
      dplyr::filter(.parsnip_fns %in% call)
  }
  
  mod_filtered_tbl <- mod_tbl
  
  mod_spec_tbl <- mod_filtered_tbl %>%
    dplyr::mutate(
      model_spec = purrr::pmap(
        dplyr::cur_data(),
        ~ match.fun(..3)(mode = ..2, engine = ..1)
        #~ get(..3)(mode = ..2, engine = ..1)
      )
    ) %>%
    # add .model_id column
    dplyr::mutate(.model_id = dplyr::row_number()) %>%
    dplyr::select(.model_id, dplyr::everything())
  
  # Return ----
  class(mod_spec_tbl) <- c("fst_class_spec_tbl", class(mod_spec_tbl))
  class(mod_spec_tbl) <- c("tidyaml_mod_spec_tbl", class(mod_spec_tbl))
  attr(mod_spec_tbl, ".parsnip_engines") <- .parsnip_eng
  attr(mod_spec_tbl, ".parsnip_functions") <- .parsnip_fns
  
  return(mod_spec_tbl)
  
}

Examples:

> fast_classification_parsnip_spec_tbl(.parsnip_fns = "logistic_reg")
# A tibble: 6 × 5
  .model_id .parsnip_engine .parsnip_mode  .parsnip_fns model_spec
      <int> <chr>           <chr>          <chr>        <list>    
1         1 brulee          classification logistic_reg <spec[+]> 
2         2 gee             classification logistic_reg <spec[+]> 
3         3 glm             classification logistic_reg <spec[+]> 
4         4 glmer           classification logistic_reg <spec[+]> 
5         5 glmnet          classification logistic_reg <spec[+]> 
6         6 LiblineaR       classification logistic_reg <spec[+]> 
> fast_classification_parsnip_spec_tbl(.parsnip_eng = c("earth","dbarts"))
# A tibble: 4 × 5
  .model_id .parsnip_engine .parsnip_mode  .parsnip_fns     model_spec
      <int> <chr>           <chr>          <chr>            <list>    
1         1 earth           classification bag_mars         <spec[+]> 
2         2 earth           classification discrim_flexible <spec[+]> 
3         3 dbarts          classification bart             <spec[+]> 
4         4 earth           classification mars             <spec[+]> 
@spsanderson spsanderson added the enhancement New feature or request label Dec 29, 2022
@spsanderson spsanderson added this to the tidyAML 0.0.1 milestone Dec 29, 2022
@spsanderson spsanderson self-assigned this Dec 29, 2022
@spsanderson
Copy link
Owner Author

Pick up here: https://www.tidymodels.org/find/parsnip/

Page 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

1 participant