【问题标题】:How to fix the problem the error number of items to replace is not a multiple of replacement length in R?如何解决R中要替换的错误数量不是替换长度的倍数的问题?
【发布时间】:2021-03-25 10:32:57
【问题描述】:

我正在尝试使用 GAM 和栅格进行空间预测。我有一个深度数据的栅格文件 (Depth_R.asc),以及来自 248 个位置的物种的存在和不存在数据 (Depth.csv)。首先我拟合了这 248 个数据,然后使用 raster:predict 函数来预测物种在整个研究区域深度数据 (Depth_R.asc) 上的出现概率。但是,当我想这样做时,会出现以下错误: p[-naind, ]

所有数据都可在:https://drive.google.com/drive/folders/15uRsWqgG0tcOlfuU9WSF-vTG6gJkuhLd?usp=sharing

我的代码是:

library(raster)
library(mgcv)
    
Data = read.csv("Depth.csv")
model <- gam(PA ~s(Depth), method="REML",family = binomial("logit"), data=Data)

X = raster("Depth_R.asc", level = 1) 
p <- raster::predict(X, model, type="response")

如何解决这个问题?

【问题讨论】:

    标签: r raster predict gam logits


    【解决方案1】:

    如果你看这里:Getting Warning: " 'newdata' had 1 row but variables found have 32 rows" on predict.lm

    您发现rasterdata 之间的名称不匹配。

    例如:

    class      : RasterLayer 
    dimensions : 1276, 686, 875336  (nrow, ncol, ncell)
    resolution : 0.005, 0.005  (x, y)
    extent     : 88.9975, 92.4275, 17.9975, 24.3775  (xmin, xmax, ymin, ymax)
    crs        : NA 
    source     : E:/dissertation/dissertation/Depth_R.asc 
    names      : Depth_R 
    
    #AND
    > names(Data)
    [1] "PA"    "Depth"
    
    1. 如您所见,X 的名称为 Depth_RData 的名称为 Depth

    您需要做的就是:

    names(X) <- "Depth"
    
    model <- gam(PA ~s(Depth), method="REML",family = binomial("logit"), data=Data)
    
    p <- raster::predict(X, t, type="response")
    
    > p
    class      : RasterLayer 
    dimensions : 1276, 686, 875336  (nrow, ncol, ncell)
    resolution : 0.005, 0.005  (x, y)
    extent     : 88.9975, 92.4275, 17.9975, 24.3775  (xmin, xmax, ymin, ymax)
    crs        : NA 
    source     : memory
    names      : layer 
    values     : 2.220446e-16, 0.9585746  (min, max)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-05
      相关资源
      最近更新 更多