|
| 1 | +#' Collapse Predictors into a single list-column |
| 2 | +#' |
| 3 | +#' `step_collapse()` creates a a *specification* of a recipe step that will |
| 4 | +#' convert a group of predictors into a single list-column. This is useful |
| 5 | +#' for custom models that need the predictors in a different format. |
| 6 | +#' |
| 7 | +#' @param recipe A recipe object. The step will be added to the sequence of |
| 8 | +#' operations for this recipe. |
| 9 | +#' @param ... One or more selector functions to choose which variables are |
| 10 | +#' affected by the step. See `[selections()]` for more details. For the `tidy` |
| 11 | +#' method, these are not currently used. |
| 12 | +#' @param role For model terms created by this step, what analysis role should |
| 13 | +#' they be assigned?. By default, the new columns are used as predictors. |
| 14 | +#' @param trained A logical to indicate if the quantities for preprocessing |
| 15 | +#' have been estimated. |
| 16 | +#' @param columns A character string of the selected variable names. This is |
| 17 | +#' `NULL` until the step is trained by `[prep.recipe()]`. |
| 18 | +#' @param new_col A character string for the name of the new list-column. The |
| 19 | +#' default is "predictor_matrix". |
| 20 | +#' @param skip A logical. Should the step be skipped when the recipe is |
| 21 | +#' baked by `[bake.recipe()]`? While all operations are baked when `prep` is run, |
| 22 | +#' skipping when `bake` is run may be other times when it is desirable to |
| 23 | +#' skip a processing step. |
| 24 | +#' @param id A character string that is unique to this step to identify it. |
| 25 | +#' |
| 26 | +#' @return An updated version of `recipe` with the new step added to the |
| 27 | +#' sequence of existing steps (if any). For the `tidy` method, a tibble with |
| 28 | +#' columns `terms` which is the columns that are affected and `value` which is |
| 29 | +#' the type of collapse. |
| 30 | +#' |
| 31 | +#' @examples |
| 32 | +#' library(recipes) |
| 33 | +#' |
| 34 | +#' # 2 predictors |
| 35 | +#' dat <- data.frame( |
| 36 | +#' x1 = 1:10, |
| 37 | +#' x2 = 11:20, |
| 38 | +#' y = 1:10 |
| 39 | +#' ) |
| 40 | +#' |
| 41 | +#' rec <- recipe(y ~ ., data = dat) %>% |
| 42 | +#' step_collapse(x1, x2, new_col = "pred") %>% |
| 43 | +#' prep() |
| 44 | +#' |
| 45 | +#' bake(rec, new_data = NULL) |
| 46 | +#' @importFrom recipes prep bake |
| 47 | +#' @export |
| 48 | +step_collapse <- function( |
| 49 | + recipe, |
| 50 | + ..., |
| 51 | + role = "predictor", |
| 52 | + trained = FALSE, |
| 53 | + columns = NULL, |
| 54 | + new_col = "predictor_matrix", |
| 55 | + skip = FALSE, |
| 56 | + id = recipes::rand_id("collapse") |
| 57 | +) { |
| 58 | + recipes::add_step( |
| 59 | + recipe, |
| 60 | + step_collapse_new( |
| 61 | + terms = enquos(...), |
| 62 | + role = role, |
| 63 | + trained = trained, |
| 64 | + columns = columns, |
| 65 | + new_col = new_col, |
| 66 | + skip = skip, |
| 67 | + id = id |
| 68 | + ) |
| 69 | + ) |
| 70 | +} |
| 71 | + |
| 72 | +step_collapse_new <- function( |
| 73 | + terms, |
| 74 | + role, |
| 75 | + trained, |
| 76 | + columns, |
| 77 | + new_col, |
| 78 | + skip, |
| 79 | + id |
| 80 | +) { |
| 81 | + recipes::step( |
| 82 | + subclass = "collapse", |
| 83 | + terms = terms, |
| 84 | + role = role, |
| 85 | + trained = trained, |
| 86 | + columns = columns, |
| 87 | + new_col = new_col, |
| 88 | + skip = skip, |
| 89 | + id = id |
| 90 | + ) |
| 91 | +} |
| 92 | + |
| 93 | +#' @export |
| 94 | +prep.step_collapse <- function(x, training, info = NULL, ...) { |
| 95 | + col_names <- recipes::recipes_eval_select(x$terms, training, info) |
| 96 | + |
| 97 | + step_collapse_new( |
| 98 | + terms = x$terms, |
| 99 | + role = x$role, |
| 100 | + trained = TRUE, |
| 101 | + columns = col_names, |
| 102 | + new_col = x$new_col, |
| 103 | + skip = x$skip, |
| 104 | + id = x$id |
| 105 | + ) |
| 106 | +} |
| 107 | + |
| 108 | +#' @export |
| 109 | +bake.step_collapse <- function(object, new_data, ...) { |
| 110 | + recipes::check_new_data(object$columns, object, new_data) |
| 111 | + |
| 112 | + rows_list <- apply( |
| 113 | + new_data[, object$columns, drop = FALSE], |
| 114 | + 1, |
| 115 | + function(row) matrix(row, nrow = 1), |
| 116 | + simplify = FALSE |
| 117 | + ) |
| 118 | + |
| 119 | + new_data[[object$new_col]] <- rows_list |
| 120 | + |
| 121 | + # drop original predictor columns |
| 122 | + new_data <- new_data[, setdiff(names(new_data), object$columns), drop = FALSE] |
| 123 | + |
| 124 | + new_data |
| 125 | +} |
| 126 | + |
| 127 | +#' @export |
| 128 | +print.step_collapse <- function(x, ...) { |
| 129 | + if (is.null(x$columns)) { |
| 130 | + cat("Collapse predictors into list-column (unprepped)\\n") |
| 131 | + } else { |
| 132 | + cat( |
| 133 | + "Collapse predictors into list-column:", |
| 134 | + paste(x$columns, collapse = ", "), |
| 135 | + " -> ", |
| 136 | + x$new_col, |
| 137 | + "\\n" |
| 138 | + ) |
| 139 | + } |
| 140 | + invisible(x) |
| 141 | +} |
0 commit comments