1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
#' Time Feature Generator
#'
#' `step_time()` creates a *specification* of a recipe
#' step that will convert date-time data into one or more factor or
#' numeric variables.
#'
#' @inheritParams step_pca
#' @inheritParams step_center
#' @param ... One or more selector functions to choose variables
#' for this step. The selected variables should have class
#' `POSIXct` or `POSIXlt`. See [selections()] for more details.
#' @param features A character string that includes at least one
#' of the following values: `am` (is is AM), `hour`, `hour12`, `minute`,
#' `second`, `decimal_day`.
#' @param columns A character string of variables that will be
#' used as inputs. This field is a placeholder and will be
#' populated once [prep()] is used.
#' @param keep_original_cols A logical to keep the original variables in the
#' output. Defaults to `TRUE`.
#' @template step-return
#' @family dummy variable and encoding steps
#' @export
#' @details Unlike some other steps, `step_time()` does *not*
#' remove the original time variables by default. Set `keep_original_cols`
#' to `FALSE` to remove them.
#'
#' `decimal_day` return time of day as a decimal number between 0 and 24. for
#' example `"07:15:00"` would be transformed to `7.25` and `"03:59:59"` would
#' be transformed to `3.999722`. The formula for these calculations are
#' `hour(x) + (second(x) + minute(x) * 60) / 3600`.
#'
#' See [step_date()] if you want to calculate features that are larger than
#' hours.
#'
#' # Tidying
#'
#' When you [`tidy()`][tidy.recipe()] this step, a tibble with columns
#' `terms` (the selectors or variables selected) and `value` (the feature
#' names).
#'
#' @examples
#' library(lubridate)
#'
#' examples <- data.frame(
#' times = ymd_hms("2022-05-06 23:51:07") +
#' hours(1:5) + minutes(1:5) + seconds(1:5)
#' )
#' time_rec <- recipe(~ times, examples) %>%
#' step_time(all_predictors())
#'
#' tidy(time_rec, number = 1)
#'
#' time_rec <- prep(time_rec, training = examples)
#'
#' time_values <- bake(time_rec, new_data = examples)
#' time_values
#'
#' tidy(time_rec, number = 1)
step_time <-
function(recipe,
...,
role = "predictor",
trained = FALSE,
features = c("hour", "minute", "second"),
columns = NULL,
keep_original_cols = TRUE,
skip = FALSE,
id = rand_id("time")) {
feat <-
c(
"am",
"hour",
"hour12",
"minute",
"second",
"decimal_day"
)
if (!is_tune(features)) {
if (!all(features %in% feat)) {
rlang::abort(paste0(
"Possible values of `features` should include: ",
paste0("'", feat, "'", collapse = ", ")
))
}
}
add_step(
recipe,
step_time_new(
terms = enquos(...),
role = role,
trained = trained,
features = features,
columns = columns,
keep_original_cols = keep_original_cols,
skip = skip,
id = id
)
)
}
step_time_new <-
function(terms, role, trained, features, columns, keep_original_cols, skip,
id) {
step(
subclass = "time",
terms = terms,
role = role,
trained = trained,
features = features,
columns = columns,
keep_original_cols = keep_original_cols,
skip = skip,
id = id
)
}
#' @export
prep.step_time <- function(x, training, info = NULL, ...) {
col_names <- recipes_eval_select(x$terms, training, info)
check_type(training[, col_names], types = "datetime")
step_time_new(
terms = x$terms,
role = x$role,
trained = TRUE,
features = x$features,
columns = col_names,
keep_original_cols = get_keep_original_cols(x),
skip = x$skip,
id = x$id
)
}
#' @export
bake.step_time <- function(object, new_data, ...) {
check_new_data(names(object$columns), object, new_data)
for (column in object$columns) {
time_values <- get_time_features(
dt = new_data[[column]],
feats = object$features
)
names(time_values) <- glue::glue("{column}_{names(time_values)}")
new_data <- bind_cols(new_data, time_values)
}
keep_original_cols <- get_keep_original_cols(object)
if (!keep_original_cols) {
new_data <- new_data[, !(colnames(new_data) %in% object$columns), drop = FALSE]
}
new_data
}
get_time_features <- function(dt, feats) {
features <- list(
am = am,
hour = hour,
hour12 = function(x) {
out <- hour(x)
out <- ifelse(out == 0L, 12L, out)
out <- ifelse(out > 12L, out - 12L, out)
out
},
minute = minute,
second = second,
decimal_day = function(x) hour(x) + (second(x) + minute(x) * 60) / 3600
)
purrr::map_dfc(features[feats], ~.x(dt))
}
print.step_time <-
function(x, width = max(20, options()$width - 29), ...) {
title <- "Time features from "
print_step(x$columns, x$terms, x$trained, title, width)
invisible(x)
}
#' @rdname tidy.recipe
#' @export
tidy.step_time <- function(x, ...) {
if (is_trained(x)) {
res <- tidyr::crossing(
terms = unname(x$columns),
value = x$features
)
} else {
term_names <- sel2char(x$terms)
res <- tidyr::crossing(
terms = term_names,
value = x$features
)
}
tibble::add_column(res, id = x$id)
}
|