|
| 1 | +#' Check Training Window Length |
| 2 | +#' |
| 3 | +#' `check_train_window` creates a *specification* of a recipe |
| 4 | +#' check that will check if there is insufficient training data |
| 5 | +#' |
| 6 | +#' @inheritParams check_missing |
| 7 | +#' @min_train_window Positive integer. The minimum amount of training |
| 8 | +#' data time points required |
| 9 | +#' to fit a predictive model. Using less results causes downstream |
| 10 | +#' fit calls to return minimal objects rather than crashing. |
| 11 | +#' @param warn If `TRUE` the check will throw a warning instead |
| 12 | +#' of an error when failing. |
| 13 | +#' @param train_window The number of days of training data. |
| 14 | +#' This is `NULL` until computed by [prep()]. |
| 15 | +#' @template check-return |
| 16 | +#' @family checks |
| 17 | +#' @export |
| 18 | +#' |
| 19 | +check_train_window <- |
| 20 | + function(recipe, |
| 21 | + ..., |
| 22 | + role = NA, |
| 23 | + skip = FALSE, |
| 24 | + trained = FALSE, |
| 25 | + min_train_window = 20, |
| 26 | + warn = TRUE, |
| 27 | + train_length, |
| 28 | + id = rand_id("train_window_check_")) { |
| 29 | + add_check( |
| 30 | + recipe, |
| 31 | + check_train_window_new( |
| 32 | + terms = dplyr::enquos(...), |
| 33 | + role = role, |
| 34 | + skip = skip, |
| 35 | + trained = trained, |
| 36 | + min_train_window = min_train_window, |
| 37 | + warn = warn, |
| 38 | + train_length = train_length, |
| 39 | + id = id |
| 40 | + ) |
| 41 | + ) |
| 42 | + } |
| 43 | + |
| 44 | +## Initializes a new object |
| 45 | +check_train_window_new <- |
| 46 | + function(terms, role, skip, trained, min_train_window, warn, |
| 47 | + train_length, id) { |
| 48 | + check( |
| 49 | + subclass = "train_window", |
| 50 | + terms = terms, |
| 51 | + role = role, |
| 52 | + skip = skip, |
| 53 | + trained = trained, |
| 54 | + min_train_window = min_train_window, |
| 55 | + warn = warn, |
| 56 | + train_length = train_length, |
| 57 | + id = id |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | +prep.check_train_window <- function(x, |
| 63 | + training, |
| 64 | + info = NULL, |
| 65 | + ...) { |
| 66 | + |
| 67 | + train_length <- nrow(training) |
| 68 | + |
| 69 | + |
| 70 | + check_train_window_new( |
| 71 | + terms = x$terms, |
| 72 | + role = x$role, |
| 73 | + trained = TRUE, |
| 74 | + skip = x$skip, |
| 75 | + warn = x$warn, |
| 76 | + min_train_window = min_train_window, |
| 77 | + warn = warn, |
| 78 | + train_length = train_length, |
| 79 | + id = x$id |
| 80 | + ) |
| 81 | +} |
| 82 | + |
| 83 | +bake.check_range <- function(object, |
| 84 | + new_data, |
| 85 | + ...) { |
| 86 | + |
| 87 | + mtw <- object$min_train_window |
| 88 | + stopifnot(is.numeric(mtw), length(mtw) == 1L, mtw == as.integer(mtw)) |
| 89 | + |
| 90 | + n <- nrow(new_data) |
| 91 | + n.complete <- sum(complete.cases(new_data)) |
| 92 | + |
| 93 | + msg <- NULL |
| 94 | + if (n < mtw) { |
| 95 | + msg <- paste0(msg, "Total available rows of data is ", n, |
| 96 | + "\n < min_train_window ", mtw, ".\n") |
| 97 | + } |
| 98 | + if (n.complete < mtw) { |
| 99 | + msg <- paste0(msg, "Total complete rows of data is ", n.complete, |
| 100 | + "\n < min_train_window ", mtw, ".\n") |
| 101 | + } |
| 102 | + |
| 103 | + if (object$warn & !is.null(msg)) { |
| 104 | + rlang::warn(msg) |
| 105 | + } else if (!is.null(msg)) { |
| 106 | + rlang::abort(msg) |
| 107 | + } |
| 108 | + |
| 109 | + as_tibble(new_data) |
| 110 | +} |
| 111 | + |
| 112 | +print.check_train_window <- |
| 113 | + function(x, width = max(20, options()$width - 30), ...) { |
| 114 | + title <- "Checking number of training observations" |
| 115 | + invisible(x) |
| 116 | + } |
| 117 | + |
| 118 | + |
0 commit comments