首页 文章

用于空间点模式分析的Kcross - 在R中

提问于
浏览
-1

我'm trying to use Kcross for spatial point pattern analysis. I wish to compare between species A and species B, from my dataset called '鸟' . 感兴趣的变量是 species_name . 它只有2个级别 - species Aspecies B .

下面是我的代码,我试图将 species_name 设置为多类型对象 .

marks(birds) <- factor("species_name") #where birds is a ppp object
Kcross(birds,i = "species A", j = "species B")

但是,我遇到了错误消息: No points have mark i = species A .

我已经尝试了所有方法,但无济于事,需要在这两个物种之间进行K交叉 . 我是否错误地定义了我的多类型对象或错误地定义了Kcross函数?这样做的正确方法是什么?

有关可重现的示例,请在下面找到与数据集和代码相关的链接:https://drive.google.com/file/d/1uaQu9LTLqnIjiIRQZLNlraRcLe6nsqkr/view?usp=sharing

library(tidyverse)
library(spatstat)
library(sp)
library(sf)
library(rgdal)
birds = read_csv("birds_sample1.csv")

#create 200x200 polygon as window
x_coord <- c(0,0,200,200,0)
y_coord <- c(0,200,200,0,0)
xym <- cbind(x_coord, y_coord)
p <- Polygon(xym)
ps <-Polygons(list(p),1)
sps <- SpatialPolygons(list(ps))
raster_owin <- as(sps, "owin") 
raster_owin

#create ppp object for birds 
birds <- st_as_sf(birds, coords = c("X", "Y"))
birds  <- as(birds , 'Spatial')
birds<- as(birds, "ppp") 
birds<- birds[raster_owin]

#attempt for Kcross, which failed
marks(birds) <- factor("species_name") #where birds is a ppp object
Kcross(birds,i = "species A", j = "species B")

2 回答

  • 0

    由于 birds 现在是一个 ppp 对象,因此属性现在列在 marks 下,因此你必须这样调用它们,

    marks(birds) <- factor(birds$marks$species_name)
    

    然后,您可以在不设置这些参数的情况下调用 KCross ,因为默认情况下已将 i 设置为第一级标记, j 设置为第二级

    levels(marks(birds))
    #[1] "Ordinary Snape"          "Rose-crested Blue Pipit"
    
    Kcross(birds)
    
  • 0

    您不需要加载很多库(仍然使用tidyverse中的 readr )来执行 spatstat . 以下是几行代码:

    library(spatstat)
    W <- owin(c(0,200), c(0,200))
    birds <- readr::read_csv("https://drive.google.com/uc?export=download&id=1uaQu9LTLqnIjiIRQZLNlraRcLe6nsqkr")
    #> Warning: Missing column names filled in: 'X1' [1]
    species <- factor(birds$species_name)
    birds <- ppp(x = birds$X, y = birds$Y, marks = species, window = W)
    #> Warning: data contain duplicated points
    Kbirds <- Kcross(birds, i = "Species A", j = "Species B")
    plot(Kbirds)
    

相关问题