rspatialpointspatstatr-ppp

Kcross for spatial point pattern analysis - in R


I'm trying to use Kcross for spatial point pattern analysis. I wish to compare between species A and species B, from my dataset called 'birds'. The variable of interest is species_name. It only has 2 levels - species A and species B.

Below are my codes where I tried to set species_name as a multi-type object.

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

However, I faced the error message: No points have mark i = species A.

I've tried all ways but to no avail, and need to perform a K cross between these 2 species. Did I define my multi-type object wrongly or the Kcross function wrongly? What's the correct way to do so?

For a reproducible example, pls find below link to birds dataset and the codes: 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")

Solution

  • Since birds is now a ppp object, the attributes are now listed under marks so you have to call them as such,

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

    You can then call KCross without setting those arguments, since the default is already to set i to the first level of marks and j to the second level

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