|
| 1 | +#' Create a shifted predictor |
| 2 | +#' |
| 3 | +#' `step_epi_shift` creates a *specification* of a recipe step that |
| 4 | +#' will add new columns of shifted data. shifted data will |
| 5 | +#' by default include NA values where the shift was induced. |
| 6 | +#' These can be removed with [step_naomit()], or you may |
| 7 | +#' specify an alternative filler value with the `default` |
| 8 | +#' argument. |
| 9 | +#' |
| 10 | +#' @param shift A vector of integers. Each specified column will be |
| 11 | +#' shifted for each value in the vector. |
| 12 | +#' @template step-return |
| 13 | +#' |
| 14 | +#' @details The step assumes that the data are already _in the proper sequential |
| 15 | +#' order_ for shifting. |
| 16 | +#' |
| 17 | +#' @family row operation steps |
| 18 | +#' @rdname step_epi_ahead |
| 19 | +step_epi_shift <- |
| 20 | + function(recipe, |
| 21 | + ..., |
| 22 | + role, |
| 23 | + trained, |
| 24 | + shift, |
| 25 | + prefix, |
| 26 | + default, |
| 27 | + keys, |
| 28 | + columns, |
| 29 | + skip, |
| 30 | + id) { |
| 31 | + add_step( |
| 32 | + recipe, |
| 33 | + step_epi_shift_new( |
| 34 | + terms = dplyr::enquos(...), |
| 35 | + role = role, |
| 36 | + trained = trained, |
| 37 | + shift = shift, |
| 38 | + prefix = prefix, |
| 39 | + default = default, |
| 40 | + keys = keys, |
| 41 | + columns = columns, |
| 42 | + skip = skip, |
| 43 | + id = id |
| 44 | + ) |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | +step_epi_shift_new <- |
| 49 | + function(terms, role, trained, shift, prefix, default, keys, |
| 50 | + columns, skip, id) { |
| 51 | + step( |
| 52 | + subclass = "epi_shift", |
| 53 | + terms = terms, |
| 54 | + role = role, |
| 55 | + trained = trained, |
| 56 | + shift = shift, |
| 57 | + prefix = prefix, |
| 58 | + default = default, |
| 59 | + keys = keys, |
| 60 | + columns = columns, |
| 61 | + skip = skip, |
| 62 | + id = id |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | +#' @export |
| 67 | +prep.step_epi_shift <- function(x, training, info = NULL, ...) { |
| 68 | + step_epi_shift_new( |
| 69 | + terms = x$terms, |
| 70 | + role = x$role, |
| 71 | + trained = TRUE, |
| 72 | + shift = x$shift, |
| 73 | + prefix = x$prefix, |
| 74 | + default = x$default, |
| 75 | + keys = x$keys, |
| 76 | + columns = recipes_eval_select(x$terms, training, info), |
| 77 | + skip = x$skip, |
| 78 | + id = x$id |
| 79 | + ) |
| 80 | +} |
| 81 | + |
| 82 | +#' @export |
| 83 | +bake.step_epi_shift <- function(object, new_data, ...) { |
| 84 | + if (!all(object$shift == as.integer(object$shift))) { |
| 85 | + rlang::abort("step_epi_shift requires 'shift' argument to be integer valued.") |
| 86 | + } |
| 87 | + grid <- tidyr::expand_grid(col = object$columns, lag_val = -object$shift) |
| 88 | + is_lag <- object$role == "predictor" |
| 89 | + if (!is_lag) { |
| 90 | + grid <- dplyr::mutate(grid,ahead_val = -lag_val) |
| 91 | + } |
| 92 | + grid <- dplyr::mutate(grid, |
| 93 | + newname = glue::glue( |
| 94 | + paste0( |
| 95 | + "{object$prefix}", |
| 96 | + ifelse(is_lag,"{lag_val}","{ahead_val}"), |
| 97 | + "_{col}" |
| 98 | + ) |
| 99 | + ) |
| 100 | + ) |
| 101 | + if (!is_lag) { |
| 102 | + grid <- dplyr::select(grid, -ahead_val) |
| 103 | + } |
| 104 | + ## ensure no name clashes |
| 105 | + new_data_names <- colnames(new_data) |
| 106 | + intersection <- new_data_names %in% grid$newname |
| 107 | + if (any(intersection)) { |
| 108 | + rlang::abort( |
| 109 | + paste0("Name collision occured in `", class(object)[1], |
| 110 | + "`. The following variable names already exists: ", |
| 111 | + paste0(new_data_names[intersection], collapse = ", "), |
| 112 | + ".")) |
| 113 | + } |
| 114 | + ok <- object$keys |
| 115 | + shifted <- purrr::reduce( |
| 116 | + purrr::pmap(grid, epi_shift_single, x = new_data, key_cols = ok), |
| 117 | + dplyr::full_join, |
| 118 | + by = ok |
| 119 | + ) |
| 120 | + |
| 121 | + dplyr::full_join(new_data, shifted, by = ok) %>% |
| 122 | + dplyr::group_by(dplyr::across(dplyr::all_of(ok[-1]))) %>% |
| 123 | + dplyr::arrange(time_value) %>% |
| 124 | + dplyr::ungroup() |
| 125 | + |
| 126 | +} |
| 127 | + |
| 128 | +#' @export |
| 129 | +print.step_epi_shift <- |
| 130 | + function(x, width = max(20, options()$width - 30), ...) { |
| 131 | + ## TODO add printing of the shifts |
| 132 | + title <- ifelse(x$role == "predictor","Lagging","Leading") %>% |
| 133 | + paste0(": ", abs(x$shift),",") |
| 134 | + recipes::print_step(x$columns, x$terms, x$trained, title, width) |
| 135 | + invisible(x) |
| 136 | + } |
0 commit comments