简体   繁体   English

在R中使用randomForest包,如何映射随机森林预测?

[英]Using randomForest package in R, how to map Random forest prediction?

enter image description here I am trying to use randomforest to generate a spatial prediction map. 在这里输入图像描述,我正在尝试使用randomforest生成空间预测图。

I developed my model by using random forest regression, but I met a little difficulty in the last step to use the best predictors for building the predictive map. 我使用随机森林回归法开发了模型,但在最后一步遇到了一些困难,无法使用最佳预测器来构建预测图。 I want to create a map prediction map. 我想创建一个地图预测地图。

My code: 我的代码:

library(raster)
library(randomForest)

set.seed(12)
s <- stack("Density.tif", "Aqui.tif", "Rech.tif", "Rainfall.tif","Land Use.tif", "Cond.tif", "Nitrogen.tif", "Regions.tif","Soil.tif","Topo.tif", "Climatclass.tif", "Depth.tif")

points <- read.table("Coordonnées3.txt",header=TRUE, sep="\t", dec=",",strip.white=TRUE)

d <- extract(s, points)
rf <-randomForest(nitrate~ . , data=d, importance=TRUE, ntree=500, na.action = na.roughfix)
p <- predict(s, rf)

plot(p)

Sample Data: 样本数据:

> head(points)
       LAT      LONG
1 -13.057007 27.549580
2  -4.255000 15.233745
3   5.300000 -1.983610
4   7.245675 -4.233336
5  12.096330 15.036016
6  -4.255000 15.233745

The error when I run my short code is: 运行我的短代码时的错误是:

Error in eval(expr, envir, enclos) : object 'nitrate' not found.

I am guessing the error happens when you fit the model. 我猜测当您拟合模型时会发生错误。

Why would there be a variable called nitrate . 为什么会有一个叫做nitrate的变量。 Given how you create your RasterStack, perhaps there is one called Nitrogen . 根据您如何创建RasterStack的方式,也许有一种叫做Nitrogen Either way you can find out by looking at names(s) and colnames(d) . 无论哪种方式,您都可以通过查看names(s)colnames(d)来找出。

NOTE that your points are not good! 注意您的points不好! They are in reverse order. 它们的顺序相反。 The order should be (longitude, latitude). 顺序应为(经度,纬度)。

Based on your comments (please edit your question instead), you should add nitrate the points file (the third column) or something like that. 根据您的评论(请编辑您的问题),您应该添加硝酸盐积分文件(第三列)或类似内容。 Then do 然后做

 xy <- points[, 2:1]
 nitrate <- points[,3]

Extract points and combine with your observed data 提取点并与您观察到的数据结合

 d <- extract(s, xy)
 d <- cbind(nitrate=nitrate, d)

Build model and predict 建立模型并进行预测

 rf <-randomForest(nitrate~ . , data=d, importance=TRUE, ntree=500, na.action = na.roughfix)
 p <- predict(s, rf)

It sounds like the error is coming when you are trying to build the forest. 听起来好像是在尝试构建目录林时出现错误。 It may be most helpful to not use the formula interface. 不使用公式界面可能会最有帮助。 Also, if d is large, then using the formula interface is not advisable. 同样,如果d大,则不建议使用公式界面。 From the help file on randomForest : "For large data sets, especially those with large number of variables, calling randomForest via the formula interface is not advised: There may be too much overhead in handling the formula." randomForest的帮助文件中:“对于大型数据集,尤其是那些具有大量变量的数据集,不建议通过公式接口调用randomForest:处理公式可能会产生过多开销。”

Assuming d$nitrate exists then the solution is randomForest(y = d$nitrate, x = subset(d, select = -nitrate), importance=TRUE, ntree=500, na.action = na.roughfix) 假设存在d$nitrate ,则解决方案为randomForest(y = d$nitrate, x = subset(d, select = -nitrate), importance=TRUE, ntree=500, na.action = na.roughfix)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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