我正在使用
spatialRF
中的包 R
进行空间随机森林回归 (SRFR) 任务。我有一个响应变量和 4 个预测变量,我正在粗略的空间尺度上执行 SRFR。我的目标是采用模型参数并将它们应用于更精细的空间分辨率,以便在更精细的空间尺度上预测响应变量。
当我跑步时
p <- stats::predict(object = model.spatial, #name of the spatialRF model
data = s, # data.frame containing the predictors at the fine spatial scale (without NaN values)
type = "response")$predictions
我收到这个错误:
Error in predict.ranger.forest(forest, data, predict.all, num.trees, type,: Error: One or more independent variables not found in data.
我已经检查了
s
和我原来的data.frame
(我用来在粗尺度上建立模型的那个)的列名,它们是相同的。我如何使用我在粗尺度上创建的模型来预测更精细空间尺度上的响应变量?
代码如下:
library(spatialRF)
library(stats)
wd = "path/"
block.data = read.csv(paste0(wd, "block.data.csv")) # coarse resolution
#names of the response variable and the predictors
dependent.variable.name <- "ntl"
predictor.variable.names <- colnames(block.data)[4:7]
#coordinates of the cases
xy <- block.data[, c("x", "y")]
block.data$x <- NULL
block.data$y <- NULL
#distance matrix
distance.matrix <- as.matrix(dist(block.data))
min(distance.matrix)
max(distance.matrix)
#distance thresholds (same units as distance_matrix)
distance.thresholds <- c(0, 20, 50, 100, 200, 500)
#random seed for reproducibility
random.seed <- 456
#creating and registering the cluster
local.cluster <- parallel::makeCluster(
parallel::detectCores() - 1,
type = "PSOCK")
doParallel::registerDoParallel(cl = local.cluster)
# fitting a non-spatial Random Forest
model.non.spatial <- spatialRF::rf(
data = block.data,
dependent.variable.name = dependent.variable.name,
predictor.variable.names = predictor.variable.names,
distance.matrix = distance.matrix,
distance.thresholds = distance.thresholds,
xy = xy,
seed = random.seed,
verbose = FALSE)
# Fitting a spatial model with rf_spatial()
model.spatial <- spatialRF::rf_spatial(
model = model.non.spatial,
method = "mem.moran.sequential",
verbose = FALSE,
seed = random.seed)
#stopping the cluster
parallel::stopCluster(cl = local.cluster)
# prediction at a finer spatial scale
s = read.csv(paste0(wd, "s.csv")) # df containg the predictors at fine scale
p <- stats::predict(object = model.spatial,
data = s,
type = "response")$predictions
从here你可以下载两个
data.frame
s(我会在创建一个包含20-30像素的较小数据集后立即更新帖子)。