Error in Random Forest: "Need at least two classes to do classification"

10,334

The problem is you only have one level after converting pa to a factor. Most likely, you only have presences.

You can check this with:

table(factor(envtrain$pa))

If you only have presences you will need to generate pseudo-absences. For a random forest, the same number of pseudo-absences as presences is recommended here.

Share:
10,334
Dimitris K.
Author by

Dimitris K.

Updated on July 24, 2022

Comments

  • Dimitris K.
    Dimitris K. almost 2 years

    I'm trying to do a species distribution model and I'm following the guide "Species distribution modeling with R" from Robert J. Hijmans and Jane Elith.

    Everything seems to be OK but when I'm trying to run the RandomForest I get this error message:

    "Need at least two classes to do classification". 
    

    The code I'm using is:

    library(randomForest)
    model <- factor (pa) ~ Bio3 + Bio2 + Bio16 + Bio11 + aspect + depth + dem + my_epikrat + corine_2000
    rfabies <- randomForest(model, data=envtrain, na.action=na.omit, ntrees=1000)
    
    
    Error in randomForest.default(m, y, ...) :
      Need at least two classes to do classification.
    

    What may cause this problem? Can anyone help me?