getSample: Stratified random sampling

Description Usage Arguments Details Value See Also Examples

View source: R/getSample.R

Description

Performs kmeans clustering to stratify x and randomly samples within the strata until n samples are selected. The number of samples selected in each strata is proportional to the occurrence of those strata across the classified raster.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
getSample(
  x,
  strata = 5,
  layers,
  norm = TRUE,
  n,
  mindist = 0,
  maxIter = 30,
  xy = TRUE,
  filename_cluster = "",
  filename_sample = "",
  ...
)

Arguments

x

A Raster* object used to generate random sample

strata

Number of strata (kmeans clusters). Default is 5.

layers

Vector indicating the bands of x used in stratification (as integer or names). By default, all layers of x are used.

norm

Logical. If TRUE (default), x is normalized before k-means clustering. This is useful if layers have different scales.

n

Sample size

mindist

Minimum distance between samples (in units of x). Default is 0.

maxIter

Numeric. This number is multiplied to the number of samples to select per strata. If the number of iterations to select samples exceeds maxIter x the number of samples to select then the loop will break and a warning message be returned. Default is 30.

xy

Logical indicating if X and Y coordinates of samples should be included in the fields of the returned SpatialPoints object.

filename_cluster

Character. Output filename of the clustered x raster including path to directory and eventually extension

filename_sample

Character. Output filename of the sample points including path to directory. File will be automatically saved as an ESRI Shapefile and any extension in filename_sample will be overwritten

...

Further arguments passed to unsuperClass, writeRaster or writeOGR to control the kmeans algorithm or writing parameters

Details

x is stratified using kmeans clustering from unsuperClass. By default, clustering is performed on a random subset of x (10000 cells) and run with multiple starting configurations in order to find a convergent solution from the multiple starts. The parameters controlling the number of random samples used to perform kmeans clustering and the number of starting configurations can be provided as additional ... arguments. More information on the behavior of the kmeans clustering can be found in unsuperClass. The default kmeans clustering method is Hartigan-Wong algorithm. The algorithm might not converge and output "Quick Transfer" warning. If this is the case, we suggest decreasing strata. Also, if mindist is too large, it might not be possible to select enough samples per strata. In that case, the warning "Exceeded maximum number of runs for strata" is displayed. In that case you can decrease the number of samples n or increase maxIter to control the number of maximum iterations allowed until the required number of samples are selected.

Value

A list with the following objects:

sample

A SpatialPoints object containing sampled points

clusterMap

The clustered x raster, output of unsuperClass

model

The kmeans model, output of unsuperClass

See Also

unsuperClass

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Load raster package
library(raster)

# Open and stack ALS metrics
elev_p95 <- raster(system.file("extdata/examples/ALS_metrics_p95.tif",package="foster"))
cover <- raster(system.file("extdata/examples/ALS_metrics_cov_mean.tif",package="foster"))
Y_vars <- stack(elev_p95,cover)
names(Y_vars) <- c("p95","cover")

# Sample 5 cells in 3 strata (kmeans clusters). Sampled points should be at least 30 m apart.
set.seed(1234) #for example reproducibility
sample_strata <- getSample(Y_vars,
                           n = 5,
                           strata = 3,
                           mindist = 30)

mqueinnec/foster documentation built on March 28, 2021, 4:27 p.m.