nearZeroVar: Identification of near zero variance predictors

nearZeroVarR Documentation

Identification of near zero variance predictors


nearZeroVar diagnoses predictors that have one unique value (i.e. are zero variance predictors) or predictors that are have both of the following characteristics: they have very few unique values relative to the number of samples and the ratio of the frequency of the most common value to the frequency of the second most common value is large. checkConditionalX looks at the distribution of the columns of x conditioned on the levels of y and identifies columns of x that are sparse within groups of y.


  freqCut = 95/5,
  uniqueCut = 10,
  saveMetrics = FALSE,
  names = FALSE,
  foreach = FALSE,
  allowParallel = TRUE

checkConditionalX(x, y)

checkResamples(index, x, y)



a numeric vector or matrix, or a data frame with all numeric data


the cutoff for the ratio of the most common value to the second most common value


the cutoff for the percentage of distinct values out of the number of total samples


a logical. If false, the positions of the zero- or near-zero predictors is returned. If true, a data frame with predictor information is returned.


a logical. If false, column indexes are returned. If true, column names are returned.


should the foreach package be used for the computations? If TRUE, less memory should be used.


should the parallel processing via the foreach package be used for the computations? If TRUE, more memory will be used but execution time should be shorter.


a factor vector with at least two levels


a list. Each element corresponds to the training set samples in x for a given resample


For example, an example of near zero variance predictor is one that, for 1000 samples, has two distinct values and 999 of them are a single value.

To be flagged, first the frequency of the most prevalent value over the second most frequent value (called the “frequency ratio”) must be above freqCut. Secondly, the “percent of unique values,” the number of unique values divided by the total number of samples (times 100), must also be below uniqueCut.

In the above example, the frequency ratio is 999 and the unique value percentage is 0.0001.

Checking the conditional distribution of x may be needed for some models, such as naive Bayes where the conditional distributions should have at least one data point within a class.

nzv is the original version of the function.


For nearZeroVar: if saveMetrics = FALSE, a vector of integers corresponding to the column positions of the problematic predictors. If saveMetrics = TRUE, a data frame with columns:


the ratio of frequencies for the most common value over the second most common value


the percentage of unique data points out of the total number of data points


a vector of logicals for whether the predictor has only one distinct value


a vector of logicals for whether the predictor is a near zero variance predictor

For checkResamples or checkConditionalX, a vector of column indicators for predictors with empty conditional distributions in at least one class of y.


Max Kuhn, with speed improvements to nearZeroVar by Allan Engelhardt


nearZeroVar(iris[, -5], saveMetrics = TRUE)

nearZeroVar(bbbDescr, names = TRUE)

classes <- factor(rep(letters[1:3], each = 30))
x <- data.frame(x1 = rep(c(0, 1), 45),
                x2 = c(rep(0, 10), rep(1, 80)))

lapply(x, table, y = classes)
checkConditionalX(x, classes)

folds <- createFolds(classes, k = 3, returnTrain = TRUE)
x$x3 <- x$x1
x$x3[folds[[1]]] <- 0

checkResamples(folds, x, classes)

caret documentation built on March 31, 2023, 9:49 p.m.