0

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")
markalex
  • 8,623
  • 2
  • 7
  • 32
Grace
  • 201
  • 2
  • 13
  • What does `marks(birds)` return? I don't think you are setting it correctly, you shouldn't have quotes around `"species_name"`, it should be a vector the same length as the number of points. Then you call just call `Kcross(birds)` and the default is to set `i` to the first level of marks and `j` to the second level – Esther Jun 24 '18 at 04:02
  • Thanks for your help. `marks(birds)` did not return any output. When I remove the quotes around `"species_name"` and change to `birds$species_name`, the following error appears: `Error in `marks<-.ppp(*tmp*, value = integer(0)) : number of points != number of marks.`. When I change to `species_name`, the message becomes, `Error in factor(species_name) : object 'species_name' not found`. – Grace Jun 24 '18 at 04:39
  • Check the number of points in your ppp object `npoints(birds)` against the length of your mark vector `length(birds$species_name)`. They need to match so that you can correctly set the mark for each point. If you still have an error can you show the output of `str(birds)` or a [reproducible](https://stackoverflow.com/help/mcve) example? – Esther Jun 24 '18 at 04:48
  • I've tried the above but there is no output. Sure, I've edited the above for a reproducible example, hope that provides more information... – Grace Jun 24 '18 at 05:12
  • Thanks, can you also add which libraries you're using? – Esther Jun 24 '18 at 05:19
  • Sure, I've added the libraries above. Thanks! – Grace Jun 24 '18 at 05:22

2 Answers2

2


You don't need to load a lot of libraries (still using readr from the tidyverse) to do this with spatstat. Here are the few lines of code:

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)

Ege Rubak
  • 4,347
  • 1
  • 10
  • 18
0

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)
Esther
  • 1,115
  • 1
  • 10
  • 15