This is the exact error:

Error in randomForest.default(m, y, ...) : sampsize has too many elements.

Here is part of my code for classification (not regression) using compiled 3rd party data for prediction:


     0      1 
243697   6303

     0      1 
243566   6434 

Train & test were both created from the same randomly sorted file using different records.

rfm_hc <- randomForest(RESPONSE ~ ., data=train_hc[!names(train_hc)%in%exclude_cols], 
                       nodesize=1, strata=train_hc$response, sampsize=c(6000,6000), 
                       ntree=501, mtry=5, importance=TRUE, type="prob", 
                       keep.forest=TRUE, test=test_hc, cutoff=c(0.7,0.3))

Error in randomForest.default(m, y, ...) : 
  sampsize has too many elements.

From what I found so far it was suggested that the issue is the dependent variable levels but what I included above shows there is only two levels and response is a factor. I also was using 500k each for train & test then changed this to 250k each. This didn't help.

Any ideas?

  • $\begingroup$ which package? Why did you pick it? Which data? What are you doing with it? The folks who say questions are poor don't like "a particular software package" and may send this to stackoverflow. If you make it a more general, more statistics related question they may keep it. $\endgroup$ – EngrStudent - Reinstate Monica Sep 5 '16 at 16:18
  • $\begingroup$ @EngrStudent thanks I added the libraries (randomForest & ROCR) used and the type of data (compiled 3rd party) and the purpose (classification) $\endgroup$ – Walter T Sep 5 '16 at 16:32

From the documentation:

y A response vector. If a factor, classification is assumed, otherwise regression is assumed. If omitted, randomForest will run in unsupervised mode.

sampsize Size(s) of sample to draw. For classification, if sampsize is a vector of the length the number of strata, then sampling is stratified by strata, and the elements of sampsize indicate the numbers to be drawn from the strata.

Check that is.factor(alldata_hc$RESPONSE). If this is FALSE, do alldata_hc$RESPONSE <- as.factor(alldata_hc$RESPONSE).

  • $\begingroup$ Response is a factor with two levels $\endgroup$ – Walter T Sep 5 '16 at 18:39
  • $\begingroup$ Response is a factor with two levels with counts greater than the stratified sample: > str(train_hc) 'data.frame': 250000 obs. of 29 variables: $ RESPONSE : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ... > str(test_hc) 'data.frame': 250000 obs. of 29 variables: $ RESPONSE : Factor w/ 2 levels "0","1": 1 2 1 1 1 1 1 1 1 1 ... $\endgroup$ – Walter T Sep 5 '16 at 18:51
  • $\begingroup$ @walterT Does the code run without problem if you do not set sampsize? $\endgroup$ – Jim Sep 6 '16 at 9:10
  • $\begingroup$ wow ... it is running now & I think it will run. Thanks!! I don't understand why it now works w/o sampsize=. I need sampsize because I have unbalanced data near a 1% response rate (1 to 99 ratio). What are my options? $\endgroup$ – Walter T Sep 6 '16 at 20:08
  • $\begingroup$ Thanks again!! I now have an idea of the constraint you suggested. I was able to use strata= and sampsize= after using a much smaller analysis file. I imagine that the rF process needs extra overhead for strata= and sampsize= even though sampling normally reduces the need for system resources. I will continue to try to uncover the threshold. $\endgroup$ – Walter T Sep 7 '16 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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