2

I'm trying to measure the distance that nestlings moved from their original nest sites to territories they established the next year. I'd like to do this using the spatstat package, but I'm having trouble with the output with some of its functions (e.g., nncross, nndist, crossdist). Here's what my code looks like, and the data is here: https://sites.google.com/site/datastackoverflow/shapefiles

library(raster)
library(spatstat)
library(maptools)

# read in shapefile with nest locations (UTMs) and convert to ppp format:
nests <- readShapeSpatial("nest_locs.shp")
X<-as.ppp(nests)
X

# read in shapefile with juvenile locations (UTMs) and convert to ppp format:
juvs <- readShapeSpatial("juv_locs.shp")
Y<-as.ppp(juvs)
Y

# calculate the distance between nest points and juveniles:
N<-nncross(X,Y)
N

# another option
crossdist(X, Y)

The results look something like this:

> crossdist(X, Y)
     [,1]     [,2]
[1,] 2756.546 1994.002
[2,] 3831.429 3466.360

Is it possible to retain the point ID's, in this case the nestID and corresponding birdID's instead of row and column numbers?

Jason
  • 892
  • 1
  • 8
  • 19

3 Answers3

1

The "IDs" are implicit as is common in R: rows are in the same order as X and the columns are ordered as Y. You could do

z <- crossdist(X, Y) 
rownames(z) <- X$nestID 
colnames(z) <- Y$birdID 

You can also use raster::pointDistance for this.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
0

In the case of crossdist you are covered by the answer by @RobertH. For nncross the function finds for each nest (X) the bird (Y) that is closest to this nest. Index of the nearest bird is in the column labelled which in N. You should be able to convert it to a birdID like this (I haven't run the code, so it is untested):

N$which <- Y$birdID[N$which]
Ege Rubak
  • 4,347
  • 1
  • 10
  • 18
0

Just for clarification:

  • crossdist(X,Y) computes the distance from every point of X to every point of Y and returns a matrix d of distances such that d[i,j] is the distance from X[i] to Y[j]. Points of X correspond to rows of d. Points of Y correspond to columns of d.

  • nncross(X,Y) finds for each point X[i] the nearest point in Y, and by default, returns both the distance to this nearest point ($dist[i]) and the serial number of the nearest point ($which[i]). That is, $which[i]=j if the nearest neighbour of X[i] is Y[j]. If the points of Y have other attributes like an ID string, just use the serial number ($which) to extract the relevant entry.

  • nndist(X) and nnwhich(X) operate on a single point pattern X. They find for each point X[i] the nearest neighbour in the same point pattern, say X[j] where j != i. Then nnwhich returns the index j while nndist returns the distance.

Adrian Baddeley
  • 1,956
  • 1
  • 8
  • 7