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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
#' Create a Profiling Version of a Data Set
#'
#' `step_profile` creates a *specification* of a recipe step that
#' will fix the levels of all variables but one and will create a
#' sequence of values for the remaining variable. This step can be
#' helpful when creating partial regression plots for additive
#' models.
#'
#' @inheritParams step_center
#' @inherit step_center return
#' @param ... One or more selector functions to choose which
#' variables will be fixed to a single value. 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 profile A call to [dplyr::vars()]) to specify which
#' variable will be profiled (see [selections()]). If a column is
#' included in both lists to be fixed and to be profiled, an error
#' is thrown.
#' @param pct A value between 0 and 1 that is the percentile to
#' fix continuous variables. This is applied to all continuous
#' variables captured by the selectors. For date variables, either
#' the minimum, median, or maximum used based on their distance to
#' `pct`.
#' @param index The level that qualitative variables will be
#' fixed. If the variables are character (not factors), this will
#' be the index of the sorted unique values. This is applied to all
#' qualitative variables captured by the selectors.
#' @param grid A named list with elements `pctl` (a logical) and
#' `len` (an integer). If `pctl = TRUE`, then `len` denotes how
#' many percentiles to use to create the profiling grid. This
#' creates a grid between 0 and 1 and the profile is determined by
#' the percentiles of the data. For example, if `pctl = TRUE` and
#' `len = 3`, the profile would contain the minimum, median, and
#' maximum values. If `pctl = FALSE`, it defines how many grid
#' points between the minimum and maximum values should be created.
#' This parameter is ignored for qualitative variables (since all
#' of their possible levels are profiled). In the case of date
#' variables, `pctl = FALSE` will always be used since there is no
#' quantile method for dates.
#' @param columns A character string that contains the names of
#' columns that should be fixed and their values. These values are
#' not determined until [prep.recipe()] is called.
#' @details This step is atypical in that, when baked, the
#' `new_data` argument is ignored; the resulting data set is
#' based on the fixed and profiled variable's information.
#' @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` (which
#' is the columns that will be affected), and `type` (fixed or
#' profiled).
#' @keywords datagen
#' @concept preprocessing
#' @export
#' @examples
#' library(modeldata)
#' data(okc)
#'
#' # Setup a grid across date but keep the other values fixed
#' recipe(~ diet + height + date, data = okc) %>%
#' step_profile(-date, profile = vars(date)) %>%
#' prep(training = okc) %>%
#' juice
#'
#'
#' ##########
#'
#' # An *additive* model; not for use when there are interactions or
#' # other functional relationships between predictors
#'
#' lin_mod <- lm(mpg ~ poly(disp, 2) + cyl + hp, data = mtcars)
#'
#' # Show the difference in the two grid creation methods
#'
#' disp_pctl <- recipe(~ disp + cyl + hp, data = mtcars) %>%
#' step_profile(-disp, profile = vars(disp)) %>%
#' prep(training = mtcars)
#'
#' disp_grid <- recipe(~ disp + cyl + hp, data = mtcars) %>%
#' step_profile(
#' -disp,
#' profile = vars(disp),
#' grid = list(pctl = FALSE, len = 100)
#' ) %>%
#' prep(training = mtcars)
#'
#' grid_data <- bake(disp_grid, new_data = NULL)
#' grid_data <- grid_data %>%
#' mutate(pred = predict(lin_mod, grid_data),
#' method = "grid")
#'
#' pctl_data <- bake(disp_pctl, new_data = NULL)
#' pctl_data <- pctl_data %>%
#' mutate(pred = predict(lin_mod, pctl_data),
#' method = "percentile")
#'
#' plot_data <- bind_rows(grid_data, pctl_data)
#'
#' library(ggplot2)
#'
#' ggplot(plot_data, aes(x = disp, y = pred)) +
#' geom_point(alpha = .5, cex = 1) +
#' facet_wrap(~ method)
step_profile <- function(recipe,
...,
profile = NULL,
pct = 0.5,
index = 1,
grid = list(pctl = TRUE, len = 100),
columns = NULL,
role = NA,
trained = FALSE,
skip = FALSE,
id = rand_id("profile")) {
if (pct < 0 | pct > 1)
rlang::abort("`pct should be on [0, 1]`")
if (length(grid) != 2)
rlang::abort("`grid` should have two named elements. See ?step_profile")
if (all(sort(names(grid)) == c("len", "ptcl")))
rlang::abort("`grid` should have two named elements. See ?step_profile")
if (grid$len < 2)
rlang::abort("`grid$len should be at least 2.`")
if (!is.logical(grid$pctl))
rlang::abort("`grid$pctl should be logical.`")
add_step(recipe,
step_profile_new(
terms = ellipse_check(...),
profile = profile,
pct = pct,
index = index,
grid = grid,
columns = columns,
role = role,
trained = trained,
skip = skip,
id = id
)
)
}
step_profile_new <-
function(terms, profile, pct, index, grid, columns, role, trained, skip, id) {
step(
subclass = "profile",
terms = terms,
profile = profile,
pct = pct,
index = index,
grid = grid,
columns = columns,
role = role,
trained = trained,
skip = skip,
id = id
)
}
#' @export
prep.step_profile <- function(x, training, info = NULL, ...) {
fixed_names <- eval_select_recipes(x$terms, training, info)
profile_name <- eval_select_recipes(x$profile, training, info)
if(length(fixed_names) == 0)
rlang::abort("At least one variable should be fixed")
if(length(profile_name) != 1)
rlang::abort("Only one variable should be profiled")
if(any(profile_name == fixed_names))
rlang::abort(
paste0(
"The profiled variable cannot be in the list of ",
"variables to be fixed."
)
)
fixed_vals <- lapply(
training[, fixed_names],
fixed,
pct = x$pct,
index = x$index
)
profile_vals <-
list(prof(training[[profile_name]], grid = x$grid))
names(profile_vals)[[1]] <- profile_name
step_profile_new(
terms = x$terms,
role = x$role,
profile = profile_vals,
pct = x$pct,
index = x$index,
grid = x$grid,
columns = fixed_vals,
trained = TRUE,
skip = x$skip,
id = x$id
)
}
#' @export
bake.step_profile <- function(object, new_data, ...) {
n <- length(object$profile[[1]])
new_data <- new_data[rep(1, n), ]
keepers <- c(names(object$columns), names(object$profile))
# Keep the predictors in the same order
keepers <- names(new_data)[names(new_data) %in% keepers]
new_data <- dplyr::select(new_data,! !keepers)
for (i in names(object$columns)) {
new_data[[i]] <- rep(object$columns[[i]], n)
}
new_data[[names(object$profile)]] <- object$profile[[1]]
as_tibble(new_data)
}
print.step_profile <-
function(x, width = max(20, options()$width - 22), ...) {
cat("Profiling data set for ")
printer(names(x$profile), x$profile, x$trained, width = width)
invisible(x)
}
#' @rdname step_profile
#' @param x A `step_profile` object.
#' @export
tidy.step_profile <- function(x, ...) {
if (is_trained(x)) {
res <- tibble(terms = x$columns)
fixed_names <- names(x$columns)
prof_names <- names(x$profile)
}
else {
fixed_names <- sel2char(x$terms)
prof_names <- sel2char(x$profile)
}
fixed_res <- tibble(terms = fixed_names,
type = rep("fixed", length = length(fixed_names)))
prof_res <- tibble(terms = prof_names,
type = rep("profiled", length = length(prof_names)))
res <- bind_rows(fixed_res, prof_res)
res$id <- x$id
res
}
# some classes for the fixed values
#' Helper Functions for Profile Data Sets
#'
#' @param x A vector
#' @param pct,index,...,grid Options pass from [step_profile()]
#' @export
#' @keywords internal
fixed <- function (x, pct, index, ...) UseMethod("fixed")
#' @export
#' @rdname fixed
fixed.default <- function(x, pct, index, ...) {
rlang::abort("No method for determining a value to fix for ",
"objects of class(s) ",
paste0("'", class(x), "'", collapse = ","),
call. = FALSE)
}
#' @export
#' @rdname fixed
fixed.numeric <- function(x, pct, index, ...) {
unname(quantile(x, probs = pct, na.rm = TRUE))
}
#' @export
#' @rdname fixed
fixed.factor <- function(x, pct, index, ...) {
lev <- levels(x)[min(index, length(levels(x)))]
factor(lev, levels = levels(x), ordered = is.ordered(x))
}
#' @export
#' @rdname fixed
fixed.character <- function(x, pct, index, ...) {
x <- sort(unique(x))
x[min(index, length(x))]
}
#' @export
#' @rdname fixed
fixed.Date <- function(x, pct, index, ...) {
vals <- c(0, .5, 1)
dst <- (vals - pct) ^ 2
mthd <- which.min(dst)
if (mthd == 1) {
out <- min(x, na.rm = TRUE)
} else {
if (mthd == 2) {
out <- median(x, na.rm = TRUE)
} else {
out <- max(x, na.rm = TRUE)
}
}
out
}
#' @export
#' @rdname fixed
fixed.POSIXct <- fixed.Date
#' @export
#' @rdname fixed
prof <- function (x, grid, ...) UseMethod("prof")
#' @export
#' @rdname fixed
prof.numeric <- function(x, grid, ...) {
if(grid$pctl) {
pct <- seq(0, 1, length = grid$len)
out <- unname(quantile(x, probs = pct, na.rm = TRUE))
} else {
out <- seq(
min(x, na.rm = TRUE),
max(x, na.rm = TRUE),
length = grid$len
)
}
unique(out)
}
#' @export
#' @rdname fixed
prof.factor <- function(x, grid, ...) {
levels(x)
}
#' @export
#' @rdname fixed
prof.character <- function(x, grid, ...) {
sort(unique(x))
}
#' @export
#' @rdname fixed
prof.Date <- function(x, grid, ...) {
out <-
seq(min(x, na.rm = TRUE), max(x, na.rm = TRUE), length.out = grid$len)
unique(out)
}
#' @export
#' @rdname fixed
prof.POSIXct <- function(x, grid, ...) {
x <- as.Date(x)
prof.Date(x, grid, ...)
}
|