【问题标题】:R: apply regression's model parameters at another spatial scaleR:在另一个空间尺度上应用回归模型参数
【发布时间】:2022-12-03 06:25:45
【问题描述】:

我有三个栅格图层,两个粗分辨率和一个精细分辨率。我使用粗分辨率栅格执行简单的线性回归,然后提取回归系数(斜率和截距),并将它们与我的高分辨率栅格一起使用。到目前为止,我是通过在控制台中绘制系数来手动执行此操作的。有什么方法可以自动执行此操作(以某种方式使用系数而无需打印它们并复制粘贴它们,因为我有 10-20 个光栅来执行此操作)。

到目前为止,这是我正在做的事情:

library(terra)
library(sp)

ntl = rast("path/ntl.tif")
vals_ntl <- as.data.frame(values(ntl))
ntl_coords = as.data.frame(xyFromCell(ntl, 1:ncell(ntl)))
combine <- as.data.frame(cbind(ntl_coords,vals_ntl))

ebbi = rast("path/tirs010.tif")
ebbi <- resample(ebbi, ntl, method="bilinear")
vals_ebbi <- as.data.frame(values(ebbi))

s = c(ntl, ebbi)
names(s) = c('ntl', 'ebbi')

block.data <- as.data.frame(cbind(combine, vals_ebbi))
names(block.data)[3] <- "ntl"
names(block.data)[4] <- "ebbi"

block.data <- na.omit(block.data)

model <- lm(formula = ntl ~ ebbi, data = block.data)

#predict to a raster
summary(model)
model$coefficients # here I plot the coefficients into the console and then manually I use them below

pop = rast("path/pop.tif")
lm_pred010 = 19.0540153 + 0.2797187 * pop
plot(lm_pred010)
res(lm_pred010)

writeRaster(lm_pred010, 
            filename = "path/lm_pred010.tif",
            overwrite = T)

我正在使用的栅格:

ntl = rast(ncols=101, nrows=84, nlyrs=1, xmin=509634.6325, xmax=550034.6325, ymin=161998.158, ymax=195598.158, names=c('ntl'), crs='EPSG:27700')

ebbi = rast(ncols=101, nrows=84, nlyrs=1, xmin=509634.6325, xmax=550034.6325, ymin=161998.158, ymax=195598.158, names=c('focal_sum'), crs='EPSG:27700')

pop = rast(ncols=407, nrows=343, nlyrs=1, xmin=509615.9028, xmax=550315.9028, ymin=161743.6035, ymax=196043.6035, names=c('pop'), crs='EPSG:27700')

【问题讨论】:

    标签: r linear-regression raster coefficients


    【解决方案1】:

    您可以使用 predict 将该拟合模型应用于您想要的任何栅格。您只需要检查从中拟合模型的栅格的名称(波段名称)是否与模型将应用到的栅格的名称相同。

    library(terra)
    lm_pred010 <- predict(pop, model)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-24
      • 2013-07-31
      • 1970-01-01
      • 2022-07-08
      • 1970-01-01
      • 2020-07-13
      相关资源
      最近更新 更多