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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
#' Imputation via Bagged Trees
#'
#' `step_bagimpute` creates a *specification* of a recipe step that will
#' create bagged tree models to impute missing data.
#'
#' @inheritParams step_center
#' @inherit step_center return
#' @param ... One or more selector functions to choose variables. For
#' `step_bagimpute`, this indicates the variables to be imputed. When used with
#' `imp_vars`, the dots indicate which variables are used to predict the
#' missing data in each variable. See [selections()] for more details. For the
#' `tidy` method, these are not currently used.
#' @param role Not used by this step since no new variables are created.
#' @param impute_with A call to `imp_vars` to specify which variables are used
#' to impute the variables that can include specific variable names separated
#' by commas or different selectors (see [selections()]). If a column is
#' included in both lists to be imputed and to be an imputation predictor, it
#' will be removed from the latter and not used to impute itself.
#' @param trees An integer for the number of bagged trees to use in each model.
#' @param options A list of options to [ipred::ipredbagg()]. Defaults are set
#' for the arguments `nbagg` and `keepX` but others can be passed in. **Note**
#' that the arguments `X` and `y` should not be passed here.
#' @param seed_val An integer used to create reproducible models. The same seed
#' is used across all imputation models.
#' @param models The [ipred::ipredbagg()] objects are stored here once this
#' bagged trees have be trained by [prep.recipe()].
#' @return An updated version of `recipe` with the new step added to the
#' sequence of existing steps (if any). For the `tidy` method, a tibble with
#' columns `terms` (the selectors or variables selected) and `model` (the
#' bagged tree object).
#' @keywords datagen
#' @concept preprocessing
#' @concept imputation
#' @export
#' @details For each variable requiring imputation, a bagged tree is created
#' where the outcome is the variable of interest and the predictors are any
#' other variables listed in the `impute_with` formula. One advantage to the
#' bagged tree is that is can accept predictors that have missing values
#' themselves. This imputation method can be used when the variable of interest
#' (and predictors) are numeric or categorical. Imputed categorical variables
#' will remain categorical. Also, integers will be imputed to integer too.
#'
#' Note that if a variable that is to be imputed is also in `impute_with`,
#' this variable will be ignored.
#'
#' It is possible that missing values will still occur after imputation if a
#' large majority (or all) of the imputing variables are also missing.
#' @references Kuhn, M. and Johnson, K. (2013). *Applied Predictive Modeling*.
#' Springer Verlag.
#' @examples
#' library(modeldata)
#' data("credit_data")
#'
#' ## missing data per column
#' vapply(credit_data, function(x) mean(is.na(x)), c(num = 0))
#'
#' set.seed(342)
#' in_training <- sample(1:nrow(credit_data), 2000)
#'
#' credit_tr <- credit_data[ in_training, ]
#' credit_te <- credit_data[-in_training, ]
#' missing_examples <- c(14, 394, 565)
#'
#' rec <- recipe(Price ~ ., data = credit_tr)
#' \dontrun{
#' impute_rec <- rec %>%
#' step_bagimpute(Status, Home, Marital, Job, Income, Assets, Debt)
#'
#' imp_models <- prep(impute_rec, training = credit_tr)
#'
#' imputed_te <- bake(imp_models, new_data = credit_te, everything())
#'
#' credit_te[missing_examples,]
#' imputed_te[missing_examples, names(credit_te)]
#'
#' tidy(impute_rec, number = 1)
#' tidy(imp_models, number = 1)
#'
#' ## Specifying which variables to imputate with
#'
#' impute_rec <- rec %>%
#' step_bagimpute(Status, Home, Marital, Job, Income, Assets, Debt,
#' impute_with = imp_vars(Time, Age, Expenses),
#' # for quick execution, nbagg lowered
#' options = list(nbagg = 5, keepX = FALSE))
#'
#' imp_models <- prep(impute_rec, training = credit_tr)
#'
#' imputed_te <- bake(imp_models, new_data = credit_te, everything())
#'
#' credit_te[missing_examples,]
#' imputed_te[missing_examples, names(credit_te)]
#'
#' tidy(impute_rec, number = 1)
#' tidy(imp_models, number = 1)
#' }
step_bagimpute <-
function(recipe,
...,
role = NA,
trained = FALSE,
impute_with = imp_vars(all_predictors()),
trees = 25,
models = NULL,
options = list(keepX = FALSE),
seed_val = sample.int(10 ^ 4, 1),
skip = FALSE,
id = rand_id("bagimpute")) {
if (is.null(impute_with))
rlang::abort("Please list some variables in `impute_with`")
add_step(
recipe,
step_bagimpute_new(
terms = ellipse_check(...),
role = role,
trained = trained,
impute_with = impute_with,
trees = trees,
models = models,
options = options,
seed_val = seed_val,
skip = skip,
id = id
)
)
}
step_bagimpute_new <-
function(terms, role, trained, models, options, impute_with, trees,
seed_val, skip, id) {
step(
subclass = "bagimpute",
terms = terms,
role = role,
trained = trained,
impute_with = impute_with,
trees = trees,
models = models,
options = options,
seed_val = seed_val,
skip = skip,
id = id
)
}
bag_wrap <- function(vars, dat, opt, seed_val) {
seed_val <- seed_val[1]
dat <- as.data.frame(dat[, c(vars$y, vars$x)])
if (!is.null(seed_val) && !is.na(seed_val))
set.seed(seed_val)
out <- do.call("ipredbagg",
c(list(y = dat[, vars$y],
X = dat[, vars$x, drop = FALSE]),
opt))
out$..imp_vars <- vars$x
out
}
## This figures out which data should be used to predict each variable
## scheduled for imputation
impute_var_lists <- function(to_impute, impute_using, training, info) {
to_impute <- eval_select_recipes(to_impute, training, info)
impute_using <- eval_select_recipes(impute_using, training, info)
var_lists <- vector(mode = "list", length = length(to_impute))
for (i in seq_along(var_lists)) {
var_lists[[i]] <- list(y = to_impute[i],
x = impute_using[!(impute_using %in% to_impute[i])])
}
var_lists
}
#' @export
prep.step_bagimpute <- function(x, training, info = NULL, ...) {
var_lists <-
impute_var_lists(
to_impute = x$terms,
impute_using = x$impute_with,
training = training,
info = info
)
opt <- x$options
opt$nbagg <- x$trees
x$models <- lapply(
var_lists,
bag_wrap,
dat = training,
opt = opt,
seed_val = x$seed_val
)
names(x$models) <- vapply(var_lists, function(x) x$y, c(""))
step_bagimpute_new(
terms = x$terms,
role = x$role,
trained = TRUE,
models = x$models,
options = x$options,
impute_with = x$impute_with,
trees = x$trees,
seed_val = x$seed_val,
skip = x$skip,
id = x$id
)
}
#' @export
bake.step_bagimpute <- function(object, new_data, ...) {
missing_rows <- !complete.cases(new_data)
if (!any(missing_rows))
return(new_data)
old_data <- new_data
for (i in seq(along.with = object$models)) {
imp_var <- names(object$models)[i]
missing_rows <- !complete.cases(new_data[, imp_var])
if (any(missing_rows)) {
preds <- object$models[[imp_var]]$..imp_vars
pred_data <- old_data[missing_rows, preds, drop = FALSE]
## do a better job of checking this:
if (all(is.na(pred_data))) {
rlang::warn("All predictors are missing; cannot impute")
} else {
pred_vals <- predict(object$models[[imp_var]], pred_data)
pred_vals <- cast(pred_vals, new_data[[imp_var]])
new_data[missing_rows, imp_var] <- pred_vals
}
}
}
## changes character to factor!
as_tibble(new_data)
}
print.step_bagimpute <-
function(x, width = max(20, options()$width - 31), ...) {
cat("Bagged tree imputation for ", sep = "")
printer(names(x$models), x$terms, x$trained, width = width)
invisible(x)
}
#' @export
#' @rdname step_bagimpute
imp_vars <- function(...) quos(...)
#' @rdname step_bagimpute
#' @param x A `step_bagimpute` object.
#' @export
tidy.step_bagimpute <- function(x, ...) {
if (is_trained(x)) {
res <- tibble(terms = names(x$models),
model = x$models)
} else {
term_names <- sel2char(x$terms)
res <- tibble(terms = term_names, model = NA)
}
res$id <- x$id
res
}
# ------------------------------------------------------------------------------
#' @rdname tunable.step
#' @export
tunable.step_bagimpute <- function(x, ...) {
tibble::tibble(
name = "trees",
call_info = list(list(pkg = "dials", fun = "trees", range = c(5L, 25L))),
source = "recipe",
component = "step_bagimpute",
component_id = x$id
)
}
|