- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
新来的,如果您需要更多信息,请告诉我。
我的目标:我使用 Rehfeldt 气候数据和 eBird 存在/不存在数据来生成使用随机森林模型的利基模型。
我的问题:我想预测整个北美的利基模型。 Rehfeldt 气候栅格具有大陆上每个单元的数据值,但这些值被“海洋单元”中的 NA 包围。看图here ,我将 NA 涂成深绿色。如果独立数据集包含 NA,则 randomForest::predict() 不会运行。因此,我想裁剪我的气候栅格(或设置工作范围?)以便 predict() 函数仅对包含数据的单元格进行操作。
故障排除:
我使用较小的范围运行了随机森林模型,其中不包括栅格的“NA 海洋”,并且模型运行良好。所以,我知道 NA 是问题所在。但是,我不想只针对北美的一小块矩形区域预测我的利基模型。
我使用了 flowla 的方法 here用于使用北美的多边形 shapefile 裁剪和屏蔽栅格。我希望这会删除 NA,但事实并非如此。有什么类似的方法可以去除 NA 吗?
我读了一些书,但想不出一种方法来调整随机森林代码本身,以便 predict() 忽略 NA。 This post看起来很相关,但我不确定它对我的情况是否有帮助。
数据
我的栅格、输入的存在/不存在文本文件和附加功能的代码是 here .与下面的主要代码一起使用以获得可重现的示例。
代码
require(sp)
require(rgdal)
require(raster)
library(maptools)
library(mapproj)
library(dismo)
library(maps)
library(proj4)
data(stateMapEnv)
# This source code has all of the functions necessary for running the Random Forest models, as well as the code for the function detecting multi-collinearity
source("Functions.R")
# Read in Rehfeldt climate rasters
# these rasters were converted to .img and given WGS 84 projection in ArcGIS
d100 <- raster("d100.img")
dd0 <- raster("dd0.img")
dd5 <- raster("dd5.img")
fday <- raster("fday.img")
ffp <- raster("ffp.img")
gsdd5 <- raster("gsdd5.img")
gsp <- raster("gsp.img")
map <- raster("map.img")
mat <- raster("mat_tenths.img")
mmax <- raster("mmax_tenths.img")
mmin <- raster("mmin_tenths.img")
mmindd0 <- raster("mmindd0.img")
mtcm <- raster("mtcm_tenths.img")
mtwm <- raster("mtwm_tenths.img")
sday <- raster("sday.img")
smrpb <- raster("smrpb.img")
# add separate raster files into one big raster, with each file being a different layer.
rehfeldt <- addLayer(d100, dd0, dd5, fday, ffp, gsdd5, gsp, map, mat, mmax, mmin, mmindd0, mtcm, mtwm, sday, smrpb)
# plot some rasters to make sure everything worked
plot(d100)
plot(rehfeldt)
# read in presence/absence data
LAZB.INBUtemp <- read.table("LAZB.INBU.txt", header=T, sep = "\t")
colnames(LAZB.INBUtemp) <- c("Lat", "Long", "LAZB", "INBU")
LAZB.INBUtemp <- LAZB.INBUtemp[c(2,1,3,4)]
LAZB.INBU <- LAZB.INBUtemp
latpr <- (LAZB.INBU$Lat)
lonpr <- (LAZB.INBU$Long)
sites <- SpatialPoints(cbind(lonpr, latpr))
LAZB.INBU.spatial <- SpatialPointsDataFrame(sites, LAZB.INBU, match.ID=TRUE)
# The below function extracts raster values for each of the different layers for each of the eBird locations
pred <- raster::extract(rehfeldt, LAZB.INBU.spatial)
LAZB.INBU.spatial@data = data.frame(LAZB.INBU.spatial@data, pred)
LAZB.INBU.spatial@data <- na.omit(LAZB.INBU.spatial@data)
# ITERATIVE TEST FOR MULTI-COLINEARITY
# Determines which variables show multicolinearity
cl <- MultiColinear(LAZB.INBU.spatial@data[,7:ncol(LAZB.INBU.spatial@data)], p=0.05)
xdata <- LAZB.INBU.spatial@data[,7:ncol(LAZB.INBU.spatial@data)]
for(l in cl) {
cl.test <- xdata[,-which(names(xdata)==l)]
print(paste("REMOVE VARIABLE", l, sep=": "))
MultiColinear(cl.test, p=0.05)
}
# REMOVE MULTI-COLINEAR VARIABLES
for(l in cl) { LAZB.INBU.spatial@data <- LAZB.INBU.spatial@data[,-which(names(LAZB.INBU.spatial@data)==l)] }
################################################################################################
# FOR LAZB
# RANDOM FOREST MODEL AND RASTER PREDICTION
require(randomForest)
# NUMBER OF BOOTSTRAP REPLICATES
b=1001
# CREATE X,Y DATA
# use column 3 for LAZB and 4 for INBU
ydata <- as.factor(LAZB.INBU.spatial@data[,3])
xdata <- LAZB.INBU.spatial@data[,7:ncol(LAZB.INBU.spatial@data)]
# PERCENT OF PRESENCE OBSERVATIONS
( dim(LAZB.INBU.spatial[LAZB.INBU.spatial$LAZB == 1, ])[1] / dim(LAZB.INBU.spatial)[1] ) * 100
# RUN RANDOM FORESTS MODEL SELECTION FUNCTION
# This model is using the model improvement ratio to select a final model.
pdf(file = "LAZB Random Forest Model Rehfeldt.pdf")
( rf.model <- rf.modelSel(x=xdata, y=ydata, imp.scale="mir", ntree=b) )
dev.off()
# RUN RANDOM FORESTS CLASS BALANCE BASED ON SELECTED VARIABLES
# This code would help in the case of imbalanced sample
mdata <- data.frame(y=ydata, xdata[,rf.model$SELVARS])
rf.BalModel <- rfClassBalance(mdata[,1], mdata[,2:ncol(mdata)], "y", ntree=b)
# CREATE NEW XDATA BASED ON SELECTED MODEL AND RUN FINAL RF MODEL
sel.vars <- rf.model$PARAMETERS[[3]]
rf.data <- data.frame(y=ydata, xdata[,sel.vars])
write.table(rf.data, "rf.data.txt", sep = ",", row.names = F)
# This the code given to me; takes forever to run for my dataset (I haven't tried to let it finish)
# ( rf.final <- randomForest(y ~ ., data=rf.data, ntree=b, importance=TRUE, norm.votes=TRUE, proximity=TRUE) )
# I use this form because it's a lot faster
( rf.final <- randomForest(x = rf.data[2:6], y = rf.data$y, ntree=1000, importance=TRUE, norm.votes=TRUE, proximity=F) )
################################################################################################
# MODEL VALIDATION
# PREDICT TO VALIDATION DATA
# Determines the percent correctly classified
rf.pred <- predict(rf.final, rf.data[,2:ncol(rf.data)], type="response")
rf.prob <- as.data.frame(predict(rf.final, rf.data[,2:ncol(rf.data)], type="prob"))
ObsPred <- data.frame(cbind(Observed=as.numeric(as.character(ydata)),
PRED=as.numeric(as.character(rf.pred)), Prob1=rf.prob[,2],
Prob0=rf.prob[,1]) )
op <- (ObsPred$Observed == ObsPred$PRED)
( pcc <- (length(op[op == "TRUE"]) / length(op))*100 )
# PREDICT MODEL PROBABILITIES RASTER
# The first line of code says what directory I'm working, and then what folder in that directory has the raster files that I'm using to predict the range
# The second line defines the x variable, wich is my final Random Forest model
rpath=paste('~/YOURPATH', "example", sep="/")
xvars <- stack(paste(rpath, paste(rownames(rf.final$importance), "img", sep="."), sep="/"))
tr <- blockSize(xvars)
s <- writeStart(xvars[[1]], filename=paste('~/YOURPATH', "prob_LAZB_Rehfeldt.img", sep="/"), overwrite=TRUE)
for (i in 1:tr$n) {
v <- getValuesBlock(xvars, row=tr$row[i], nrows=tr$nrows[i])
v <- as.data.frame(v)
rf.pred <- predict(rf.final, v, type="prob")[,2]
writeValues(s, rf.pred, tr$row[i])
}
s <- writeStop(s)
prob_LAZB <- raster("prob_LAZB_Rehfeldt.img")
# Write range prediction raster to .pdf
pdf(file="LAZB_range_pred.pdf")
plot(prob_LAZB)
map("state", add = TRUE)
dev.off()
谢谢!!
最佳答案
您是否尝试在调用 RF 时设置“na.action”? randomForest R manual 中清楚地标明了该选项.您对 RF 的调用如下所示:
rf.final <- randomForest(x = rf.data[2:6], y = rf.data$y, ntree=1000, importance=TRUE, norm.votes=TRUE, proximity=F, na.action = omit)
这将告诉 RF 忽略存在 NA 的行,从而丢弃这些观察结果。这不一定是最好的方法,但在您的情况下可能会很方便。
选项 2:rfImpute
或 na.roughfix
:这将填充您的 NA,以便您可以继续进行预测。当心,因为这可能会在 NA 被估算/“固定”的任何地方给你虚假的预测。
选项 3:从选项 2 开始,在获得预测后,将栅格导入您选择的 GIS/图像处理软件,并屏蔽掉不需要的区域。就您而言,掩盖水体非常简单。
关于r - 栅格和 randomForest::predict() 中的 NA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24270908/
在 R Language Definition 中,对NA值进行了简要描述,其中一部分说 ... In particular, FALSE & NA is FALSE, TRUE | NA is TR
我对 R 还很陌生,目前遇到一个问题,数据如下所示: ID h1 h2 h3 h4 h5 h6 h7 h8
我有一个 csv包含国家名称及其 ISO 代码的文件。这是它的样子: "Name","Code" "Afghanistan","AF" "Albania","AL" "Algeria","DZ" "N
我想用 dplyr 解决以下问题。最好与窗口功能之一一起使用。我有一个包含房屋和购买价格的数据框。下面是一个例子: houseID year price 1 19
在data.frame(或data.table)中,我想用最接近的先前非NA值“向前填充”NA。一个使用向量(而不是 data.frame)的简单示例如下: > y yy [1] NA NA NA
这是一个示例数据框: > df = data.frame(rep(seq(0, 120, length.out=6), times = 2), c(sample(1:50, 4), + NA, NA,
我有一个包含条目的数据框;似乎这些值不被视为 NA,因为 is.na 返回 FALSE。我想将这些值转换为 NA 但找不到方法。 最佳答案 使用 dfr[dfr==""]=NA哪里dfr是你的数据框。
我有一个示例表,其中包含一些但不是全部 NA需要替换的值。 > dat id message index 1 1 1 2 1 foo 2 3 1
在 R 中,如果从 NA 中减去一个数字,它将返回 NA: > x NA - x [1] NA 但是如果你尝试从 NA 中减去一个日期,它会返回一个错误: > x NA - x Error in
这个问题在这里已经有了答案: Logical operators (AND, OR) with NA, TRUE and FALSE (2 个答案) 关闭 4 年前。 为什么在 R 中会这样? >
我有一个看起来像这样的数据框: SampleNo Lab1 Lab2 Lab3 lab4 lab5 lab6 lab7 lab8 lab9 lab10 1 59
我有一个按“id”分组的数据框和一个包含缺失值的变量“age”,NA。 在每个“id”中,我想替换“age”的缺失值,但只“填充”之前 第一个 非NA 值。 data % group_by(id) %
我有如下所示的数据框: df df id value v1 v2 v3 1 1 351 NA 1 0 2 2 585 0 1 1 3 3 321 NA 0 1 4
所以我有一个数据集,只需查看它,数据集中就有明显的 NA。 > dput(bmi.cig) structure(list(MSI.subset.BMI = structure(c(4L, 4L, 4
我有两个 30m x 30m 的光栅文件,我想从中采样点。在采样之前,我想从图像中移除模糊区域。我求助于 R 和 Hijman 的 Raster 包来完成这项任务。 使用 drawPoly(sp=TR
我有以下时间序列 > y y[c(1,2,5,9,10)] y [,1] 2011-09-04 NA 2011-09-05 NA 2011-09-06 3 201
这个问题在这里已经有了答案: Replace missing values (NA) with most recent non-NA by group (7 个回答) 5年前关闭。 我有一个 DF 个
我想向我的数据框中添加一个新变量 (N_notNAs),它定义了其他任何变量是否为 NA。 x y z N_notNAs 2 3 NA NA NA 1 3 NA 2
我有一个名为 SMOKE 的因子,级别为“Y”和“N”。缺失值被替换为 NA(从初始级别“NULL”开始)。然而,当我查看这个因素时,我得到这样的结果: head(SMOKE) # N N Y Y
假设我有以下 data.frame: t<-c(1,1,2,4,5,4) u<-c(1,3,4,5,4,2) v<-c(2,3,4,5,NA,2) w<-c(NA,3,4,5,2,3) x<-c(2,
我是一名优秀的程序员,十分优秀!