rfe  R Documentation 
A simple backwards selection, a.k.a. recursive feature elimination (RFE), algorithm
rfe(x, ...) ## Default S3 method: rfe( x, y, sizes = 2^(2:4), metric = ifelse(is.factor(y), "Accuracy", "RMSE"), maximize = ifelse(metric %in% c("RMSE", "MAE", "logLoss"), FALSE, TRUE), rfeControl = rfeControl(), ... ) ## S3 method for class 'formula' rfe(form, data, ..., subset, na.action, contrasts = NULL) rfeIter( x, y, testX, testY, sizes, rfeControl = rfeControl(), label = "", seeds = NA, ... ) ## S3 method for class 'rfe' update(object, x, y, size, ...) ## S3 method for class 'recipe' rfe( x, data, sizes = 2^(2:4), metric = NULL, maximize = NULL, rfeControl = rfeControl(), ... )
x 
A matrix or data frame of predictors for model training. This
object must have unique column names. For the recipes method, 
... 
options to pass to the model fitting function (ignored in

y 
a vector of training set outcomes (either numeric or factor) 
sizes 
a numeric vector of integers corresponding to the number of features that should be retained 
metric 
a string that specifies what summary metric will be used to
select the optimal model. By default, possible values are "RMSE" and
"Rsquared" for regression and "Accuracy" and "Kappa" for classification. If
custom performance metrics are used (via the 
maximize 
a logical: should the metric be maximized or minimized? 
rfeControl 
a list of options, including functions for fitting and prediction. The web page http://topepo.github.io/caret/recursivefeatureelimination.html#rfe has more details and examples related to this function. 
form 
A formula of the form 
data 
Data frame from which variables specified in

subset 
An index vector specifying the cases to be used in the training sample. (NOTE: If given, this argument must be named.) 
na.action 
A function to specify the action to be taken
if NAs are found. The default action is for the procedure to
fail. An alternative is 
contrasts 
A list of contrasts to be used for some or all the factors appearing as variables in the model formula. 
testX 
a matrix or data frame of test set predictors. This must have
the same column names as 
testY 
a vector of test set outcomes 
label 
an optional character string to be printed when in verbose mode. 
seeds 
an optional vector of integers for the size. The vector should
have length of 
object 
an object of class 
size 
a single integers corresponding to the number of features that should be retained in the updated model 
More details on this function can be found at http://topepo.github.io/caret/recursivefeatureelimination.html.
This function implements backwards selection of predictors based on predictor importance ranking. The predictors are ranked and the less important ones are sequentially eliminated prior to modeling. The goal is to find a subset of predictors that can be used to produce an accurate model. The web page http://topepo.github.io/caret/recursivefeatureelimination.html#rfe has more details and examples related to this function.
rfe
can be used with "explicit parallelism", where different
resamples (e.g. crossvalidation group) can be split up and run on multiple
machines or processors. By default, rfe
will use a single processor
on the host machine. As of version 4.99 of this package, the framework used
for parallel processing uses the foreach package. To run the resamples
in parallel, the code for rfe
does not change; prior to the call to
rfe
, a parallel backend is registered with foreach (see the
examples below).
rfeIter
is the basic algorithm while rfe
wraps these
operations inside of resampling. To avoid selection bias, it is better to
use the function rfe
than rfeIter
.
When updating a model, if the entire set of resamples were not saved using
rfeControl(returnResamp = "final")
, the existing resamples are
removed with a warning.
A list with elements
finalVariables 
a list of size

pred 
a data frame with columns for the test set outcome, the predicted outcome and the subset size. 
We using a recipe as an input, there may be some subset sizes that are not wellreplicated over resamples. 'rfe' method will only consider subset sizes where at least half of the resamples have associated results in the search for an optimal subset size.
Max Kuhn
rfeControl
## Not run: data(BloodBrain) x < scale(bbbDescr[,nearZeroVar(bbbDescr)]) x < x[, findCorrelation(cor(x), .8)] x < as.data.frame(x, stringsAsFactors = TRUE) set.seed(1) lmProfile < rfe(x, logBBB, sizes = c(2:25, 30, 35, 40, 45, 50, 55, 60, 65), rfeControl = rfeControl(functions = lmFuncs, number = 200)) set.seed(1) lmProfile2 < rfe(x, logBBB, sizes = c(2:25, 30, 35, 40, 45, 50, 55, 60, 65), rfeControl = rfeControl(functions = lmFuncs, rerank = TRUE, number = 200)) xyplot(lmProfile$results$RMSE + lmProfile2$results$RMSE ~ lmProfile$results$Variables, type = c("g", "p", "l"), auto.key = TRUE) rfProfile < rfe(x, logBBB, sizes = c(2, 5, 10, 20), rfeControl = rfeControl(functions = rfFuncs)) bagProfile < rfe(x, logBBB, sizes = c(2, 5, 10, 20), rfeControl = rfeControl(functions = treebagFuncs)) set.seed(1) svmProfile < rfe(x, logBBB, sizes = c(2, 5, 10, 20), rfeControl = rfeControl(functions = caretFuncs, number = 200), ## pass options to train() method = "svmRadial") ## classification data(mdrr) mdrrDescr < mdrrDescr[,nearZeroVar(mdrrDescr)] mdrrDescr < mdrrDescr[, findCorrelation(cor(mdrrDescr), .8)] set.seed(1) inTrain < createDataPartition(mdrrClass, p = .75, list = FALSE)[,1] train < mdrrDescr[ inTrain, ] test < mdrrDescr[inTrain, ] trainClass < mdrrClass[ inTrain] testClass < mdrrClass[inTrain] set.seed(2) ldaProfile < rfe(train, trainClass, sizes = c(1:10, 15, 30), rfeControl = rfeControl(functions = ldaFuncs, method = "cv")) plot(ldaProfile, type = c("o", "g")) postResample(predict(ldaProfile, test), testClass) ## End(Not run) ####################################### ## Parallel Processing Example via multicore ## Not run: library(doMC) ## Note: if the underlying model also uses foreach, the ## number of cores specified above will double (along with ## the memory requirements) registerDoMC(cores = 2) set.seed(1) lmProfile < rfe(x, logBBB, sizes = c(2:25, 30, 35, 40, 45, 50, 55, 60, 65), rfeControl = rfeControl(functions = lmFuncs, number = 200)) ## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.