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 create_model_spec() function #11

Closed
spsanderson opened this issue Dec 6, 2022 · 0 comments
Closed

Make create_model_spec() function #11

spsanderson opened this issue Dec 6, 2022 · 0 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@spsanderson
Copy link
Owner

spsanderson commented Dec 6, 2022

The purpose

The purpose of this function is to allow a user to create a model specification of any parsnip type or multiples of different types. It can return either a list of the model specifications or a tibble both of which have the same information.

Function:

library(purrr)
library(parsnip)

create_model_spec <- function(.parsnip_engine = list("lm"), 
                              .mode = list("regression"),
                              .parsnip_fns = list("linear_reg"),
                              .return_tibble = TRUE) {
  
  # Tidyeval ----
  engine <- .parsnip_engine %>%
    purrr::flatten_chr() %>%
    as.list()
  mode <- .mode %>%
    purrr::flatten_chr() %>%
    as.list()
  call <- .parsnip_fns %>%
    purrr::flatten_chr() %>%
    as.list()
  ret_tibble <- as.logical(.return_tibble)

  # Make Model list for purrr call
  model_spec_list <- list(
    call,
    engine,
    mode
  )

  # Use purrr pmap to make mode specs
  models <- purrr::pmap(
    .l = model_spec_list,
    .f = function(call, engine, mode) {
      match.fun(call)(engine = engine, mode = mode)
    }
  )

  # Return ----
  models_list <- list(
    .parsnip_engine = engine,
    .parsnip_mode = mode,
    .parsnip_fns = call,
    .model_spec = models
  )

  ret_tbl <- dplyr::tibble(
    .parsnip_engine = unlist(engine),
    .parsnip_mode   = unlist(mode),
    .parsnip_fns    = unlist(call),
    .model_spec     = models
  )

  ifelse(ret_tibble, return(ret_tbl), return(models_list))
}

Examples:

> create_model_spec(
+   .engine = list("lm","glm","glmnet","cubist"), 
+   .parsnip_fns = list(
+     rep("linear_reg", 3),
+     "cubist_rules")
+   )
# A tibble: 4 × 4
  .parsnip_engine .parsnip_mode .parsnip_fns .model_spec
  <chr>           <chr>         <chr>        <list>     
1 lm              regression    linear_reg   <spec[+]>  
2 glm             regression    linear_reg   <spec[+]>  
3 glmnet          regression    linear_reg   <spec[+]>  
4 cubist          regression    cubist_rules <spec[+]>  
> model_spec_list <- create_model_spec(
+   .engine = list("lm","glm","glmnet","cubist"), 
+   .parsnip_fns = list(
+     rep("linear_reg", 3),
+     "cubist_rules"),
+   .return_tibble = FALSE
+ )
> model_spec_list$.model_spec
[[1]]
Linear Regression Model Specification (regression)

Computational engine: lm 


[[2]]
Linear Regression Model Specification (regression)

Computational engine: glm 


[[3]]
Linear Regression Model Specification (regression)

Computational engine: glmnet 


[[4]]
! parsnip could not locate an implementation for `cubist_rules` regression model
  specifications using the `cubist` engine.

Cubist Model Specification (regression)

Computational engine: cubist 
@spsanderson spsanderson added the enhancement New feature or request label Dec 6, 2022
@spsanderson spsanderson added this to the tidyaml 0.0.1 milestone Dec 6, 2022
@spsanderson spsanderson self-assigned this Dec 6, 2022
spsanderson added a commit that referenced this issue Dec 6, 2022
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