Enabling options within functions based on recipe (tidymodels)

Hi,
I want to create a function including training based on tidymodels, where I want to allow various options for pre-processing (e.g., scaling or not) and models (e.g., linear regression versus logistic regression). How is this best achieved in code? See some example code below, where I now just comment out certain aspects of the code; but would instead want to make this available for the user within the function.

trainfunction <- function(y, x, na=TRUE, model) {
 recipe <-
    recipe(y ~ .,
                    data = dataframe) %>%
    recipes::step_BoxCox(all_predictors()) %>%
    recipes::step_naomit(V1, skip = TRUE) %>%
    #recipes::step_center(all_predictors()) %>%
    recipes::step_scale(all_predictors())


  model <-
    parsnip::linear_reg(penalty = tune(), mixture = tune()) %>%
    #parsnip::logistic_reg(mode = "classification", penalty = tune(), mixture = tune()) %>%
    parsnip::set_engine("glmnet")
}

...

Thanks in advance

you provide parametes to allow steps to not be done

trainfunction <- function(y, x, na=TRUE, model,step_center_all = TRUE) {
 recipe <-
    recipe(y ~ .,
                    data = dataframe) %>%
    recipes::step_BoxCox(all_predictors()) %>%
    recipes::step_naomit(V1, skip = TRUE) 
if(step_center_all){
  recipe <-  recipes::step_center(recipe,all_predictors()) 
}
recipe <- recipes::step_scale(recipe,all_predictors())

...

then the user can provide step_center_all=TRUE, or step_center_all=FALSE for the different effects, or leave it blank for the TRUE default case.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.