【问题标题】:Using randomForest package in R, how to map Random forest prediction?在 R 中使用 randomForest 包,如何映射随机森林预测?
【发布时间】:2016-11-11 03:54:06
【问题描述】:

enter image description here我正在尝试使用随机森林来生成空间预测图。

我使用随机森林回归开发了我的模型,但在最后一步中遇到了一点困难,即使用最佳预测器来构建预测图。我想创建一个地图预测图。

我的代码:

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)

样本数据:

> 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

我运行短代码时的错误是:

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

【问题讨论】:

  • head(d) 将有助于更好地调试您的问题,并查看d 是否有 nitrate 或 Nitrogen 作为列名。
  • 哪一行代码产生了错误?是模型拟合还是预测?
  • 我在该步骤运行时出现代码错误:rf
  • 我有十二个光栅格式的预测变量。不同的栅格存储在: s

标签: r random prediction


【解决方案1】:

我猜当你拟合模型时会发生错误。

为什么会有一个名为nitrate的变量。鉴于您创建 RasterStack 的方式,也许有一个称为 Nitrogen。无论哪种方式,您都可以通过查看 names(s)colnames(d) 找到答案。

请注意,您的points 不好!它们的顺序相反。顺序应该是(经度,纬度)。

根据您的 cmets(请改为编辑您的问题),您应该 添加硝酸盐点文件(第三列)或类似的东西。然后做

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

提取点并结合您观察到的数据

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

构建模型并预测

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

【讨论】:

  • 我有十二个光栅格式的预测变量。不同的栅格存储在: s
  • 如果我将硝酸盐值添加到“点”,我获得:头部(点)ln.n.n.n.no3._mean lat长1 0.2311117 -13.057007 27.549580 2 0.3148107 -4.255000 15.233745 3 0.3162695 3 0.3162695 3 0.3162695 5.300000 -1.983610 4 0.4050-1.983610 4 0.4050 -1.983610 4 0.4050 -1.9836104 7.245675 -4.233336 5 0.4054651 12.096330 15.036016 6 0.6205765 -4.255000 15.233745 在这种情况下,如果我将硝酸盐值与 LAT 和 LONG 放在一起,当我再次运行时,出现的错误是:> d
  • 名为 nitrate 的变量不是光栅格式。而是一个定量变量。
【解决方案2】:

听起来好像是在您尝试构建森林时出现错误。不使用公式界面可能最有帮助。另外,如果d 很大,那么不建议使用公式界面。来自randomForest 的帮助文件:“对于大型数据集,尤其是具有大量变量的数据集,不建议通过公式接口调用 randomForest:处理公式的开销可能太大。”

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

【讨论】:

    猜你喜欢
    • 2019-05-04
    • 2014-06-14
    • 2014-08-07
    • 2014-08-17
    • 2019-04-15
    • 2017-12-25
    • 2014-03-23
    • 2021-03-21
    相关资源
    最近更新 更多