Take the 2-minute tour ×

I am using random forests in a big data problem, which has a very unbalanced response class, so I read the documentation and I found the following parameters:



The documentation for these parameters is sparse (or I didn´t have the luck to find it) and I really don´t understand how to implement it. I am using the following code:


The response is a class with two possible values, the first one appears more frequently than the second (10000:1 or more)

The list.params is a list with different parameters (duh! I know...)

Well, the question (again) is: How I can use the 'strata' parameter? I am using sampsize correctly?

And finally, sometimes I get the following error:

Error in randomForest.default(x = predictors, y = response, data = train.data,  :
  Still have fewer than two classes in the in-bag sample after 10 attempts.

Sorry If I am doing so many (and maybe stupid) questions ...

share|improve this question
Following a suggestion of DWin, I will try to make a description of the data. The data frame has a size of 1 Mrows or maybe a little more (2 or 3 Mrows), It has nearly 33 columns, which are factors except by two which are of type numeric, and one extra column which are the response, a factor with two possible values. I hope this helps. –  nanounanue Jan 2 '12 at 20:32
After puzzling over your description of the error message you were getting, I scanned the source code on CRAN, and ended up contacting the package author. Turns out there was in fact a small bug in the code that throws that error that may have been giving you problems. Try checking CRAN for an patched version over the next several days and see if that helps. –  joran Jan 4 '12 at 17:21
@joran Wow, who would know! Let me try it again with the original code. Thanks! –  nanounanue Jan 19 '12 at 19:31
Sorry for taking so long in answer, but It works now. It was a bug as @joran saids... Thanks again –  nanounanue Mar 17 '12 at 22:47
add comment

3 Answers

up vote 4 down vote accepted

You should try using sampling methods that reduce the degree of imbalance from 1:10,000 down to 1:100 or 1:10. You should also reduce the size of the trees that are generated. (At the moment these are recommendations that I am repeating only from memory, but I will see if I can track down more authority than my spongy cortex.)

One way of reducing the size of trees is to set the "nodesize" larger. With that degree of imbalance you might need to have the node size really large, say 5-10,000. Here's a thread in rhelp: https://stat.ethz.ch/pipermail/r-help/2011-September/289288.html

In the current state of the question you have sampsize=c(250000,2000), whereas I would have thought that something like sampsize=c(8000,2000), was more in line with my suggestions. I think you are creating samples where you do not have any of the group that was sampled with only 2000.

share|improve this answer
Thank you DWin, could you provide me with some example of code? Thanks in advance (again) –  nanounanue Jan 2 '12 at 20:21
I generally make an attempt at code only when given either data or a sufficiently clear description that supports constructing an example that matches the problem. –  IShouldBuyABoat Jan 2 '12 at 20:24
Ok, that´s sound fair ... :) –  nanounanue Jan 2 '12 at 20:25
Respectly to your answer, If I increase the size of node size, it will not impact in the predictions? I mean, It will decrease the accuracy? In the other hand, if I decrease the size of the tree how it can improve the accuracy? –  nanounanue Jan 2 '12 at 20:27
I suspect if may increase the accuracy of your predictions in this situation. The analogy is with logistic regression where the smaller of the outcome groups should generally be at least 10 times the number of predictors. –  IShouldBuyABoat Jan 2 '12 at 20:31
show 3 more comments

Sorry, I don't know how to post a comment on the earlier answer, so I'll create a separate answer.

I suppose that the problem is caused by high imbalance of dataset (too few cases of one of the classes are present). For each tree in RF the algorithm creates bootstrap sample, which is a training set for this tree. And if you have too few examples of one of the classes in your dataset, then the bootstrap sampling will select examples of only one class (major class). And thus tree cannot be grown on only one class examples. It seems that there is a limit on 10 unsuccessful sampling attempts. So the proposition of DWin to reduce the degree of imbalance to lower values (1:100 or 1:10) is the most reasonable one.

share|improve this answer
Thank you DrDom (one letter more and would be a great nickname, by the way, I am kinda fan of Dr. Doom) But where to do the reduction of imbalance? At the time of getting the data from the database or in the sampsize attribute? I tried the second one (I mean leaving 1M row or something) and balancing the sampsize and the error appears again, but if I reduce from 1Mrows to lets said 250K everything works, but I am worried about the hit on predictability or maybe over fitting... What is your opinion? –  nanounanue Jan 3 '12 at 16:24
@nanounanue, I can suggest to take all minor class objects and add to them 100 times more objects from the major class (selection of major class object can be done randomly or supervised), and develop a model. Then repeat this step so many times to use all objects from major class at least one time. Thus you will have a pack of models. And all of them should be used for prediction. The final prediction you make by majority vote scheme-which class has more votes it wins. Another possibility is reduce the number of major class objects. This can be done according to cluster analysis for example. –  DrDom Jan 3 '12 at 16:38
All your suggestions where right! Thank you so much! –  nanounanue Mar 17 '12 at 22:47
add comment

There are a few options.

If you have a lot of data, set aside a random sample of the data. Build your model on one set, then use the other to determine a proper cutoff for the class probabilities using an ROC curve.

You can also upsample the data in the minority class. The SMOTE algorithm might help (see the reference below and the DMwR package for a function).

You can also use other techniques. rpart() and a few other functions can allow different costs on the errors, so you could favor the minority class more. You can bag this type of rpart() model to approximate what random forest is doing.

ksvm() in the kernlab package can also use unbalanced costs (but the probability estimates are no longer good when you do this). Many other packages have arguments for setting the priors. You can also adjust this to put more emphasis on the minority class.

One last thought: maximizing models based on accuracy isn't going to get you anywhere (you can get 99.99% off the bat). The caret can tune models based on the Kappa statistic, which is a much better choice in your case.

share|improve this answer
You are right, I think that I use the word "accuracy" too freely.. –  nanounanue Jan 3 '12 at 21:06
add comment

Your Answer


