r语言 - 使用随机森林、插入符号和因子变量预测栅格时的误差



我试图用randomForest和插入符号包预测栅格层,但当我引入因子变量时失败。如果没有因子,一切都可以正常工作,但是一旦我引入因子,我就会得到错误:

Error in predict.randomForest(modelFit, newdata) : Type of predictors in new data do not match that of the training data.

我在下面创建了一些示例代码来完成这个过程。我用几个步骤来展示它的透明度,并提供一个工作示例。

(要跳过设置代码,请从这里向下跳转…)

首先是创建样本数据,拟合RF模型,并在没有因素的情况下预测栅格。一切正常

# simulate data
x1p <- runif(50, 10, 20) # presence
x2p <- runif(50, 100, 200)
x1a <- runif(50, 15, 25) # absence
x2a <- runif(50, 180, 400)
x1 <- c(x1p, x1a)
x2 <- c(x2p,x2a)
y <- c(rep(1,50), rep(0,50)) # presence/absence
d <- data.frame(x1 = x1, x2 = x2, y = y)
# RF Classification on data with no factors... works fine
require(randomForest)
dRF <- d
dRF$y <- factor(ifelse(d$y == 1, "present", "absent"),
                levels = c("present", "absent"))
rfFit <- randomForest(y = dRF$y, x = dRF[,1:2], ntree=100) # RF Classfication
# Create sample Rasters
require(raster)
r1 <- r2 <- raster(nrow=100, ncol=100)
values(r1) <- runif(ncell(r1), 5, 25 )
values(r2) <- runif(ncell(r2), 85, 500 )
s <- stack(r1, r2)
names(s) <- c("x1", "x2")
# raster::predict() with no factors, works fine.
model <- predict(s, rfFit, na.rm=TRUE, type="prob", progress='text')
spplot(model)

接下来的步骤是创建一个因子变量来添加到训练数据中,并为预测创建一个具有匹配值的光栅。请注意,栅格是一个常规的旧整数,而不是as.factor栅格。一切都还好……

# Create factor variable
x3p <- sample(0:5, 50, replace=T)
x3a <- sample(3:7, 50, replace=T)
x3 <- c(x3p, x3a)
dFac <- dRF
dFac$x3 <- as.factor(x3)
dFac <- dFac[,c(1,2,4,3)] # reorder
# RF model with factors, works fine
rfFit2 <- randomForest(y ~ x1 + x2 + x3, data=dFac, ntree=100)
# Create new raster, but not as.factor()
r3 <- raster(nrow=100, ncol=100)
values(r3) <- sample(0:7, ncell(r3), replace=T)
s2 <- stack(s, r3)
names(s2) <- c("x1", "x2", "x3") 
s2 <- brick(s2) # brick or stack, either work
# RF, raster::predict() from fit with factor
f <- levels(dFac$x3) # included, but not necessary
model2 <- predict(s2, rfFit2,  type="prob", 
          progress='text', factors=f, index=1:2)
spplot(model2) # works fine

在上述步骤之后,我现在有了一个RF模型,该模型使用包含因子变量的数据进行训练,并在包含类似值的整数栅格的栅格砖上进行预测。这是我的最终目标,但我希望能够通过caret包工作流程做到这一点。下面我介绍了caret::train(),没有任何因素,都很好。

# RF with Caret and NO factors
require(caret)
rf_ctrl <- trainControl(method = "cv", number=10,
           allowParallel=FALSE, verboseIter=TRUE, 
           savePredictions=TRUE, classProbs=TRUE) 
cFit1 <- train(y = dRF$y, x = dRF[,1:2], method = "rf", 
         tuneLength=4, trControl = rf_ctrl, importance = TRUE)
model3 <- predict(s2, cFit1,  type="prob", 
          progress='text', factors=f, index=1:2) 
spplot(model3) # works with caret and NO factors

(…到这里。这就是问题开始的地方)

这就是失败的地方。带有A因子变量的插入符号训练的Rf模型可以工作,但在raster::predict()处失败。

# RF with Caret and FACTORS
rf_ctrl2 <- trainControl(method = "cv", number=10,
            allowParallel=FALSE, verboseIter=TRUE, 
            savePredictions=TRUE, classProbs=TRUE)
cFit2 <- train(y = dFac$y, x = dFac[,1:3], method = "rf", 
         tuneLength=4, trControl = rf_ctrl2, importance = TRUE)
model4 <- predict(s2, cFit2,  type="prob", 
          progress='text', factors=f, index=1:2) 
# FAIL: "Type of predictors in new data do not match that of the training data."

尝试与上面相同,但不是具有与因子水平相同值的整数栅格,而是使用as.factor()将栅格转换为因子并分配级别。

#trying with raster as.factor()
r3f <- raster(nrow=100, ncol=100)
values(r3f) <- sample(0:7, ncell(r3f), replace=T)
r3f <- as.factor(r3f)
f <- levels(r3f)[[1]]
f$code <- as.character(f[,1])
levels(r3f) <- f
s2f <- stack(s, r3f)
names(s2f) <- c("x1", "x2", "x3")
s2f <- brick(s2f)
model4f <- predict(s2f, cFit2,  type="prob", 
           progress='text', factors=f, index=1:2)
# FAIL "Type of predictors in new data do not match that of the training data."

上述步骤的错误和进展清楚地表明,我的方法和caret:train()raster::predict()之间存在问题。我已经完成了调试(尽我所能)并解决了我注意到的问题,但没有确凿的证据。

任何和所有的帮助将非常感激。谢谢!

补充道:我继续乱弄,意识到如果caret::train()中的模型是用公式形式写的,它是有效的。查看模型对象的结构,很容易看到为因子变量创建了对比。我想这也意味着raster::predict()认识到了这种对比。这很好,但令人失望的是,因为我的方法没有设置为使用基于公式的预测。如有任何额外的帮助,我仍很感激。

#with Caret WITH FACTORS as model formula!
rf_ctrl3 <- trainControl(method = "cv", number=10,
            allowParallel=FALSE, verboseIter=TRUE, savePredictions=TRUE, classProbs=TRUE)
cFit3 <- train(y ~ x1 + x2 + x3, data=dFac, method = "rf", 
            tuneLength=4, trControl = rf_ctrl2, importance = TRUE)
model5 <- predict(s2, cFit3,  type="prob", progress='text') # prediction raster
spplot(model5) 

这需要进行一些测试,但答案是raster::predict()只适用于从包含因子的caret::train()生成的模型,如果模型呈现为公式(y ~ x1 + x2 + x3)而不是y = y, x = x(作为矩阵或data.frame)。只有通过公式界面,模型才能创建适当的对比或虚拟变量。没有必要通过as.factor()将栅格图层设置为因子。预测函数会帮你做这些。

如果您将函数raster::predict的输入结构转换为参数factors,则您的代码正在使用具有raster::predict的因子和具有非公式接口的插入符号模型:

f <- list(x3 = levels(dFac$x3))

(替换f <- levels(dFac$x3) # included, but not necessary行)

代码

# RF with Caret and FACTORS
rf_ctrl2 <- trainControl(method = "cv", number=10,
                         allowParallel=FALSE, verboseIter=TRUE, 
                         savePredictions=TRUE, classProbs=TRUE)
cFit2 <- train(y = dFac$y, x = dFac[,1:3], method = "rf", 
                tuneLength=4, trControl = rf_ctrl2, importance = TRUE)
model4 <- predict(s2, cFit2,  type="prob", 
                  progress='text', factors=f, index=1:2) 

最新更新