# Feature selection and parameter tuning with caret for random forest

I have data with a few thousand features and I want to do recursive feature selection (RFE) to remove uninformative ones. I do this with caret and RFE. However, I started thinking, if I want to get the best regression fit (random forest, for example), when should I perform parameter tuning (mtry for RF)? That is, as I understand caret trains RF repeatedly on different feature subsets with a fixed mtry. I suppose the optimum mtry should be found after the feature selection is finished, but will the mtry value that caret uses influence the selected subset of features? Using caret with low mtry is much faster, of course.

Hope someone can explain this to me.

• RF has a robust built-in feature selection - no need to use RFE so one can just tune mtry and be done with it. – Yevgeny Sep 5 '12 at 21:42

One thing you might want to look into are regularized random forests, which are specifically designed for feature selection. This paper explains the concept, and how they differ from normal random forests

Feature Selection via Regularized Trees

There's also a CRAN package RRF that's build on the randomForest that will allow you to implement them easily in R. I've had good luck with this methodology myself.

Regarding your initial question, the only advice I can give is that if you have a lot of collinearity then you need to use smaller tree sizes. This allows the algorithm to determine importance with less interference from collinearity effects.

You might be able to use caretFuncs Something like this:

myRFE <- caretFuncs
myRFE\$summary <- twoClassSummary  (default is defaultSummary)

rctrl <- rfeControl(method='repeatedcv', repeats=5, number=10,
functions=myRFE)

tctrl <- trainControl(method = "cv",
classProbs = TRUE,
summaryFunction = twoClassSummary)

rfeObj = rfe(x,y,sizes=seq(1,ncol(x),2),
rfeControl=rctrl,
# to be passed to train()
method='rf',
importance=T,  # do not forget this
ntree=1000,
metric = "ROC",
tuneLength = 10,
# mtry=c(1,3,5,50),
# specify the exact mtry, or tuneLength
# can auto truncate the grid to minimal sizes (with or without warning)
# p <- ncol(x) ... if (mtry < 1 || mtry > p) warning("invalid try: reset to within valid range") try <- max(1, min(p, round(try)))
trControl=tctrl)


Also, one may check valSelRF package. Not sure how it differs from regularized random forest mentioned here.