|
| 1 | +#' Check the dataset contains enough data points. |
| 2 | +#' |
| 3 | +#' `check_enough_data` creates a *specification* of a recipe |
| 4 | +#' operation that will check if variables contain enough data. |
| 5 | +#' |
| 6 | +#' @param recipe A recipe object. The check will be added to the |
| 7 | +#' sequence of operations for this recipe. |
| 8 | +#' @param ... One or more selector functions to choose variables for this check. |
| 9 | +#' See [selections()] for more details. You will usually want to use |
| 10 | +#' [recipes::all_predictors()] and/or [recipes::all_outcomes()] here. |
| 11 | +#' @param min_observations The minimum number of data points required for |
| 12 | +#' training. If this is NULL, the total number of predictors will be used. |
| 13 | +#' @param epi_keys A character vector of column names on which to group the data |
| 14 | +#' and check threshold within each group. Useful if your forecaster trains |
| 15 | +#' per group (for example, per geo_value). |
| 16 | +#' @param drop_na A logical for whether to count NA values as valid rows. |
| 17 | +#' @param role Not used by this check since no new variables are |
| 18 | +#' created. |
| 19 | +#' @param trained A logical for whether the selectors in `...` |
| 20 | +#' have been resolved by [prep()]. |
| 21 | +#' @param id A character string that is unique to this check to identify it. |
| 22 | +#' @param skip A logical. If `TRUE`, only training data is checked, while if |
| 23 | +#' `FALSE`, both training and predicting data is checked. Technically, this |
| 24 | +#' answers the question "should the check be skipped when the recipe is baked |
| 25 | +#' by [bake()]?" While all operations are baked when [prep()] is run, some |
| 26 | +#' operations may not be able to be conducted on new data (e.g. processing the |
| 27 | +#' outcome variable(s)). Care should be taken when using `skip = TRUE` as it |
| 28 | +#' may affect the computations for subsequent operations. |
| 29 | +#' @family checks |
| 30 | +#' @export |
| 31 | +#' @details This check will break the `prep` and/or bake function if any of the |
| 32 | +#' checked columns have not enough non-NA values. If the check passes, nothing |
| 33 | +#' is changed in the data. It is best used after every other step. |
| 34 | +#' |
| 35 | +#' For checking training data, it is best to set `...` to be |
| 36 | +#' `all_predictors(), all_outcomes()`, while for checking prediction data, it |
| 37 | +#' is best to set `...` to be `all_predictors()` only, with `n = 1`. |
| 38 | +#' |
| 39 | +#' # tidy() results |
| 40 | +#' |
| 41 | +#' When you [`tidy()`][tidy.recipe()] this check, a tibble with column |
| 42 | +#' `terms` (the selectors or variables selected) is returned. |
| 43 | +#' |
| 44 | +check_enough_data <- |
| 45 | + function(recipe, |
| 46 | + ..., |
| 47 | + min_observations = NULL, |
| 48 | + epi_keys = NULL, |
| 49 | + drop_na = TRUE, |
| 50 | + role = NA, |
| 51 | + trained = FALSE, |
| 52 | + skip = TRUE, |
| 53 | + id = rand_id("enough_data")) { |
| 54 | + recipes::add_check( |
| 55 | + recipe, |
| 56 | + check_enough_data_new( |
| 57 | + min_observations = min_observations, |
| 58 | + epi_keys = epi_keys, |
| 59 | + drop_na = drop_na, |
| 60 | + terms = enquos(...), |
| 61 | + role = role, |
| 62 | + trained = trained, |
| 63 | + columns = NULL, |
| 64 | + skip = skip, |
| 65 | + id = id |
| 66 | + ) |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | +check_enough_data_new <- |
| 71 | + function(min_observations, epi_keys, drop_na, terms, |
| 72 | + role, trained, columns, skip, id) { |
| 73 | + recipes::check( |
| 74 | + subclass = "enough_data", |
| 75 | + prefix = "check_", |
| 76 | + min_observations = min_observations, |
| 77 | + epi_keys = epi_keys, |
| 78 | + drop_na = drop_na, |
| 79 | + terms = terms, |
| 80 | + role = role, |
| 81 | + trained = trained, |
| 82 | + columns = columns, |
| 83 | + skip = skip, |
| 84 | + id = id |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | +#' @export |
| 89 | +prep.check_enough_data <- function(x, training, info = NULL, ...) { |
| 90 | + col_names <- recipes::recipes_eval_select(x$terms, training, info) |
| 91 | + if (is.null(x$min_observations)) { |
| 92 | + x$min_observations <- length(col_names) |
| 93 | + } |
| 94 | + |
| 95 | + check_enough_data_core(training, x, col_names, "train") |
| 96 | + |
| 97 | + check_enough_data_new( |
| 98 | + min_observations = x$min_observations, |
| 99 | + epi_keys = x$epi_keys, |
| 100 | + drop_na = x$drop_na, |
| 101 | + terms = x$terms, |
| 102 | + role = x$role, |
| 103 | + trained = TRUE, |
| 104 | + columns = col_names, |
| 105 | + skip = x$skip, |
| 106 | + id = x$id |
| 107 | + ) |
| 108 | +} |
| 109 | + |
| 110 | +#' @export |
| 111 | +bake.check_enough_data <- function(object, new_data, ...) { |
| 112 | + col_names <- object$columns |
| 113 | + check_enough_data_core(new_data, object, col_names, "predict") |
| 114 | + new_data |
| 115 | +} |
| 116 | + |
| 117 | +#' @export |
| 118 | +print.check_enough_data <- function(x, width = max(20, options()$width - 30), ...) { |
| 119 | + title <- paste0("Check enough data (n = ", x$min_observations, ") for ") |
| 120 | + recipes::print_step(x$columns, x$terms, x$trained, title, width) |
| 121 | + invisible(x) |
| 122 | +} |
| 123 | + |
| 124 | +#' @export |
| 125 | +tidy.check_enough_data <- function(x, ...) { |
| 126 | + if (recipes::is_trained(x)) { |
| 127 | + res <- tibble(terms = unname(x$columns)) |
| 128 | + } else { |
| 129 | + res <- tibble(terms = recipes::sel2char(x$terms)) |
| 130 | + } |
| 131 | + res$id <- x$id |
| 132 | + res$min_observations <- x$min_observations |
| 133 | + res$epi_keys <- x$epi_keys |
| 134 | + res$drop_na <- x$drop_na |
| 135 | + res |
| 136 | +} |
| 137 | + |
| 138 | +check_enough_data_core <- function(epi_df, step_obj, col_names, train_or_predict) { |
| 139 | + epi_df <- epi_df %>% |
| 140 | + group_by(across(all_of(.env$step_obj$epi_keys))) |
| 141 | + if (step_obj$drop_na) { |
| 142 | + any_missing_data <- epi_df %>% |
| 143 | + mutate(any_are_na = rowSums(across(any_of(.env$col_names), ~ is.na(.x))) > 0) %>% |
| 144 | + # count the number of rows where they're all not na |
| 145 | + summarise(sum(any_are_na == 0) < .env$step_obj$min_observations, .groups = "drop") |
| 146 | + any_missing_data <- any_missing_data %>% |
| 147 | + summarize(across(all_of(setdiff(names(any_missing_data), step_obj$epi_keys)), any)) %>% |
| 148 | + any() |
| 149 | + |
| 150 | + # figuring out which individual columns (if any) are to blame for this dearth |
| 151 | + # of data |
| 152 | + cols_not_enough_data <- epi_df %>% |
| 153 | + summarise( |
| 154 | + across( |
| 155 | + all_of(.env$col_names), |
| 156 | + ~ sum(!is.na(.x)) < .env$step_obj$min_observations |
| 157 | + ), |
| 158 | + .groups = "drop" |
| 159 | + ) %>% |
| 160 | + # Aggregate across keys (if present) |
| 161 | + summarise(across(all_of(.env$col_names), any), .groups = "drop") %>% |
| 162 | + unlist() %>% |
| 163 | + # Select the names of the columns that are TRUE |
| 164 | + names(.)[.] |
| 165 | + |
| 166 | + # Either all columns have enough data, in which case this message won't be |
| 167 | + # sent later or none of the single columns have enough data, that means its |
| 168 | + # the combination of all of them. |
| 169 | + if (length(cols_not_enough_data) == 0) { |
| 170 | + cols_not_enough_data <- |
| 171 | + glue::glue("no single column, but the combination of {paste0(col_names, collapse = ', ')}") |
| 172 | + } |
| 173 | + } else { |
| 174 | + # if we're not dropping na values, just count |
| 175 | + cols_not_enough_data <- epi_df %>% |
| 176 | + summarise(across(all_of(.env$col_names), ~ dplyr::n() < .env$step_obj$min_observations)) |
| 177 | + any_missing_data <- cols_not_enough_data %>% |
| 178 | + summarize(across(all_of(.env$col_names), all)) %>% |
| 179 | + all() |
| 180 | + cols_not_enough_data <- cols_not_enough_data %>% |
| 181 | + summarise(across(all_of(.env$col_names), any), .groups = "drop") %>% |
| 182 | + unlist() %>% |
| 183 | + # Select the names of the columns that are TRUE |
| 184 | + names(.)[.] |
| 185 | + } |
| 186 | + |
| 187 | + if (any_missing_data) { |
| 188 | + cli_abort( |
| 189 | + "The following columns don't have enough data to {train_or_predict}: {cols_not_enough_data}.", |
| 190 | + class = "epipredict__not_enough_data" |
| 191 | + ) |
| 192 | + } |
| 193 | +} |
0 commit comments