我正在尝试创建一个森林库存图网络来校准激光雷达数据集以预测森林生物特征。为了确保绘图网络代表激光雷达覆盖范围中存在的树高的完整分布,我使用“clhs”包中的
clhs()
函数来使用条件拉丁超立方采样方法。我使用来自我创建的树冠高度模型栅格的大量随机像素样本作为样本空间,该样本空间将被划分为条件拉丁超立方采样的边缘层。按照包文档中的说明,我使用“sf”包将采样像素转换为点向量。但是,当我运行 clhs::clhs()
例程时,出现错误:
Error: Not a matrix.
我对此感到困惑,因为
clhs()
文档特别提到“sf”点是接受的输入。如果我使用 as.matrix()
将数据强制转换为矩阵,则会收到一条错误消息,指出不接受矩阵作为输入。我还通过接受模拟退火算法参数化的所有默认值,将其与主要输入(clhs()
中的参数 x)隔离。这可能是一个错误吗?
以下是可重现的示例:
##loading necessary packages##
library(clhs)
library(terra)
library(sf)
##For reproducibility##
set.seed(78)
##Creating fake raster of tree heights##
fake_rast<-rast(xmin=-123.5, xmax=123.0, ymin=44.5, ymax=45.0, nrow=200, ncol=200, crs="EPSG:4326")
values(fake_rast)<-rnorm(40000, 35, 10)
##Sampling the raster and converting to sf##
smp<-spatSample(fake_rast, 500, as.points=TRUE, na.rm=TRUE, replace=FALSE) %>% st_as_sf()
##Conditioned Latin Hypercube##
#Accepting all Metropolis-Hastings Defaults and Simulated Annealing Defaults##
clhs.test<-clhs(x=smp, size=n)
#Setting all Metropolis-Hastings Defaults and Simulated Annealing Defaults##
n<-50
mi<-NULL
temp<-100
lc<-10
ol<-0.01
td<-0.9
clhs.test<-clhs(x=smp, size=n, temp=temp, iter=lc*log(ol/temp)/log(td),
obj.limit=ol, tdecrease=td, length.cycle=lc, must.include=mi,
use.cpp=TRUE, simple=FALSE)
事实证明,这是任何错误消息中最简单的,并且如果我逻辑地思考此方法的 HYPERCUBE 部分,我就会意识到我需要多个维度来运行 CLHS。相反,我只有一个维度,它适合阻塞而不是拉丁超立方体。添加额外的假尺寸解决了该错误。以下是可重现的解决方案:
##loading necessary packages##
library(clhs)
library(terra)
library(sf)
##For reproducibility##
set.seed(78)
##Creating fake raster of tree heights##
blank<-rast(xmin=-123.5, xmax=123.0, ymin=44.5, ymax=45.0, nrow=200, ncol=200, crs="EPSG:4326")
fake_rast1<-blank
fake_rast2<-blank
fake_rast3<-blank
values(fake_rast1)<-rnorm(40000, 35, 10)
values(fake_rast2)<-rnorm(40000, 25, 5)
values(fake_rast3)<-rnorm(40000, 15, 2)
fake_rast<-c(fake_rast1, fake_rast2, fake_rast3)
##Sampling the raster and converting to sf##
smp<-spatSample(fake_rast, 500, as.points=TRUE, na.rm=TRUE, replace=FALSE) %>% st_as_sf()
#Setting all Metropolis-Hastings Defaults and Simulated Annealing Defaults##
n<-50
mi<-NULL
temp<-100
lc<-10
ol<-0.01
td<-0.9
clhs.test<-clhs(x=smp, size=n, temp=temp, iter=lc*log(ol/temp)/log(td),
obj.limit=ol, tdecrease=td, length.cycle=lc, must.include=mi,
use.cpp=TRUE, simple=FALSE)