Some time ago, I found a paper describing usage of lasso/elastic net regression on binary variables come from random forest. In short, (i,j)-th variable takes 1 if given observation belong to leaf no. i inside tree no. j. Because this will result in a huge number of variables, authors suggested using lasso regression to obtain sparse and interpretable solution.

Does somebody know if such a procedure is implemented in R/Python or can (at least) post a link to this article (or similar). Thanks in advance.


3 Answers 3


First I think it is hard to say one model out "perform" another. Each model has different pros and cons and should be applied to different cases. For example, I would not say random forest outperforms linear regression, because linear regression is 1. more "stable" 2. requires less computational power 3. more interpretable, plus, if you ground truth between feature and value is really linear, no one can beat linear regression.

Now, back to your question, on code to try two approaches.

You can easily to do the experiment with both way and compare the performance. The trick is using model.matrix in R. Here is one example from ISL book to use model.matrix to convert factors to design matrix and use ridge or lasso.

# Chapter 6 Lab 2 of ISL book: Ridge Regression and the Lasso

# transfer formula input to matrix input

train=sample(1:nrow(x), nrow(x)/2)


# The Lasso

# get best fit lamda and fit all data

On the other hand, you can easily do randomForest like


  • 2
    $\begingroup$ How does it relate to my question? $\endgroup$ May 17, 2016 at 16:38
  • $\begingroup$ you were asking if you can do experiment with Python or R, and I listed some code you can try yourself. In addition, I do not believe you can directly compare two types of the model in your way. $\endgroup$
    – Haitao Du
    May 17, 2016 at 16:45
  • $\begingroup$ is this related (not randomforest but svm)? please check When to use LIBLINEAR but not LIBSVM $\endgroup$
    – Haitao Du
    May 17, 2016 at 16:47
  • $\begingroup$ I asked about procedure (or at least article) which enables to obtain binary variables from randomforest and then applies some regularized regression. I have no interest in comparing randomforest and regression, that's why I considered your post completely irrelevant. $\endgroup$ May 17, 2016 at 18:30
  • $\begingroup$ I was trying to suggest to use model.matrix (the procedure) to "obtain binary variables from random forest then applies some regularized regression". Sorry for your confusion. $\endgroup$
    – Haitao Du
    May 17, 2016 at 19:08

This is two years after the original post, but may be of use to others:

R package pre allows for fitting prediction rule ensembles through the algorithm of Friedman & Popescu. It is available from CRAN; development version and an example can be found on GitHub (https://github.com/marjoleinF/pre).

  • 1
    $\begingroup$ Actually, I've already used your package! It's really cool. I was wondering, whether you have a plan to add (or maybe it's already done) some survival regreesion rulefit models (like taking the leaf assignments from randomForestSRC for survival analysis task and apply som regularized Cox regression model)? $\endgroup$ Jun 9, 2018 at 13:59
  • 1
    $\begingroup$ Cool, thanks! Survival regression is not implemented yet, but should not be too hard to implement. Would be an interesting addition. Will take a look at it this week and let you know when implemented (or when it turns out to be more difficult than I thought). $\endgroup$ Jun 10, 2018 at 10:46
  • 2
    $\begingroup$ The current development version on github now supports survival regression. Not extensively tested yet, comments and suggestions are welcome. Here's an example (please excuse lack of line breaks): library("devtools"); install_github("marjoleinF/pre"); library("pre"); data("GBSG2", package = "TH.data"); set.seed(42); surv_pre <- pre(Surv(time, cens) ~ ., data = GBSG2); surv_pre; predict(surv_pre, newdata = GBSG2[1:10,], type = "response"); coef(surv_pre)[1:10,]; imp <- importance(surv_pre); imp $\endgroup$ Jun 12, 2018 at 15:33

This sounds almost like RuleFit described in Friedman & Popescu (2005) in which the lasso is applied to rules formed by an ensemble of trees in addition to linear terms of the variables.

R code is available via the first link.

EDIT: Just noticed that it says on the website that the current RuleFit implementation will not function after Dec 29, 2015 and no updates seem to be available. But give it a try and see if it still works.

  • $\begingroup$ This wasn't article I was talking about, but, anyway, thanks for the link - I 'll definitely try that out. $\endgroup$ May 17, 2016 at 16:41
  • $\begingroup$ Just found this bmcsystbiol.biomedcentral.com/articles/10.1186/… Is that the paper you were talking about? $\endgroup$
    – ErikL
    Jun 4, 2016 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.