random.sample: Spatially random sample

Description Usage Arguments Value Author(s) References Examples

View source: R/random_sample.r

Description

This function draws a spatially random sample from a discrete set of units located over some defined geographical region or generate completely spatially random points within a polygon.

Usage

1
random.sample(obj = NULL, poly = NULL, type, size, plotit = TRUE)

Arguments

obj

a sf or sp object (with N ≥q \code{size}) where each line corresponds to one spatial location. It should contain values of 2D coordinates, data and, optionally, covariate(s) value(s) at the locations. This argument must be provided when sampling from a "discrete" set of points, see 'type' below for details.

poly

'optional' a sf or sp polygon in which to generate the design. The default is the bounding box of points given by obj. When sampling from a "continuum", the argument 'poly' must be provided.

type

random sampling, a choice of either "discrete", from a set of N potential sampling points or "continuum" from independent, compeletely random points.

size

a non-negative integer giving the total number of locations to be sampled.

plotit

'logical' specifying if graphical output is required. Default is plotit = TRUE.

Value

a sf or sp object of dimension n by p=dim(obj)[2] containing the final sampled locations and any associated values, if sampling from a "discrete" set of points. A matrix of n by 2 containing sampled locations, if sampling from a "continuum".

Author(s)

Michael G. Chipeta mchipeta@mlw.mw

Peter J. Diggle p.diggle@lancaster.ac.uk

References

Rowlingson, B. and Diggle, P. 1993 Splancs: spatial point pattern analysis code in S-Plus. Computers and Geosciences, 19, 627-655

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
# 1. Sampling from a discrete set of points.
library("dplyr")
x <- 0.015+0.03*(1:33)
xall <- rep(x,33)
yall <- c(t(matrix(xall,33,33)))
xy <- cbind(xall,yall)+matrix(-0.0075+0.015*runif(33*33*2),33*33,2)
colnames(xy) <- c('X','Y')

# Convert to SF
xy <- xy %>%
  as.data.frame %>%
  sf::st_as_sf(coords = c(1,2))
xy <- sf::st_as_sf(xy, coords = c('X', 'Y'))


# Sampling from a discrete set.
set.seed(15892)
xy.sample <- random.sample(obj = xy, size = 100, type = "discrete", plotit = TRUE)


# Sampling from a continuum.
library("geoR")
data("parana")
poly <- parana$borders
poly <- matrix(c(poly[,1],poly[,2]),dim(poly)[1],2,byrow=FALSE)
# Convert matrix to polygon
poly <- st_sf(st_sfc(st_polygon(list(as.matrix(poly)))))

set.seed(15892)
xy.sample <- random.sample(poly = poly,size = 100, type = "continuum", plotit = TRUE)

geosample documentation built on May 2, 2019, 6:15 a.m.