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
|
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/other.R
\name{step_other}
\alias{step_other}
\alias{tidy.step_other}
\title{Collapse Some Categorical Levels}
\usage{
step_other(
recipe,
...,
role = NA,
trained = FALSE,
threshold = 0.05,
other = "other",
objects = NULL,
skip = FALSE,
id = rand_id("other")
)
\method{tidy}{step_other}(x, ...)
}
\arguments{
\item{recipe}{A recipe object. The step will be added to the
sequence of operations for this recipe.}
\item{...}{One or more selector functions to choose which
variables that will potentially be reduced. See
\code{\link[=selections]{selections()}} for more details. For the \code{tidy}
method, these are not currently used.}
\item{role}{Not used by this step since no new variables are
created.}
\item{trained}{A logical to indicate if the quantities for
preprocessing have been estimated.}
\item{threshold}{A numeric value between 0 and 1 or an integer greater or
equal to one. If it's less than one then factor levels whose rate of
occurrence in the training set are below \code{threshold} will be "othered". If
it's greater or equal to one then it's treated as a frequency and factor
levels that occur less then \code{threshold} times will be "othered".}
\item{other}{A single character value for the "other" category.}
\item{objects}{A list of objects that contain the information
to pool infrequent levels that is determined by
\code{\link[=prep.recipe]{prep.recipe()}}.}
\item{skip}{A logical. Should the step be skipped when the
recipe is baked by \code{\link[=bake.recipe]{bake.recipe()}}? While all operations are baked
when \code{\link[=prep.recipe]{prep.recipe()}} is run, some operations may not be able to be
conducted on new data (e.g. processing the outcome variable(s)).
Care should be taken when using \code{skip = TRUE} as it may affect
the computations for subsequent operations}
\item{id}{A character string that is unique to this step to identify it.}
\item{x}{A \code{step_other} object.}
}
\value{
An updated version of \code{recipe} with the new step
added to the sequence of existing steps (if any). For the
\code{tidy} method, a tibble with columns \code{terms} (the
columns that will be affected) and \code{retained} (the factor
levels that were not pulled into "other")
}
\description{
\code{step_other} creates a \emph{specification} of a recipe
step that will potentially pool infrequently occurring values
into an "other" category.
}
\details{
The overall proportion (or total counts) of the categories are
computed. The "other" category is used in place of any categorical levels
whose individual proportion (or frequency) in the training set is less than
\code{threshold}.
If no pooling is done the data are unmodified (although character data may
be changed to factors based on the value of \code{strings_as_factors} in
\code{\link[=prep.recipe]{prep.recipe()}}). Otherwise, a factor is always returned with
different factor levels.
If \code{threshold} is less than the largest category proportion, all levels
except for the most frequent are collapsed to the \code{other} level.
If the retained categories include the value of \code{other}, an error is
thrown. If \code{other} is in the list of discarded levels, no error
occurs.
If no pooling is done, novel factor levels are converted to missing. If
pooling is needed, they will be placed into the other category.
When data to be processed contains novel levels (i.e., not
contained in the training set), the other category is assigned.
}
\examples{
library(modeldata)
data(okc)
set.seed(19)
in_train <- sample(1:nrow(okc), size = 30000)
okc_tr <- okc[ in_train,]
okc_te <- okc[-in_train,]
rec <- recipe(~ diet + location, data = okc_tr)
rec <- rec \%>\%
step_other(diet, location, threshold = .1, other = "other values")
rec <- prep(rec, training = okc_tr)
collapsed <- bake(rec, okc_te)
table(okc_te$diet, collapsed$diet, useNA = "always")
tidy(rec, number = 1)
# novel levels are also "othered"
tahiti <- okc[1,]
tahiti$location <- "a magical place"
bake(rec, tahiti)
# threshold as a frequency
rec <- recipe(~ diet + location, data = okc_tr)
rec <- rec \%>\%
step_other(diet, location, threshold = 2000, other = "other values")
rec <- prep(rec, training = okc_tr)
tidy(rec, number = 1)
# compare it to
# okc_tr \%>\% count(diet, sort = TRUE) \%>\% top_n(4)
# okc_tr \%>\% count(location, sort = TRUE) \%>\% top_n(3)
}
\seealso{
\code{\link[=step_factor2string]{step_factor2string()}}, \code{\link[=step_string2factor]{step_string2factor()}},
\code{\link[=dummy_names]{dummy_names()}}, \code{\link[=step_regex]{step_regex()}}, \code{\link[=step_count]{step_count()}},
\code{\link[=step_ordinalscore]{step_ordinalscore()}}, \code{\link[=step_unorder]{step_unorder()}}, \code{\link[=step_novel]{step_novel()}}
}
\concept{factors}
\concept{preprocessing}
\keyword{datagen}
|