2
$\begingroup$

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

sampsize(70,70)

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?

$\endgroup$
0

1 Answer 1

1
$\begingroup$

you probably want

sampsize(c(70,70))

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

$\endgroup$
5
  • $\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
    Commented Mar 17, 2014 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$ Commented Mar 17, 2014 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
    Commented Jul 17, 2014 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$ Commented Jul 19, 2014 at 19:53
  • $\begingroup$ it is the full confusion matrix that confused me! Your explanation makes sense though - thank you very much! $\endgroup$
    – sth
    Commented Jul 21, 2014 at 19:58

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.