sample.grid.cell: Function to spatially sample points

Description Usage Arguments Details Value References Examples

Description

Spatially thin eBird records based on a user-defined grid.

Usage

1
2
sample.grid.cell(xxx, yyy, xlim = c(NA, NA), ylim = c(NA, NA), nx, ny,
  jitter = FALSE, size, replace = FALSE)

Arguments

xxx

Vector of longitude or E-W coordinates.

yyy

Vector of latitide or N-S coordinates.

xlim

Together xlim and ylim define a bounding box within which lookup occurs. I.e. all (xxx, yyy) pairs outside of this box are ignored.

ylim

Together xlim and ylim define a bounding box within which lookup occurs. I.e. all (xxx, yyy) pairs outside of this box are ignored.

nx

Number of grid cells in x direction.

ny

Number of grid cells in y direction.

jitter

Defaults to FALSE. Set to TRUE to randomize grid location.

size

Maximum number of points per cell to sample

replace

Defaults to FALSE. Set to TRUE to sample with replacement.

Details

This function generates a stratified sample over a regular grid of strata, i.e. grid cells. It is relatively efficient computationally speaking. The sample() function cannot take a sample larger than the population (in a cell) when 'replace = FALSE' If 'replace = FALSE' and the size parameter is larger than the cell populatuion size this function passes back a vector of length size, but it will contain only as many unique points in the cell and the rest of the entries will be NA's. Note that the arguments in this function are currently named things that already mean something in base R. We may want to change that.

Value

Index vector of selected locations.

References

Team eBird. Daniel Fink.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#Generate Random Points over 2D field 
nnn <- 1000
xxx <- runif(nnn, 0, 10)
yyy <- runif(nnn, 0, 10)
par(cex = 0.5)
plot(xxx, yyy, 
 xlim = c(-1,11), 
 ylim = c(-1,11), 
 pch=20, 
 col="red", 
 cex=0.5)
sgc <- sample.grid.cell(
 xxx, 
 yyy, 
 xlim = c(-1,3), 
 ylim = c(3,6),
 nx = 5, 
 ny = 2, 
 jitter = TRUE, 
 size = 1, 
 replace = FALSE ) 
length(sgc$sample.index)
sum(!is.na(sgc$sample.index))
points(
 xxx[!is.na(sgc$cell.number)], 
 yyy[!is.na(sgc$cell.number)], 
 col = "blue", 
 pch = 20, 
 cex = 0.5)  
points(xxx[sgc$sample.index], yyy[sgc$sample.index], pch=20, cex=1, col="green")

# Reconstruct the Grid that was used
for (iii in 1:(sgc$nx+1))
 lines( rep(sgc$bb[1,1] + (iii-1)*sgc$xwidth, 2), range(sgc$bb[,"yyy"]), col="grey")   
for (iii in 1:(sgc$ny+1))
 lines( range(sgc$bb[,"xxx"]), rep(sgc$bb[1,2] + (iii-1)*sgc$ywidth, 2), , col="grey")   

table(sgc$cell.number)
length(table(sgc$cell.number))
sort(unique(sgc$sample.index))
sort(unique(c(1:length(xxx))[!is.na(sgc$cell.number)]))

eliotmiller/ebirdr documentation built on May 14, 2019, 10:33 a.m.