我有一个使用Bagged trees
模型的工作示例。但是,在大区域中,计算时间非常长且占用大量内存(16 Gb RAM上最大为1200x1200像素区域)。
您能否帮助我将此示例重新制作为ranger
包中实现的随机森林模型,并通过covstack
加载协变量?
execGRASS("g.region", flags = "p", region = "10m_envelope")
grids10m <- readGDAL("soil.asc")
names(grids10m) <- "soil"
grids10m$soil.c <- as.factor(grids10m$soil)
LSP.list <- c("dem.asc", "twi.asc", "gmf.asc", "rad.asc", "aspect.asc",
"slope.asc", "profc.asc", "planc.asc", "longc.asc",
"minic.asc", "maxic.asc", "flowdirect.asc", "flowaccum.asc",
"flowlength.asc", "diststream.asc", "medial.asc")
for(i in 1:length(LSP.list)){
grids10m@data[strsplit(LSP.list[i], ".asc")[[1]]] <-
readGDAL(LSP.list[i])$band1
}
#--------- Bagged Trees model----------
library(ipred)
traindatastat <- readOGR("traindatastat.shp", "traindatastat")
traindatastat <- na.omit(traindatastat)
traindatastat$soil <- as.factor(traindatastat$soil)
traindatastat$gmf <- as.factor(traindatastat$gmf)
traindatastatd = as.data.frame(traindatastat)
#--------- train model ----------
BGT.soilpredict.train <- bagging(soil ~ dem + twi + rad + slope +
longc + maxic + flowaccum + flowlength +
diststream, data = traindatastatd,
nbagg = 100)
pred.BGT.soilpredict.train <- predict(BGT.soilpredict.train,
newdata = traindatastatd)
DSM_data5s <- as.data.frame(grids10m@data)
#--------- Load full dataset (covariates) ----------
files <- c("dem.asc", "twi.asc", "gmf.asc", "rad.asc", "aspect.asc",
"slope.asc", "profc.asc", "planc.asc", "longc.asc",
"minic.asc", "maxic.asc", "flowdirect.asc", "flowaccum.asc",
"flowlength.asc", "diststream.asc")
covStack <- raster(files[1])
for (i in 2:length(files)) {
covStack <- stack(covStack, files[i])
}
#--------- Predict categorial data (soil type) ----------
pred.BGT.soilpredict <- predict(covStack, BGT.soilpredict.train, type = "class",
"map.BGT.rSoil.tif", format = "GTiff",
datatype = "INT2S", overwrite = TRUE)
spplot(pred.BGT.soilpredict, col.regions=rainbow, cuts = 5,
scales=list(draw = TRUE), main = "Predict soils by Bagged Trees model")