You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用spatialRF::rf函数时distance.matrix报错,如何正确计算?

解决spatialRF::rf函数对角线替换报错及distance.matrix正确计算方法

问题背景

使用spatialRF包执行回归任务时,参考示例预计算distance.matrix并传入spatialRF::rf函数,出现报错:

Error in diag<-(tmp, value = NA): only matrix diagonals can be replaced

自身数据集无x、y空间坐标列,原示例代码及数据集如下:

原示例代码

library(spatialRF)

#加载训练数据
data(block.data)

#响应变量和预测变量名称
dependent.variable.name <- "ntl"
predictor.variable.names <- colnames(block.data)[2:4]

#样本坐标
xy <- block.data[, c("x", "y")]

#距离矩阵
distance.matrix <- dist(subset(block.data, select = -c(x, y)))

#用于复现的随机种子
random.seed <- 1

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 = 0,
  xy = xy,
  seed = random.seed,
  verbose = FALSE)

自身数据集

block.data = structure(list(ntl = c(11.4058170318604, 13.7000455856323, 16.0420398712158, 
17.4475727081299, 26.263370513916, 30.658130645752, 19.8927211761475, 
20.917688369751, 23.7149887084961, 25.2641334533691), pop = c(111.031448364258, 
145.096557617188, 166.351989746094, 193.804962158203, 331.787200927734, 
382.979248046875, 237.971466064453, 276.575958251953, 334.015289306641, 
345.376617431641), tirs = c(35.392936706543, 34.4172630310059, 
33.7765464782715, 35.3224639892578, 40.4262886047363, 39.6619148254395, 
38.6306076049805, 36.752326965332, 37.2010040283203, 36.1100578308105
), agbh = c(1.15364360809326, 0.177780777215958, 0.580717206001282, 
0.647109687328339, 3.84336423873901, 5.6310133934021, 2.10894227027893, 
3.9533429145813, 2.7016019821167, 4.36041164398193), lc = c(40L, 
40L, 40L, 126L, 50L, 50L, 50L, 50L, 40L, 50L)), class = "data.frame", row.names = c(NA, 
-10L))

报错原因

dist()函数返回的是dist类的距离对象,而非标准矩阵格式。spatialRF::rf函数内部会尝试修改距离矩阵的对角线(设为NA),但dist对象不支持这种操作,因此触发报错。此外,自身数据集无x、y坐标,不应传入xy参数。

解决方案

  1. 将dist对象转换为标准矩阵格式,使用as.matrix()函数
  2. 移除xy参数(因无空间坐标)

正确计算distance.matrix的方式

根据需求选择:

  • 特征空间距离:基于数据集的特征变量计算距离,示例:
    # 基于所有非响应变量计算特征距离
    distance.matrix <- as.matrix(dist(subset(block.data, select = -ntl)))
    # 或仅基于预测变量计算
    distance.matrix <- as.matrix(dist(block.data[, predictor.variable.names]))
    
  • 空间距离:若后续补充了空间坐标,可基于x、y计算空间距离:
    # 假设已有xy坐标列
    distance.matrix <- as.matrix(dist(block.data[, c("x", "y")]))
    

修正后的完整代码

library(spatialRF)

# 加载数据集
block.data = structure(list(ntl = c(11.4058170318604, 13.7000455856323, 16.0420398712158, 
17.4475727081299, 26.263370513916, 30.658130645752, 19.8927211761475, 
20.917688369751, 23.7149887084961, 25.2641334533691), pop = c(111.031448364258, 
145.096557617188, 166.351989746094, 193.804962158203, 331.787200927734, 
382.979248046875, 237.971466064453, 276.575958251953, 334.015289306641, 
345.376617431641), tirs = c(35.392936706543, 34.4172630310059, 
33.7765464782715, 35.3224639892578, 40.4262886047363, 39.6619148254395, 
38.6306076049805, 36.752326965332, 37.2010040283203, 36.1100578308105
), agbh = c(1.15364360809326, 0.177780777215958, 0.580717206001282, 
0.647109687328339, 3.84336423873901, 5.6310133934021, 2.10894227027893, 
3.9533429145813, 2.7016019821167, 4.36041164398193), lc = c(40L, 
40L, 40L, 126L, 50L, 50L, 50L, 50L, 40L, 50L)), class = "data.frame", row.names = c(NA, 
-10L))

# 响应变量和预测变量名称
dependent.variable.name <- "ntl"
predictor.variable.names <- colnames(block.data)[2:4]

# 计算特征空间距离矩阵(转成矩阵格式)
distance.matrix <- as.matrix(dist(block.data[, predictor.variable.names]))

# 随机种子
random.seed <- 1

# 训练模型(移除xy参数)
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 = 0,
  seed = random.seed,
  verbose = FALSE
)

# 查看模型结果
summary(model.non.spatial)

内容的提问来源于stack exchange,提问作者Nikos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 09:02:58