随机森林模型的预测

时间:2016-01-05 09:12:06

标签: r output raster random-forest predict

我实施了随机森林,然后开始使用10个变量预测森林中的叶面积指数。但是当预测停止时,预测的输出是具有空值的空地图。如果有人帮助我,我感激不尽。我在这里添加了我正在运行的代码。

在第一步中,我在栅格中为NA值指定零。

tex37$glcm_variance_ENVI[is.na(tex37$glcm_variance_ENVI)] <- 0
tex37$glcm_entropy[is.na(tex37$glcm_entropy)] <- 0
RVI_raster3$glcm_mean_ENVI[is.na(RVI_raster3$glcm_mean_ENVI)] <- 0
RVI_raster3$glcm_mean[is.na(RVI_raster3$glcm_mean)] <- 0
RVI_raster3$glcm_variance[is.na(RVI_raster3$glcm_variance)] <- 0
RVI_raster5$glcm_mean_ENVI[is.na(RVI_raster5$glcm_mean_ENVI)] <- 0
RVI_raster5$glcm_variance[is.na(RVI_raster5$glcm_variance)] <- 0
RVI_raster5$glcm_mean[is.na(RVI_raster5$glcm_mean)] <- 0
RVI_raster7$glcm_mean_ENVI[is.na(RVI_raster7$glcm_mean_ENVI)] <- 0
RVIrededge[is.na(RVIrededge)] <- 0

在这里,我将所有10个栅格图层堆叠在一个栅格中。

image_stack_imp = stack(tex37$glcm_variance_ENVI,
                     tex37$glcm_entropy,
                     RVI_raster3$glcm_mean_ENVI,
                     RVI_raster3$glcm_mean,
                     RVI_raster3$glcm_variance,
                     RVI_raster5$glcm_mean_ENVI,
                     RVI_raster5$glcm_variance,
                     RVI_raster5$glcm_mean,
                     RVI_raster7$glcm_mean_ENVI,
                     RVIrededge)

我根据我的功能创建了一个表格,并将Leaf区域索引作为标签。

Table_Importance = data.frame(LAI=d$LAI,
                          tex37.glcm_variance_ENVI=LAI37$glcm_variance_ENVI,
                          tex37.glcm_entropy=LAI37$glcm_entropy,
                          RVI_raster3.glcm_mean_ENVI=RVI3$glcm_mean_ENVI,
                          RVI_raster3.glcm_variance=RVI3$glcm_variance,
                          RVI_raster3.glcm_mean=RVI3$glcm_mean,
                          RVI_raster5.glcm_variance=RVI5$glcm_variance,
                          RVI_raster5.glcm_mean_ENVI=RVI5$glcm_mean_ENVI,
                          RVI_raster5.glcm_mean=RVI5$glcm_mean,
                          RVI_raster7.glcm_mean_ENVI=RVI7$glcm_mean_ENVI,
                          RVIrededge_values)

最后运行Randomforest:

set.seed(104)
ind <- sample(2, nrow(Table_Importance), replace = 1, prob=c(0.66,0.33))
rf_imp<- randomForest(LAI ~ ., data=Table_Importance[ind ==     1,],importance=TRUE, ntree=1000, mtry=3)
pred_imp <- predict(rf_imp, Table_Importance[ind == 2,])
rmse(Table_Importance[ind==2, "LAI"], pred_imp)
###############################predicting########################
Map_LAI = predict(image_stack_imp, rf_imp,na.rm = TRUE, progress='window', type ='response')
plot(Map_LAI)

以下是小样本:

(head(Table_Importance, 20)

1 个答案:

答案 0 :(得分:0)

自己发现问题,:D 答案只是表属性的名称应该与训练表的名称相同。在我的例子中,属性名称如下:

Table_Importance = data.frame(LAI=d$LAI,
                          glcm_variance_ENVI=LAI37$glcm_variance_ENVI,
                          glcm_entropy=LAI37$glcm_entropy,
                          glcm_mean_ENVI.1=RVI3$glcm_mean_ENVI,
                          glcm_variance.1=RVI3$glcm_variance,
                          glcm_mean.1=RVI3$glcm_mean,
                          glcm_variance.2=RVI5$glcm_variance,
                          glcm_mean_ENVI.2=RVI5$glcm_mean_ENVI,
                          glcm_mean.2=RVI5$glcm_mean,
                          glcm_mean_ENVI.3=RVI7$glcm_mean_ENVI,
                          layer=RVIrededge_values)