I have a data set that is imbalanced and would like to weight the samples to compensate, however I can't find code to implement this in R though I believe there is a feature in the randomForest package to do this.

Here's a sample dataset :

id      buy=1/noBuy=0    timeOnSite(sec.)     clicksOnSite      estAge
1             0              150                   12             44
2             0              342                   56             23
3             0               33                   11             18
4             1              167                   34             27
5             0               95                    3             52
6             0              254                   98             26
7             0              982                    4             36
8             0               72                    6             22
9             1              259                   62             48
10            0              438                  104             24

I'm trying to predict the buy/noBuy column but the real data is even more imbalanced than this. Anyone know of an example written in R to deal with a similar situation?


1 Answer 1


Ok, so I found part of my answer but not the good part. It turns out the randomForest package can do stratified sampling but only for classification. Here is a link to the package author's explanation.

I'm still looking for ideas on how to do stratified sampling for regression rf's.

  • 1
    $\begingroup$ This question confuses me - are you looking for a general way to do stratified sampling for regression forests (hard, because there's no obvious classes to weight), or is it for this problem in particular? This problem looks very much like a straight-up binary classification problem, in which case all you need to do is convert your buy/noBuy column to a factor. $\endgroup$ Mar 20, 2012 at 23:27
  • $\begingroup$ @MartinO'Leary: I'm not sure how well the question is posed but I was looking ideally for a way to do stratified sampling for both regression and classification. I just used a simple classification problem to write the question. I do some work with securities prices where a large portion of the securities move in a small range and a small percentage have large ranges and was curious whether strat.samp. might help. $\endgroup$
    – screechOwl
    Mar 21, 2012 at 1:20
  • 1
    $\begingroup$ reading your link I find, in contrary, it stated very clearly how to do stratified sampling for RF regression. Use or make a stratification vector and feed it to "strata" choose sample size of each category with sampsize: randomForest(..., strata=center, sampsize=rep(min(table(center))), nlevels(center))) $\endgroup$ Aug 10, 2015 at 14:18

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.