Feature selection is an important step for practical commercial data mining which is often characterised by data sets with far too many variables for model building. In a previous post we looked at all-relevant feature selection using the Boruta package while in this post we consider the same (artificial, toy) examples using the caret package. Max Kuhn kindly listed me as a contributor for some performance enhancements I submitted, but the genius behind the package is all his.
The caret package provides a very flexible framework for the analysis as we shall see, but first we set up the artificial test data set as in the previous article.
The flexibility of the caret package is to a large extent implemented by using control objects. Here we specify to use the randomForest classification algorithm (which is also what Boruta uses) and if the multicore package is available then we use that for extra perfomance (you can also use MPI etc – see the documentation):
And of course, if you have your own favourite model class that is not already implemented, then you can easily do that yourself. We like gbm from the package of the same name, which is kind of silly to use here because it provides variable importance automatically as part of the fitting process, but may still be useful. It needs numeric predictors so we do:
Code
## Use gbm for predictiony.1<-as.numeric(y.1)-1y.2<-as.numeric(y.2)-1y.3<-as.numeric(y.3)-1y.4<-as.numeric(y.4)-1gbmFuncs<-treebagFuncsgbmFuncs$fit<-function(x, y, first, last, ...){library("gbm")n.levels<-length(unique(y))if(n.levels==2){distribution<-"bernoulli"}else{distribution<-"gaussian"}gbm.fit(x, y, distribution =distribution, ...)}gbmFuncs$pred<-function(object, x){n.trees<-suppressWarnings(gbm.perf(object, plot.it =FALSE, method ="OOB"))if(n.trees<=0)n.trees<-object$n.treespredict(object, x, n.trees =n.trees, type ="link")}control$functions<-gbmFuncsn.trees<-1e2# Default value for gbm is 100profile.1<-rfe(x, y.1, sizes =sizes, rfeControl =control, verbose =FALSE, n.trees =n.trees)cat("gbm : Profile 1 predictors:", predictors(profile.1), fill =TRUE)profile.2<-rfe(x, y.2, sizes =sizes, rfeControl =control, verbose =FALSE, n.trees =n.trees)cat("gbm : Profile 2 predictors:", predictors(profile.2), fill =TRUE)profile.3<-rfe(x, y.3, sizes =sizes, rfeControl =control, verbose =FALSE, n.trees =n.trees)cat("gbm : Profile 3 predictors:", predictors(profile.3), fill =TRUE)profile.4<-rfe(x, y.4, sizes =sizes, rfeControl =control, verbose =FALSE, n.trees =n.trees)cat("gbm : Profile 4 predictors:", predictors(profile.4), fill =TRUE)