2

I've set up my random forests model very well, see the code

modelRF <- randomForest(x=p$NDVI,
                   y=p$BushCategories,
                   ntree=500, do.trace=TRUE, 
                   importance=TRUE, forest=TRUE, na.action=na.omit)

but then it gives the error below

Error in if (n == 0) stop("data (x) has 0 rows") : 
argument is of length zero

See the structure of my data below

'data.frame':   197 obs. of  5 variables:
$ Waypoint_No   : chr  "OMATSC028" "OMATSC200" "OMATSC072N" "OMATSC317" ...
$ Longitude     : num  17.7 17.6 17.8 17.9 17.9 ...
$ Latitude      : num  -21.2 -21.2 -21.1 -20.9 -21.1 ...
$ NDVI          : num  0.256 0.327 0.25 0.268 0.283 ...
$ BushCategories: Factor w/ 4 levels "0-25%","26-50%",..: 3 1 2 3 2 3 1 2 1 
1 ...

What could be the problem?

Here is a sample of how my dataset looks like

x<-c('0.256', '0.327', '0.25', '0.268')
y<-c('0-25%','26-50%','51-75%','76-100%')
data<-data.frame(x,y)                       
jmutua
  • 290
  • 1
  • 12
  • `na.action=na.omit` ? Do you have NAs on every row maybe? See http://stackoverflow.com/questions/8370455/how-to-build-random-forests-in-r-with-missing-na-values – zx8754 Jul 25 '16 at 08:10
  • There are no NAs, I've removed the NA function and still getting that error. – jmutua Jul 25 '16 at 08:24
  • Please provide [reproducible data](http://stackoverflow.com/questions/5963269) so we get the same error. – zx8754 Jul 25 '16 at 08:27
  • 1
    @zx8754 I've update the question with sample data. – jmutua Jul 25 '16 at 08:40

1 Answers1

0

Your formula (first input to randomForest) is wrong. You have to specify the class types and the training data in your formula so that the algorithms know what to train on.

In your case, I assume you are trying to predict NDVI from BushCategories. So the formula that you should use is NDVI ~ BushCategories. The second element is the training data. So, just fix that and it should work for you.

Vahid Zadeh
  • 564
  • 4
  • 25