2016-07-25 160 views
2

我建立了我的随机森林模型非常好,看到代码随机森林模型错误(错误的,如果(N == 0)停止(“数据(x)具有0行”))

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

但随后,给出以下

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

错误请参见下面

'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 ... 

我的数据的结构可能是什么问题呢?

这里是我的数据集看起来像

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)      
+0

'na.action = na.omit'?你有没有每个行都有NAs的可能?请参阅http://stackoverflow.com/questions/8370455/how-to-build-random-forests-in-r-with-missing-na-values – zx8754

+0

没有NAs,我已经删除了NA功能,仍然得到那个错误。 – jmutua

+0

请提供[可重现的数据](http://stackoverflow.com/questions/5963269),所以我们得到相同的错误。 – zx8754

回答

0

公式(第一输入随机森林)是错误的样本。您必须在公式中指定班级类型和训练数据,以便算法知道要训练什么。

就你而言,我假设你正试图从BushCategories预测NDVI。所以你应该使用的公式是NDVI〜BushCategories。第二个元素是训练数据。所以,只要解决它,它应该为你工作。