% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/enn_impl.R
\name{enn}
\alias{enn}
\title{Edited Nearest Neighbors}
\usage{
enn(
  df,
  var,
  neighbors = 3,
  distance = "euclidean",
  times = 1,
  all_k = FALSE,
  kind_sel = "mode"
)
}
\arguments{
\item{df}{data.frame or tibble. Must have 1 factor variable and remaining
numeric variables.}

\item{var}{Character, name of variable containing factor variable.}

\item{neighbors}{An integer. Number of nearest neighbor that are used to
decide whether an observation is removed. Defaults to \code{3}, unlike the
over-sampling steps which default to \code{5}.}

\item{distance}{A character string specifying the distance metric used for
nearest neighbor calculations, defaulting to \code{"euclidean"}. The available
metrics fall into three groups.

\code{"euclidean"}, \code{"cosine"}, and \code{"mahalanobis"} use approximate nearest
neighbors via the RANN package and scale well to large datasets.

\code{"squared_chord"}, \code{"matusita"}, \code{"hellinger"}, and \code{"bhattacharyya"} are
probability-divergence measures that treat each row as a distribution over
the predictors, so they require non-negative values. \code{"hellinger"} and
\code{"bhattacharyya"} further require each row to sum to 1. All four also use
the RANN package and scale well to large datasets.

\code{"manhattan"}, \code{"chebyshev"}, \code{"canberra"}, \code{"soergel"}, \code{"lorentzian"},
\code{"jeffreys"}, \code{"topsoe"}, \code{"jensen-shannon"}, \code{"jensen_difference"},
\code{"taneja"}, and \code{"kumar-johnson"} compute an exact all-pairs distance
matrix. This takes time and memory proportional to the square of the number
of observations in a class, so these are best suited to smaller datasets.
Everything from \code{"canberra"} onwards is a probability divergence requiring
non-negative values, is provided by the philentropy package (which must be
installed separately), and in the case of \code{"jeffreys"}, \code{"taneja"}, and
\code{"kumar-johnson"} requires strictly positive values, since those divide by
individual predictor values.

The probability divergences are meaningful for compositional predictors such
as proportions or counts normalized per observation, and are generally not
appropriate for standardized predictors.}

\item{times}{A positive integer for the maximum number of times ENN is
applied. Defaults to \code{1} for a single pass. Values greater than \code{1} repeat
the cleaning, stopping early once a pass removes no observations. Use
\code{Inf} to repeat until convergence (Repeated Edited Nearest Neighbors).}

\item{all_k}{A logical. When \code{TRUE}, ENN is applied with an increasing number
of neighbors, from \code{1} up to \code{neighbors}, cleaning the data at each step
(All k-Nearest Neighbors). Takes precedence over \code{times}. Defaults to
\code{FALSE}.}

\item{kind_sel}{A character string. The rule used to decide whether an
observation is removed. \code{"mode"} (the default) removes an observation when
the majority of its neighbors disagree with its class. \code{"all"} is stricter
and removes an observation unless all of its neighbors share its class.}
}
\value{
A data.frame or tibble, depending on type of \code{df}.
}
\description{
Removes observations whose class differs from the majority of their nearest
neighbors.
}
\details{
Edited Nearest Neighbors (ENN) is a cleaning method. For each observation it
finds the \code{neighbors} nearest neighbors and, if the class of the observation
does not match the majority class among those neighbors, the observation is
removed. This tends to remove noisy and borderline observations, which can
lead to smoother decision boundaries.

Setting \code{times} greater than 1 applies ENN repeatedly, removing more noisy
and borderline observations on each pass and stopping early once a pass
removes nothing. This corresponds to Repeated Edited Nearest Neighbors
(RENN).

Setting \code{all_k = TRUE} applies ENN with increasing numbers of neighbors, from
\code{1} up to \code{neighbors}, cleaning the data at each step. This corresponds to
All k-Nearest Neighbors (AllKNN) and takes precedence over \code{times}.

Setting \code{kind_sel = "all"} uses a stricter cleaning rule: instead of removing
an observation when the majority of its neighbors disagree, it is removed
unless every one of its neighbors shares its class. This removes more
observations than the default \code{kind_sel = "mode"}.

All columns used in this function must be numeric with no missing data.

Use \code{times = Inf} to repeat ENN until convergence.
}
\examples{
circle_numeric <- circle_example[, c("x", "y", "class")]

res <- enn(circle_numeric, var = "class")

res <- enn(circle_numeric, var = "class", neighbors = 5)

res <- enn(circle_numeric, var = "class", distance = "manhattan")

# Repeated Edited Nearest Neighbors (RENN)
res <- enn(circle_numeric, var = "class", times = Inf)

# All k-Nearest Neighbors (AllKNN)
res <- enn(circle_numeric, var = "class", all_k = TRUE)

# Stricter cleaning: remove unless all neighbors agree
res <- enn(circle_numeric, var = "class", kind_sel = "all")
}
\references{
Wilson, D. L. (1972). Asymptotic properties of nearest neighbor
rules using edited data. IEEE Transactions on Systems, Man, and Cybernetics,
(3), 408-421.

Tomek, I. (1976). An experiment with the edited nearest-neighbor rule. IEEE
Transactions on Systems, Man, and Cybernetics, (6), 448-452.
}
\seealso{
\code{\link[=step_enn]{step_enn()}} for step function of this method

Other Direct Implementations:
\code{\link[=adasyn]{adasyn()}},
\code{\link[=bsmote]{bsmote()}},
\code{\link[=cluster_centroids]{cluster_centroids()}},
\code{\link[=cnn]{cnn()}},
\code{\link[=instance_hardness]{instance_hardness()}},
\code{\link[=kmeans_smote]{kmeans_smote()}},
\code{\link[=ncl]{ncl()}},
\code{\link[=nearmiss]{nearmiss()}},
\code{\link[=oss]{oss()}},
\code{\link[=rose]{rose()}},
\code{\link[=smogn]{smogn()}},
\code{\link[=smote]{smote()}},
\code{\link[=smoten]{smoten()}},
\code{\link[=smotenc]{smotenc()}},
\code{\link[=svmsmote]{svmsmote()}},
\code{\link[=tomek]{tomek()}}
}
\concept{Direct Implementations}
