Generating prediction raster from Random Forest model using R?
Solution 1:
The "object names" have nothing to do with the layer names, so you need to set these to match the names in the data.frame used to fit the model. In most workflows you would do something like
f <- c("cost_dist_ecotone_s.tif.tif", "cost_dist_hea_s.tif.tif", "cost_dist_medstr_s.tif.tif")
s <- stack(f)
names(s) <- gsub(".tif.tif", "", f)
And then extract values from the RasterStack to fit your model --- in that case the names already match.
But the main mistake you made was here
ApPl_prob <- raster::predict(rf1, newdata=ApPl_stack, type="prob")
The first argument should be the RasterStack:
ApPl_prob <- raster::predict(ApPl_stack, rf1, type="prob")
Or use named parameters as you did in your answer
raster::predict(model=rf1, object=ApPl_stack, type="prob")
Solution 2:
After more closely inspecting the structure of all objects generated by the code above, I found the issue. For whatever reason, stack()
was changing the names of the raster layers back to their original file names, rather than the object names I had assigned. I hadn't noticed the problem initially, as plot(ApPl_stack)
showed the names I expected to be there, even though they weren't actually reflected in the structure of the raster stack. As a result, the raster names from the stack provided to raster::predict()
didn't match those in the Random Forest model.
Adding in an extra step to assign matching names solved the problem:
names(ApPl_stack) <- c("COST_DIST_ECOTONE", "COST_DIST_HEA", "COST_DIST_MEDSTR", "COST_DIST_RIV_COAST", "DEM30_ASP_RE_2", "DEM30_ASP_RE_3", "DEM30_ASP_RE_4", "DEM30_ASP_RE_5", "DEM30_M", "DEM30_SLOPE", "LOC_REL_RE", "LOC_SD_SLOPE", "SSURGO_ESRI_DRAINAGE_RE_2", "SSURGO_ESRI_DRAINAGE_RE_3", "SSURGO_ESRI_DRAINAGE_RE_4", "SSURGO_ESRI_DRAINAGE_RE_5", "SSURGO_ESRI_DRAINAGE_RE_6", "SSURGO_ESRI_EROSION_RE_2", "SSURGO_ESRI_EROSION_RE_3", "SSURGO_ESRI_EROSION_RE_4", "SSURGO_ESRI_LOC_DIV", "SSURGO_ESRI_NATIVEVEG_2", "SSURGO_ESRI_NATIVEVEG_3", "SSURGO_PH")
Then I was able to generate and plot predictions with no issues using the following code:
#plot predictions and save raster to file
ApPl_prob <- 1- raster::predict(model=rf1, object=ApPl_stack, type="prob")
palette <- matlab.like(20)
plot(ApPl_prob, col=palette)
writeRaster(ApPl_prob, "ApPl_prob", format='GTiff')