我正在尝试使用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
。
我尝试了所有方法,但无济于事,需要在这2个物种之间进行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")
答案 0 :(得分: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)
答案 1 :(得分: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)