【问题标题】:Species distribution model to produce psuedo-absence data using the randomPoints() function in the Dismo package in R: Error Message使用 R 中 Dismo 包中的 randomPoints() 函数生成伪缺失数据的物种分布模型:错误消息
【发布时间】:2020-09-02 02:38:45
【问题描述】:

目标:

我的目标是使用Dismo包中的randomPoints()函数建立一个物种分布预测模型,最终目标是生成伪缺失点并将它们绘制在地图上。这些点将被转换成一个rastor文件,以便从MODIS文件中提取元数据(即海面盐度、叶绿素水平等),作为重要的生态预测因子,以确定它们如何影响分布蓝鲸。

我们的想法是将存在和伪缺席数据与相关元数据值插入一般线性混合 (GLM),这最终将使我的模型更加平衡和准确。

问题大纲:

我正在尝试按照此species distribution exercise 使用 randomPoints() 函数生成伪缺失点(所需输出:参见图 1)。 但是,在运行我的 R 代码后(参见下面),我遇到了这个 R 错误消息(见下文)。我的 r 代码还生成了一张带有 GPS 点的地图(图 2)。

错误信息:

(函数(类,fdef,mtable)中的错误: 无法为签名“standardGeneric”找到函数“nlayers”的继承方法

我试图找到解决方案。但是,我在使用 R 中的地图方面相对较新,我对这里的问题感到非常困惑!

我的数据框包含 918 行,我想生成与存在点相同数量的伪缺失点。不幸的是,我无法发布我的数据,但我提供了一个迷你数据框作为示例。

如果有人可以帮助我,我将不胜感激!

非常感谢!

R 代码:

 ###Open Packages

    library("sp")
    library("rgdal")
    library("raster")
    library("maptools")
    library("rgdal")
    library("dismo")
    library("spatialEco")
    library("ggplot2")
    library("dplyr")
    library("maps")
    library("ggspatial")
    library("GADMTools")
    library("maps")

    ##Mini Dataframe
    Blue.whale_New <- data.frame(longitude = c(80.5, 80.5, 80.5, 80.5, 80.4, 80.4, 80.5, 80.5, 80.4),
                             latitude = c(5.84, 5.82, 5.85, 5.85, 5.89, 5.82, 5.82, 5.84, 5.83))


    ####World Bioclim Data + GADM Object

    ##Creating an array object with just longitude and latitude decimal coordinates
    ##Upload the maps
    ##Plotting the map of Sri Lanka
    ###GADM OBJECT

    dev.new()
    bioclim1.data <- getData('GADM', country='LKA', level=1)

    #####Worldclim raster layers

    bioclim.data <- getData(name = "worldclim",
                            var = "bio",
                            res = 2.5,
                            path = "./")

    ####Get bounding box of Sri Lanka shape file
    bb=bioclim1.data@bbox

    # Determine geographic extent of our data
    max.lat <- ceiling(max(Blue.whale$latitude))
    min.lat <- floor(min(Blue.whale$latitude))
    max.lon <- ceiling(max(Blue.whale$longitude))
    min.lon <- floor(min(Blue.whale$longitude))
    geographic.extent <- extent(x = c(min.lon, max.lon, min.lat, max.lat))

    #####Plot map
    dev.new()

    plot(bioclim1.data, 
         xlim = c(min(c(min.lon,bb[1,1])), max(c(max.lon,bb[1,2]))),
         ylim = c(min(c(min.lat,bb[2,1])), max(c(max.lat,bb[2,2]))),
         axes = TRUE, 
         col = "grey95")

    # Add the points for individual observation
    points(x = Blue.whale$longitude, 
           y = Blue.whale$latitude, 
           col = "olivedrab", 
           pch = 15, 
           cex = 0.50)

    ###Building a model and visualising results

    ##Crop bioclim data to geographic extent of blue whales GADM Map
    bioclim.data.blue.whale_1<-crop(x=bioclim1.data, y=geographic.extent)

    ##Crop bioclim data to geographic extent of blue whales World Clim
    bioclim.data.blue.whale_2<-crop(x = bioclim.data, y = geographic.extent)

    #Build distribution mode using the World Clim)

    bw.model <- bioclim(x = bioclim.data, p = Blue.whale_New)

    # Predict presence from model
    predict.presence <- dismo::predict(object = bw.model, x = bioclim.data, ext = geographic.extent)

    # Plot base map

    dev.new()

    plot(bioclim1.data, 
         xlim = c(min(c(min.lon,bb[1,1])), max(c(max.lon,bb[1,2]))),
         ylim = c(min(c(min.lat,bb[2,1])), max(c(max.lat,bb[2,2]))),
         axes = TRUE, 
         col = "grey95")

    # Add model probabilities
    plot(predict.presence, add = TRUE)

    # Redraw those country borders
    plot(bioclim1.data, add = TRUE, border = "grey5")

    # Add original observations
    points(Blue.whale_New$longitude, 
           Blue.whale_New$latitude, 
           col = "olivedrab", pch = 20, cex = 0.75)

    ##Psuedo Absence Points

    # Use the bioclim data files for sampling resolution
    bil.files <- list.files(path = "data/wc2-5", 
                            pattern = "*.bil$", 
                            full.names = TRUE)

    # Randomly sample points (same number as our observed points)

    ##Mask = provides resolution of sampling points
    ##n = number of random points
    ##ext = Spatially restricts sampling
    ##extf = expands sampling a little bit

    background <- randomPoints(mask = mask,    
                               n = nrow(Blue.whale_New),     
                               ext = geographic.extent, 
                               extf = 1.25)  

#Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘nlayers’ for signature ‘"standardGeneric"’  

解决问题后我计划运行的代码

Visualize the pseudo-absence points on a map:

# Plot the base map

dev.new()

plot(bioclim1.data, 
     xlim = c(min(c(min.lon,bb[1,1])), max(c(max.lon,bb[1,2]))),
     ylim = c(min(c(min.lat,bb[2,1])), max(c(max.lat,bb[2,2]))),
     axes = TRUE, 
     col = "grey95")

# Add the background points
points(background, col = "grey30", pch = 1, cex = 0.75)

# Add the observations
points(x = Blue.whale_New$longitude, 
       y = Blue.whale_New$latitude, 
       col = "olivedrab", 
       pch = 20, 
       cex = 0.75)

box()

# Arbitrarily assign group 1 as the testing data group
testing.group <- 1

# Create vector of group memberships
group.presence <- kfold(x = Blue.whale_New, k = 5) # kfold is in dismo package

图像 1(所需输出)

图 2:

【问题讨论】:

    标签: r maps latitude-longitude rgdal dismo


    【解决方案1】:

    我认为您的问题是您没有将正确的掩码(例如光栅层)传递给 randomPoints 函数,而是传递了 mask 函数本身,这是一个 standardGeneric em>,因此出现错误消息。

    您可以从要使用的预测栅格生成掩码,然后将其传递给randomPoints 函数

    mask_r <- bioclim1.data[[1]] #just one raterLayer
    mask_r[!is.na(mask_r)] <- 1 #set all non-NA values to 1
    background <- randomPoints(mask = mask_r, #note the proper mask layer    
                               n = nrow(Blue.whale_New),     
                               ext = geographic.extent, 
                               extf = 1.25)
    

    我还没有检查,但是这个解决方案应该可以工作。

    最好的,

    埃米利奥

    【讨论】:

      猜你喜欢
      • 2016-01-22
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-25
      相关资源
      最近更新 更多