簡體   English   中英

R:以柵格作為響應和解釋變量的隨機森林

[英]R: Random forest with raster as response and explanitory variable

我想用隨機森林方法創建一個火災發生概率 map。 我的響應變量是一個柵格,每個網格單元的平均年燃燒面積。 我的解釋變量是多個柵格(溫度、海拔、土地利用和人口密度)。 是否可以使用柵格作為響應變量以及基本代碼行的外觀如何? 我找不到任何相關信息。

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)

到目前為止我的代碼也是如此,但我收到錯誤:as.data.frame.default(data) 中的錯誤:無法將 'structure("RasterStack", package = "raster")' 轉換為 data.frame

我試圖將火光柵保存為.dataframe 但所有網格單元格只獲得 NA 值。

我會嘗試

  1. 將響應(火災)柵格轉換為點
  2. 提取點處預測變量的值
  3. 使用生成的數據幀訓練隨機森林 model。
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)

如果您的目標是進行空間(時間)預測,請確保使用空間(時間)(交叉)驗證策略。 有關更多信息,請查看例如 Roberts 等人。 (2016): https://doi.org/10.1111/ecog.02881

問候,揚

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM