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

问题描述 投票:0回答:2

我正在尝试使用 Kcross 进行空间点模式分析。我希望比较我名为“鸟类”的数据集中的物种 A 和物种 B。感兴趣的变量是

species_name
。它只有 2 个级别 -
species A
species 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")
r spatial point spatstat r-ppp
2个回答
2
投票


您不需要加载大量库(仍然使用 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)


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)
© www.soinside.com 2019 - 2024. All rights reserved.