I am trying to predict species presence or absence using randomForest in R (classification). In fact, I am trying to do it for several species, in separate models.

For a couple of the species, the training data are quite unbalanced e.g., 70 observations of species presence, and and 6500 observations of species absence.

This is my code:

#read in data frame containing observations of species presence/absence and predictor     variables
mydata <- read.csv('mydata.csv')

#fit random forests model
fitmodelA <- randomForest(SPECIESA ~ var1 + var2 + var3 + var4 + var5 +var6 + var7 +   var8 + var9 + var10, data=mydata, mytry=3, ntrees=500, replace=TRUE, importance=TRUE,   keep.forest=TRUE)

#predict to new data
predictmodel <- predict(fitmodelA, newdata, type="prob")

In the output prediction, almost the entire study area is predicted with prob > 0.7. I take this to be predictions of species occurrence? or is it the probability of species absence?

I want to try to balance the data by forcing the model to select equal sample sizes from observations of presence and absence, e.g., adding the argument


But I get the error message "Error in if (ncol(x) != ncol(xtest)) stop("x and xtest must have same number of columns")"

What am I doing wrong here?


you probably want


You can also play with class weights which influence the gini impurity function for picking splits. Check out this paper

  • $\begingroup$ Thank you Andrew, your suggestion worked. I have also seen examples that use the term "rep" in the sampsize argument. Any idea of how this works? $\endgroup$ – sth Mar 17 '14 at 22:26
  • $\begingroup$ rep(70, 2) = [70, 70] which is the same as c(70, 70). Type ?rep to read about the function $\endgroup$ – Andrew Cassidy Mar 17 '14 at 22:50
  • 1
    $\begingroup$ I used the argument sampsize=(c(70,70)) and while I don't get an error message, it doesn't appear to be working, or I don't understand what is going on behind the scenes. The confusion matrix that is output displays the predictions and class error for the full, unbalanced sample size, not the number that I specified. I tried adding the argument strata="mydata$SPECIESA" in order to make sure it knew what variable to sample from, but that does not seem to make a difference. Do you have any idea what could be going wrong? $\endgroup$ – sth Jul 17 '14 at 22:48
  • $\begingroup$ @sth the outputed confusion matrix should be the full unbalanced sample size. The sampsize argument is used on a "per-tree" basis. I admit that the outputed confusion matrix is particularly confusing. The sampsize argument says for each tree in the forest sample 70 of class 1 and 70 of class 2 (with replacement by default) and then split this data into a training and testing set. So each tree has a training and testing set. Can you elaborate on anything else that is specifically wrong besides the full confusion matrix. I would potentially open a new question so I can post code as well. $\endgroup$ – Andrew Cassidy Jul 19 '14 at 19:53
  • $\begingroup$ it is the full confusion matrix that confused me! Your explanation makes sense though - thank you very much! $\endgroup$ – sth Jul 21 '14 at 19:58

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.