简体   繁体   中英

R: Random forest with raster as response and explanitory variable

I want to create a fire occurence probability map with random forest method. My response variable is a raster with the mean annual burned area per grid cell. My explanitory variables are mulitple rasters (temperature, elevation, land use and population density). Is it possible to use a raster as the response variable and how would a basic codeline look like? I couldn't find any information on that.

files <- list.files(path="C:/Users/fsorb/OneDrive/Desktop/test/fire_prob", pattern="grd", all.files=FALSE, full.names=TRUE,recursive=TRUE)
predictors <- stack(files)
fire <- raster("C:/Users/fsorb/OneDrive/Desktop/test/env_data/fire.tif")
fire_occ_prob <- randomForest(fire ~ ., data = predictors, ntree=500)

So is the code I have so far, but I get the error: Error in as.data.frame.default(data): can not transform 'structure("RasterStack", package = "raster")' into data.frame

I tried to save the fire raster as.dataframe but all grid cells only get NA value.

I would try to

  1. convert the response (fire) raster to points
  2. extract the values of the predictors at the points
  3. train a random forest model using the resulting data frame.
require(raster)
require(sf)
require(dplyr)
require(randomForest)

files <- list.files(path="C:/Users/fsorb/OneDrive/Desktop/test/fire_prob", pattern="grd", all.files=FALSE, full.names=TRUE,recursive=TRUE)
predictors <- stack(files)
fire <- raster("C:/Users/fsorb/OneDrive/Desktop/test/env_data/fire.tif")

# convert raster to point
response <- rasterToPoints(fire, spatial = TRUE) %>% st_as_sf()
response$ID <- c(1:nrow(response))
colnames(response)[1] <- "response"

# combine predictor values with the response
rs_preds <- full_join(terra::extract(x=r2, y=response, df=TRUE), 
                      st_drop_geometry(response), by="ID")

# train random forest
fire_occ_prob <- randomForest(response ~ .,
                              data = rs_preds[,!names(rs_preds) %in% "ID"],
                              ntree=500,
                              importance = TRUE)
# plot variable importance
varImpPlot(fire_occ_prob)

# make spatial predictions
sp_pred <- raster::predict(predictors, model=fire_occ_prob)

If your aim is to make spatial (temporal) predictions, make sure to use a spatial (temporal) (cross-) validation strategy. For further information take a look at eg Roberts et al. (2016): https://doi.org/10.1111/ecog.02881

Greetings, Jan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM