栅格预测函数将因子更改为数字并给出错误

Raster predict function changes factor to numeric and gives error

我正在使用 Biomod2 包来 运行 R 中的一系列物种分布模型。我正在使用的建模技术之一是分类树分析 (CTA),它使用 rpart包.

这些模型中的响应是植物物种的 presence/absence,预测变量包含在 rasterStack 中。 rasterStack 中的大多数变量都是连续的数值变量,除了一个土地覆盖变量,地质学,这是一个因素。我堆叠了每个个体 rasterLayer,然后使用 as.factor() 将地质层转换为一个因子。

我 运行试图从 CTA 进行预测时遇到错误消息。 CTA 模型是用一个数据框构建的,其中“地质”是一个因素(见下文),并在 rasterStack(“地质”是一个因素,见下文)上使用栅格 predict 函数。但是,运行 调用 predict 函数时,我收到一条错误消息,提示我提供了数字而不是因子。我已经检查了所有可能的点,看看是否以某种方式将“地质学”转换回数字,但它似乎是一个因素(应该)。

编辑:更改数据以使其可重现。

library(raster)
library(rpart)

set.seed(123)

# Create sample rasterStack
data.rast <- stack(system.file("external/rlogo.grd", package = "raster"))
# Create one layer as a factor 
data.rast$geology <- as.factor(sampleInt(7, length(data.rast$red), replace = TRUE))

# Create sample presence/absence data by randomly selecting cells of raster
data <- as.data.frame(data.rast)
data <- data[sample(nrow(data), 300, replace = FALSE), ]
data$pa <- as.factor(sample(0:1, nrow(data), replace = TRUE))
names(data)[4] <- "geology"

head(data)
#     red green blue geology pa
#2463 251   255  255       7  1
#1944 191   190  186       5  0
#5016 162   174  226       7  0
#5771 255   255  253       4  1
#3739 204   205  199       7  0
#5483 131   133  122       3  0

# Build CTA model using presence/absence dataframe
# Parameters set as the defaults in Biomod2 modeling options
cta <- rpart(pa ~ .,
              data = data,
              na.action = na.omit,
              method = "class",
              control = list(xval = 5, 
                             minbucket = 5, 
                             minsplit = 5,
                             cp = 0.001,
                             maxdepth = 25))

# Confirm classes of data before running predict function
data.frame(ctaClass = attr(terms(cta), "dataClasses")[2:5],
            rasterFactor = is.factor(data.rast))
#        ctaClass rasterFactor
#red      numeric        FALSE
#green    numeric        FALSE
#blue     numeric        FALSE
#geology   factor         TRUE

# Once again confirming this rasterLayer is a factor
levels(data.rast$geology)
#[[1]]
#  ID VALUE
#1  1     1
#2  2     2
#3  3     3
#4  4     4
#5  5     5
#6  6     6
#7  7     7

# Run predict function on rasterStack
cta.predict <- predict(object = data.rast, 
                        model = cta,
                        type = "class")
#Error: variable 'geology' was fitted with type "factor" but type "numeric" was #supplied
#In addition: Warning message:
#In model.frame.default(Terms, newdata, na.action = na.action, xlev = #attr(object,  :
#  variable 'geology' is not a factor

编辑:添加证明它适用于 randomForests 模型

library(randomForest)
rf <- randomForest(pa ~ .,
                    data = data,
                    na.action = na.omit)
rf.predict <- predict(data.rast, rf)        

rf.predict
#class      : RasterLayer 
#dimensions : 77, 101, 7777  (nrow, ncol, ncell)
#resolution : 1, 1  (x, y)
#extent     : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
#crs        : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
#source     : memory
#names      : layer 
#values     : 0, 1  (min, max)
#attributes :
# ID value
#  1     0
#  2     1

在这种情况下,您需要通过提供因子名称和水平来帮助 predict

data$geology <- as.factor(data$geology)
cta.predict <- predict(data.rast, cta, type="class", factors=list(geology=levels(data$geology)))

还要注意 type=class 中的 type=,你不应该只做 class(除非你想让 filename 变成 class.grd

使用 terra 效果会好一些,我认为(希望)

library(terra)
x <- rast(data.rast*1)
x$geology <- as.factor(x$geology)
cta.predict <- predict(x, cta, type="class")