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

wflw #30

Closed
Tracked by #27
spsanderson opened this issue Dec 8, 2022 · 0 comments
Closed
Tracked by #27

wflw #30

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

Comments

@spsanderson
Copy link
Owner

spsanderson commented Dec 8, 2022

Function:

# Safely make workflow
internal_make_wflw <- function(.model_tbl, .rec_obj){
  
  # Tidyeval ----
  model_tbl <- .model_tbl
  rec_obj <- .rec_obj
  
  # Checks ----
  if (!inherits(model_tbl, "tidyaml_mod_spec_tbl")){
    rlang::abort(
      message = "'.model_tbl' must inherit a class of 'tidyaml_mod_spec_tbl",
      use_cli_format = TRUE
    )
  }
  
  # Manipulation
  model_factor_tbl <- model_tbl %>%
    dplyr::mutate(.model_id = forcats::as_factor(.model_id)) %>%
    dplyr::mutate(rec_obj = list(rec_obj))
  
  # Make a group split object list
  models_list <- model_factor_tbl %>%
    dplyr::group_split(.model_id)
  
  # Make the Workflow Object using purrr imap
  wflw_list <- models_list %>%
    purrr::imap(
      .f = function(obj, id){
        
        # Pull the model column and then pluck the model
        mod <- obj %>% dplyr::pull(5) %>% purrr::pluck(1)
        
        # PUll the recipe column and then pluck the recipe
        rec_obj <- obj %>% dplyr::pull(6) %>% purrr::pluck(1)
        
        # Create a safe add_model function
        safe_add_model <- purrr::safely(
          workflows::add_model,
          otherwise = "Error - Could not make workflow object.",
          quiet = FALSE
        )
        
        # Return the workflow object with recipe and model
        ret <- workflows::workflow() %>%
          workflows::add_recipe(rec_obj) %>%
          safe_add_model(mod)
        
        # Pluck the result
        res <- ret %>% purrr::pluck("result")
        
        # Return the result
        return(res)
      }
    )
  
  
  # Return
  return(wflw_list)
}

Example:

library(tidyverse)
library(tidymodels)
tidymodels_prefer()

mod_spec_tbl <- fast_regression_parsnip_spec_tbl(
  .parsnip_fns = "linear_reg", 
  .parsnip_eng = c("lm","glm")
)

# A tibble: 2 × 5
  .model_id .parsnip_engine .parsnip_mode .parsnip_fns model_spec
      <int> <chr>           <chr>         <chr>        <list>    
1         1 lm              regression    linear_reg   <spec[+]> 
2         2 glm             regression    linear_reg   <spec[+]> 

# Generate Workflow object
mod_tbl <- mod_spec_tbl %>%
  dplyr::mutate(
    wflw = internal_make_wflw(mod_spec_tbl, .rec_obj = rec_obj)
  )

> mod_tbl
# A tibble: 2 × 6
  .model_id .parsnip_engine .parsnip_mode .parsnip_fns model_spec wflw      
      <int> <chr>           <chr>         <chr>        <list>     <list>    
1         1 lm              regression    linear_reg   <spec[+]>  <workflow>
2         2 glm             regression    linear_reg   <spec[+]>  <workflow>
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