spsanderson / tidyAML

Auto ML for the tidyverse
http://www.spsanderson.com/tidyAML/
Other
64 stars 7 forks source link

`make_full_function(.rec_obj)` #144

Closed spsanderson closed 1 year ago

spsanderson commented 1 year ago
outcome_col <- rec_obj$var_info |> filter(role == "outcome") |> pull(variable)
pred_cols <- rec_obj$var_info |> filter(role == "predictor") |> pull(variable)

as.formula(paste(outcome_col, " ~ ", paste(pred_cols, collapse= "+")))

Or something like:

reformulate(pred_cols, outcome_col)

> linear_reg() %>% 
+   set_engine("gee") %>% 
+   fit(reformulate(pred_cols, outcome_col), data = mtcars)
Error in `gee_formula()`:
! There should be a single 'id' column specified using the `id_vars()` function (e.g. `y ~ x + id_vars(id_col)`
Run `rlang::last_trace()` to see where the error occurred.

Or something like this: https://community.rstudio.com/t/extract-formula-from-recipes-recipe-object/2728/2

function(x) {
  x <- summary(x)
  x_vars <- x$variable[x$role == "predictor"]
  y_vars <- x$variable[x$role == "outcome"]  

  x_vars <- paste0(x_vars, collapse = "+")
  y_vars <- paste0(y_vars, collapse = "+")

  as.formula(paste(y_vars, x_vars, sep = "~"))
}

Might also be able to use something like formula(rec_obj) since formula is a generic and tidymodels seems to want to use that

spsanderson commented 1 year ago

This unecessary as the generic formula will extract the formula from the recipe object.