% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/rose.R
\name{step_rose}
\alias{step_rose}
\alias{tidy.step_rose}
\title{Apply ROSE Algorithm}
\usage{
step_rose(
  recipe,
  ...,
  role = NA,
  trained = FALSE,
  column = NULL,
  over_ratio = 1,
  minority_prop = 0.5,
  minority_smoothness = 1,
  majority_smoothness = 1,
  indicator_column = NULL,
  skip = TRUE,
  seed = sample.int(10^5, 1),
  id = rand_id("rose")
)
}
\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
variable is used to sample the data. See \link[recipes:selections]{recipes::selections}
for more details. The selection should result in \emph{single
factor variable}. 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{column}{A character string of the variable name that will
be populated (eventually) by the \code{...} selectors.}

\item{over_ratio}{A numeric value for the total size of the synthetic data
relative to twice the size of the majority class. Unlike the other
over-sampling steps this is not a per-class target, so a named vector of
ratios is not accepted here.}

\item{minority_prop}{A numeric value between 0 and 1 for the proportion of
synthetic observations from the minority class. Defaults to 0.5, which
generates an equal split of minority and majority synthetic observations.
This parameter controls the class balance \emph{within} the synthetic data,
while \code{over_ratio} controls the \emph{total size} of the synthetic data.}

\item{minority_smoothness}{A numeric. Shrink factor to be multiplied by the
smoothing parameters to estimate the conditional kernel density of the
minority class. Defaults to 1.}

\item{majority_smoothness}{A numeric. Shrink factor to be multiplied by the
smoothing parameters to estimate the conditional kernel density of the
majority class. Defaults to 1.}

\item{indicator_column}{A single string or \code{NULL} (the default). If a
string is given, a logical column with that name is added to the output.
Because ROSE generates a fully synthetic dataset, all rows are marked
\code{TRUE}.}

\item{skip}{A logical. Should the step be skipped when the recipe is baked by
\code{\link[recipes:bake]{bake()}}? While all operations are baked when \code{\link[recipes:prep]{prep()}} 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{seed}{An integer that will be used as the seed when applied.}

\item{id}{A character string that is unique to this step to identify it.}
}
\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} which is
the variable used to sample.
}
\description{
\code{step_rose()} creates a \emph{specification} of a recipe step that generates
samples of synthetic data by enlarging the feature space of minority and
majority class examples. Using \code{\link[ROSE:ROSE]{ROSE::ROSE()}}.
}
\details{
The factor variable used to balance around must only have 2 levels.

The ROSE algorithm works by selecting an observation belonging to class k
and generating new examples in its neighborhood, which is determined by a
smoothing matrix H_k. Smaller values of \code{minority_smoothness} and
\code{majority_smoothness} shrink the entries of H_k, producing tighter
neighborhoods. This is a cautious choice when there is a concern that
excessively large neighborhoods could blur the boundaries between classes.

All columns in the data are sampled and returned by \code{\link[recipes:juice]{recipes::juice()}}
and \code{\link[recipes:bake]{recipes::bake()}}.

When used in modeling, users should strongly consider using the
option \code{skip = TRUE} so that the extra sampling is \emph{not}
conducted outside of the training set.
}
\section{Tidying}{
When you \code{\link[recipes:tidy.recipe]{tidy()}} this step, a tibble is returned with
columns \code{terms} and \code{id}:

\describe{
\item{terms}{character, the selectors or variables selected}
\item{id}{character, id of this step}
}
}

\section{Tuning Parameters}{
This step has 1 tuning parameters:
\itemize{
\item \code{over_ratio}: Over-Sampling Ratio (type: double, default: 1)
}
}

\section{Case weights}{


The underlying operation does not allow for case weights. Supplying data
with a case weights column to this step results in an error.
}

\examples{
\dontshow{if (rlang::is_installed("modeldata")) withAutoprint(\{ # examplesIf}
library(recipes)
library(modeldata)
data(hpc_data)

hpc_data0 <- hpc_data |>
  mutate(class = factor(class == "VF", labels = c("not VF", "VF"))) |>
  select(-protocol, -day)

orig <- count(hpc_data0, class, name = "orig")
orig

up_rec <- recipe(class ~ ., data = hpc_data0) |>
  step_rose(class) |>
  prep()

training <- up_rec |>
  bake(new_data = NULL) |>
  count(class, name = "training")
training

# Since `skip` defaults to TRUE, baking the step has no effect
baked <- up_rec |>
  bake(new_data = hpc_data0) |>
  count(class, name = "baked")
baked

orig |>
  left_join(training, by = "class") |>
  left_join(baked, by = "class")

library(ggplot2)

ggplot(circle_example, aes(x, y, color = class)) +
  geom_point() +
  labs(title = "Without ROSE")

recipe(class ~ x + y, data = circle_example) |>
  step_rose(class) |>
  prep() |>
  bake(new_data = NULL) |>
  ggplot(aes(x, y, color = class)) +
  geom_point() +
  labs(title = "With ROSE")
\dontshow{\}) # examplesIf}
}
\references{
Lunardon, N., Menardi, G., and Torelli, N. (2014). ROSE: a
Package for Binary Imbalanced Learning. R Journal, 6:79–89.

Menardi, G. and Torelli, N. (2014). Training and assessing
classification rules with imbalanced data. Data Mining and Knowledge
Discovery, 28:92–122.
}
\seealso{
\code{\link[=rose]{rose()}} for direct implementation

Other Steps for over-sampling:
\code{\link[=step_adasyn]{step_adasyn()}},
\code{\link[=step_bsmote]{step_bsmote()}},
\code{\link[=step_kmeans_smote]{step_kmeans_smote()}},
\code{\link[=step_smogn]{step_smogn()}},
\code{\link[=step_smote]{step_smote()}},
\code{\link[=step_smoten]{step_smoten()}},
\code{\link[=step_smotenc]{step_smotenc()}},
\code{\link[=step_svmsmote]{step_svmsmote()}},
\code{\link[=step_upsample]{step_upsample()}}
}
\concept{Steps for over-sampling}
