Sampling

Currently, there are 9 functions associated with the sample verb in the sgsR package:

Algorithm Description Reference
sample_srs() Simple random
sample_systematic() Systematic
sample_strat() Stratified Queinnec, White, & Coops (2021)
sample_sys_strat() Systematic Stratified
sample_nc() Nearest centroid Melville & Stone (2016)
sample_clhs() Conditioned Latin hypercube Minasny & McBratney (2006)
sample_balanced() Balanced sampling Grafström, A. Lisic, J (2018)
sample_ahels() Adapted hypercube evaluation of a legacy sample Malone, Minasny, & Brungard (2019)
sample_existing() Sub-sampling an existing sample

sample_srs

We have demonstrated a simple example of using the sample_srs() function in vignette("sgsR"). We will demonstrate additional examples below.

raster

The input required for sample_srs() is a raster. This means that sraster and mraster are supported for this function.

#--- perform simple random sampling ---#
sample_srs(
  raster = sraster, # input sraster
  nSamp = 200, # number of desired sample units
  plot = TRUE
) # plot

#> Simple feature collection with 200 features and 0 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431110 ymin: 5337790 xmax: 438530 ymax: 5343210
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>                  geometry
#> 1  POINT (432130 5338050)
#> 2  POINT (437830 5341790)
#> 3  POINT (437310 5339490)
#> 4  POINT (432730 5340570)
#> 5  POINT (431670 5343070)
#> 6  POINT (433450 5340430)
#> 7  POINT (435010 5337910)
#> 8  POINT (431290 5338190)
#> 9  POINT (432350 5342410)
#> 10 POINT (432690 5338050)
sample_srs(
  raster = mraster, # input mraster
  nSamp = 200, # number of desired sample units
  access = access, # define access road network
  mindist = 200, # minimum distance sample units must be apart from one another
  buff_inner = 50, # inner buffer - no sample units within this distance from road
  buff_outer = 200, # outer buffer - no sample units further than this distance from road
  plot = TRUE
) # plot

#> Simple feature collection with 200 features and 0 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431110 ymin: 5337710 xmax: 438490 ymax: 5343230
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>                  geometry
#> 1  POINT (433590 5342070)
#> 2  POINT (434630 5341790)
#> 3  POINT (436490 5341330)
#> 4  POINT (435150 5339590)
#> 5  POINT (437350 5342390)
#> 6  POINT (433550 5341590)
#> 7  POINT (434130 5341610)
#> 8  POINT (434710 5338930)
#> 9  POINT (432230 5341530)
#> 10 POINT (432590 5342970)

sample_systematic

The sample_systematic() function applies systematic sampling across an area with the cellsize parameter defining the resolution of the tessellation. The tessellation shape can be modified using the square parameter. Assigning TRUE (default) to the square parameter results in a regular grid and assigning FALSE results in a hexagonal grid.

The location of sample units can also be adjusted using the locations parameter, where centers takes the center, corners takes all corners, and random takes a random location within each tessellation. Random start points and translations are applied when the function is called.

#--- perform grid sampling ---#
sample_systematic(
  raster = sraster, # input sraster
  cellsize = 1000, # grid distance
  plot = TRUE
) # plot

#> Simple feature collection with 38 features and 0 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431487.6 ymin: 5337830 xmax: 438221.2 ymax: 5343217
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>                    geometry
#> 1  POINT (436900.3 5337882)
#> 2  POINT (437789.5 5338339)
#> 3  POINT (434664.4 5337856)
#> 4  POINT (435553.6 5338313)
#> 5  POINT (438221.2 5339686)
#> 6  POINT (432428.5 5337830)
#> 7  POINT (433317.7 5338287)
#> 8  POINT (434206.9 5338745)
#> 9  POINT (435096.1 5339202)
#> 10 POINT (435985.3 5339660)
#--- perform grid sampling ---#
sample_systematic(
  raster = sraster, # input sraster
  cellsize = 500, # grid distance
  square = FALSE, # hexagonal tessellation
  location = "random", # randomly sample within tessellation
  plot = TRUE
) # plot

#> Simple feature collection with 169 features and 0 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431108.2 ymin: 5337770 xmax: 438521.8 ymax: 5343194
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>                    geometry
#> 1  POINT (438387.1 5343007)
#> 2  POINT (438239.2 5343090)
#> 3  POINT (438214.1 5342496)
#> 4  POINT (438521.8 5340596)
#> 5  POINT (438026.8 5342695)
#> 6  POINT (438094.6 5341920)
#> 7  POINT (438242.8 5341084)
#> 8  POINT (438452.3 5340391)
#> 9  POINT (438516.1 5339555)
#> 10 POINT (437617.6 5342993)
sample_systematic(
  raster = sraster, # input sraster
  cellsize = 500, # grid distance
  access = access, # define access road network
  buff_outer = 200, # outer buffer - no sample units further than this distance from road
  square = FALSE, # hexagonal tessellation
  location = "corners", # take corners instead of centers
  plot = TRUE
)

#> Simple feature collection with 635 features and 0 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431149.7 ymin: 5337713 xmax: 438493.9 ymax: 5343209
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>                    geometry
#> 1  POINT (431886.5 5338179)
#> 2  POINT (431968.4 5337902)
#> 3  POINT (431149.7 5340670)
#> 4  POINT (431231.5 5340394)
#> 5  POINT (431968.4 5337902)
#> 6  POINT (431886.5 5338179)
#> 7  POINT (432003.4 5338665)
#> 8  POINT (431886.5 5338179)
#> 9  POINT (431231.5 5340394)
#> 10 POINT (431149.7 5340670)

sample_strat

The sample_strat() contains two methods to perform sampling:

  • "Queinnec" - Hierarchical sampling using a focal window to isolate contiguous groups of stratum pixels, which was originally developed by Martin Queinnec.

  • "random" - Traditional stratified random sampling. This method ignores much of the functionality of the algorithm to allow users the capability to use standard stratified random sampling approaches without the use of a focal window to locate contiguous stratum cells.

method = "Queinnec"

Queinnec, M., White, J. C., & Coops, N. C. (2021). Comparing airborne and spaceborne photon-counting LiDAR canopy structural estimates across different boreal forest types. Remote Sensing of Environment, 262(August 2020), 112510.

This algorithm uses moving window (wrow and wcol parameters) to filter the input sraster to prioritize sample unit allocation to where stratum pixels are spatially grouped, rather than dispersed individuals across the landscape.

Sampling is performed using 2 rules:

  • Rule 1 - Sample within spatially grouped stratum pixels. Moving window defined by wrow and wcol.

  • Rule 2 - If no additional sample units exist to satisfy desired sample size(nSamp), individual stratum pixels are sampled.

The rule applied to a select each sample unit is defined in the rule attribute of output samples. We give a few examples below:

#--- perform stratified sampling random sampling ---#
sample_strat(
  sraster = sraster, # input sraster
  nSamp = 200
) # desired sample size # plot
#> Simple feature collection with 200 features and 3 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431230 ymin: 5337750 xmax: 438530 ymax: 5343210
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>    strata type  rule               geometry
#> x       1  new rule1 POINT (436250 5342970)
#> x1      1  new rule1 POINT (435830 5339590)
#> x2      1  new rule1 POINT (435650 5342590)
#> x3      1  new rule1 POINT (433830 5341550)
#> x4      1  new rule1 POINT (438230 5339230)
#> x5      1  new rule1 POINT (437750 5341150)
#> x6      1  new rule1 POINT (436290 5342770)
#> x7      1  new rule1 POINT (433550 5341550)
#> x8      1  new rule1 POINT (434290 5341090)
#> x9      1  new rule1 POINT (434230 5341350)

In some cases, users might want to include an existing sample within the algorithm. In order to adjust the total number of sample units needed per stratum to reflect those already present in existing, we can use the intermediate function extract_strata().

This function uses the sraster and existing sample units and extracts the stratum for each. These sample units can be included within sample_strat(), which adjusts total sample units required per class based on representation in existing.

#--- extract strata values to existing samples ---#
e.sr <- extract_strata(
  sraster = sraster, # input sraster
  existing = existing
) # existing samples to add strata value to

TIP!

sample_strat() requires the sraster input to have an attribute named strata and will give an error if it doesn’t.

sample_strat(
  sraster = sraster, # input sraster
  nSamp = 200, # desired sample size
  access = access, # define access road network
  existing = e.sr, # existing sample with strata values
  mindist = 200, # minimum distance sample units must be apart from one another
  buff_inner = 50, # inner buffer - no sample units within this distance from road
  buff_outer = 200, # outer buffer - no sample units further than this distance from road
  plot = TRUE
) # plot

#> Simple feature collection with 400 features and 3 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431130 ymin: 5337730 xmax: 438530 ymax: 5343230
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>    strata     type     rule               geometry
#> 1       1 existing existing POINT (437570 5342770)
#> 2       1 existing existing POINT (433510 5340890)
#> 3       1 existing existing POINT (437630 5338990)
#> 4       1 existing existing POINT (433510 5341070)
#> 5       1 existing existing POINT (437790 5342710)
#> 6       1 existing existing POINT (435450 5342070)
#> 7       1 existing existing POINT (438270 5339550)
#> 8       1 existing existing POINT (435450 5342850)
#> 9       1 existing existing POINT (437110 5338070)
#> 10      1 existing existing POINT (433390 5341410)

The code in the example above defined the mindist parameter, which specifies the minimum euclidean distance that new sample units must be apart from one another.

Notice that the sample units have type and rule attributes which outline whether they are existing or new, and whether rule1 or rule2 were used to select them. If type is existing (a user provided existing sample), rule will be existing as well as seen above.

sample_strat(
  sraster = sraster, # input
  nSamp = 200, # desired sample size
  access = access, # define access road network
  existing = e.sr, # existing samples with strata values
  include = TRUE, # include existing sample in nSamp total
  buff_outer = 200, # outer buffer - no samples further than this distance from road
  plot = TRUE
) # plot

#> Simple feature collection with 200 features and 3 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431130 ymin: 5337730 xmax: 438530 ymax: 5343170
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>    strata     type     rule               geometry
#> 1       1 existing existing POINT (437570 5342770)
#> 2       1 existing existing POINT (433510 5340890)
#> 3       1 existing existing POINT (437630 5338990)
#> 4       1 existing existing POINT (433510 5341070)
#> 5       1 existing existing POINT (437790 5342710)
#> 6       1 existing existing POINT (435450 5342070)
#> 7       1 existing existing POINT (438270 5339550)
#> 8       1 existing existing POINT (435450 5342850)
#> 9       1 existing existing POINT (437110 5338070)
#> 10      1 existing existing POINT (433390 5341410)

The include parameter determines whether existing sample units should be included in the total sample size defined by nSamp. By default, the include parameter is set as FALSE.

method = "random

Stratified random sampling with equal probability for all cells (using default algorithm values for mindist and no use of access functionality). In essence this method perform the sample_srs algorithm for each stratum separately to meet the specified sample size.

#--- perform stratified sampling random sampling ---#
sample_strat(
  sraster = sraster, # input sraster
  method = "random", # stratified random sampling
  nSamp = 200, # desired sample size
  plot = TRUE
) # plot

#> Simple feature collection with 200 features and 2 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431110 ymin: 5337730 xmax: 438530 ymax: 5343210
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>    strata type               geometry
#> x       1  new POINT (438510 5337970)
#> x1      1  new POINT (433190 5341630)
#> x2      1  new POINT (438070 5339550)
#> x3      1  new POINT (434710 5339050)
#> x4      1  new POINT (435470 5342050)
#> x5      1  new POINT (434850 5342830)
#> x6      1  new POINT (435650 5342270)
#> x7      1  new POINT (432670 5342810)
#> x8      1  new POINT (433750 5340830)
#> x9      1  new POINT (435950 5339430)

sample_sys_strat

sample_sys_strat() function implements systematic stratified sampling on an sraster. This function uses the same functionality as sample_systematic() but takes an sraster as input and performs sampling on each stratum iteratively.

#--- perform grid sampling on each stratum separately ---#
sample_sys_strat(
  sraster = sraster, # input sraster with 4 strata
  cellsize = 1000, # grid size
  plot = TRUE # plot output
)
#> Warning: [readStart] source already open for reading
#> Processing strata : 1
#> Warning: [extract] source already open for reading
#> Processing strata : 2
#> Warning: [extract] source already open for reading
#> Processing strata : 3
#> Warning: [extract] source already open for reading
#> Processing strata : 4
#> Warning: [extract] source already open for reading

#> Simple feature collection with 30 features and 1 field
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431134.3 ymin: 5337735 xmax: 438391.3 ymax: 5343003
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>    strata                 geometry
#> 1       1 POINT (437894.8 5340474)
#> 2       1 POINT (437542.2 5339104)
#> 3       1 POINT (435508.5 5342549)
#> 4       1 POINT (437189.5 5337735)
#> 5       1 POINT (433786.3 5341532)
#> 6       2   POINT (431762 5338570)
#> 7       2 POINT (432392.5 5339836)
#> 8       2 POINT (432387.4 5342998)
#> 9       2 POINT (433971.1 5341420)
#> 10      2 POINT (434288.8 5340471)

Just like with sample_systematic() we can specify where we want our samples to fall within our tessellations. We specify location = "corners" below. Note that the tesselations are all saved to a list file when details = TRUE should the user want to save them.

sample_sys_strat(
  sraster = sraster, # input sraster with 4 strata
  cellsize = 500, # grid size
  square = FALSE, # hexagon tessellation
  location = "corners", # samples on tessellation corners
  plot = TRUE # plot output
)
#> Processing strata : 1
#> Warning: [extract] source already open for reading
#> Processing strata : 2
#> Warning: [extract] source already open for reading
#> Processing strata : 3
#> Warning: [extract] source already open for reading
#> Processing strata : 4
#> Warning: [extract] source already open for reading

#> Simple feature collection with 1261 features and 1 field
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431105.8 ymin: 5337705 xmax: 438538.9 ymax: 5343224
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>    strata                 geometry
#> 1       1 POINT (432962.5 5343057)
#> 2       1 POINT (432674.7 5343035)
#> 3       1 POINT (434401.7 5343168)
#> 4       1 POINT (432674.7 5343035)
#> 5       1 POINT (432549.9 5342775)
#> 6       1 POINT (432962.5 5343057)
#> 7       1 POINT (432962.5 5343057)
#> 8       1 POINT (434401.7 5343168)
#> 9       1 POINT (437730.8 5343172)
#> 10      1   POINT (437443 5343150)

This sampling approach could be especially useful incombination with strat_poly() to ensure consistency of sampling accross specific management units.

#--- read polygon coverage ---#
poly <- system.file("extdata", "inventory_polygons.shp", package = "sgsR")
fri <- sf::st_read(poly)
#> Reading layer `inventory_polygons' from data source 
#>   `/tmp/RtmpdRhMcx/Rinst15736361282e/sgsR/extdata/inventory_polygons.shp' 
#>   using driver `ESRI Shapefile'
#> Simple feature collection with 632 features and 3 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 431100 ymin: 5337700 xmax: 438560 ymax: 5343240
#> Projected CRS: UTM_Zone_17_Northern_Hemisphere

#--- stratify polygon coverage ---#
#--- specify polygon attribute to stratify ---#
attribute <- "NUTRIENTS"

#--- specify features within attribute & how they should be grouped ---#
#--- as a single vector ---#
features <- c("poor", "rich", "medium")

#--- get polygon stratification ---#
srasterpoly <- strat_poly(
  poly = fri,
  attribute = attribute,
  features = features,
  raster = sraster
)

#--- systematatic stratified sampling for each stratum ---#
sample_sys_strat(
  sraster = srasterpoly, # input sraster from strat_poly() with 3 strata
  cellsize = 500, # grid size
  square = FALSE, # hexagon tessellation
  location = "random", # randomize plot location
  plot = TRUE # plot output
)
#> Processing strata : 1
#> Warning: [extract] source already open for reading
#> Processing strata : 2
#> Warning: [extract] source already open for reading
#> Processing strata : 3
#> Warning: [extract] source already open for reading
#> Simple feature collection with 163 features and 1 field
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431152.2 ymin: 5337716 xmax: 438498.4 ymax: 5343239
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>    strata                 geometry
#> 1       1 POINT (431277.8 5343080)
#> 2       1 POINT (431366.7 5342305)
#> 3       1 POINT (432046.6 5342837)
#> 4       1 POINT (431991.3 5342485)
#> 5       1 POINT (432630.3 5342978)
#> 6       1 POINT (431795.6 5341897)
#> 7       1 POINT (432005.6 5342145)
#> 8       1 POINT (433789.7 5342817)
#> 9       1 POINT (432751.7 5341912)
#> 10      1 POINT (433339.8 5342370)

sample_nc

sample_nc() function implements the Nearest Centroid sampling algorithm described in Melville & Stone (2016). The algorithm uses kmeans clustering where the number of clusters (centroids) is equal to the desired sample size (nSamp).

Cluster centers are located, which then prompts the nearest neighbour mraster pixel for each cluster to be selected (assuming default k parameter). These nearest neighbours are the output sample units.

#--- perform simple random sampling ---#
sample_nc(
  mraster = mraster, # input
  nSamp = 25, # desired sample size
  plot = TRUE
)
#> K-means being performed on 3 layers with 25 centers.

#> Simple feature collection with 25 features and 4 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431410 ymin: 5337910 xmax: 438490 ymax: 5342970
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>        zq90 pzabove2  zsd kcenter               geometry
#> 4924  14.40     47.3 4.04       1 POINT (432590 5342970)
#> 38086 17.20     88.6 4.28       2 POINT (431890 5341190)
#> 99566  4.67     30.8 1.12       3 POINT (438050 5337910)
#> 15785 15.00     88.7 3.65       4 POINT (433470 5342390)
#> 28781 18.70     86.0 5.11       5 POINT (432290 5341690)
#> 83289 23.80     89.8 6.83       6 POINT (433290 5338770)
#> 89485  7.33     14.2 1.96       7 POINT (437850 5338450)
#> 4966  10.40     64.3 2.73       8 POINT (433430 5342970)
#> 39124  7.62     81.8 1.70       9 POINT (437730 5341150)
#> 41792 15.20     94.5 2.71      10 POINT (431410 5340990)

Altering the k parameter leads to a multiplicative increase in output sample units where total output samples = nSamp * k.

#--- perform simple random sampling ---#
samples <- sample_nc(
  mraster = mraster, # input
  k = 2, # number of nearest neighbours to take for each kmeans center
  nSamp = 25, # desired sample size
  plot = TRUE
)
#> K-means being performed on 3 layers with 25 centers.


#--- total samples = nSamp * k (25 * 2) = 50 ---#
nrow(samples)
#> [1] 50

Visualizing what the kmeans centers and sample units looks like is possible when using details = TRUE. The $kplot output provides a quick visualization of where the centers are based on a scatter plot of the first 2 layers in mraster. Notice that the centers are well distributed in covariate space and chosen sample units are the closest pixels to each center (nearest neighbours).

#--- perform simple random sampling with details ---#
details <- sample_nc(
  mraster = mraster, # input
  nSamp = 25, # desired sample number
  details = TRUE
)
#> K-means being performed on 3 layers with 25 centers.

#--- plot ggplot output ---#

details$kplot

sample_clhs

sample_clhs() function implements conditioned Latin hypercube (clhs) sampling methodology from the clhs package.

TIP!

A number of other functions in the sgsR package help to provide guidance on clhs sampling including calculate_pop() and calculate_lhsOpt(). Check out these functions to better understand how sample numbers could be optimized.

The syntax for this function is similar to others shown above, although parameters like iter, which define the number of iterations within the Metropolis-Hastings process are important to consider. In these examples we use a low iter value for efficiency. Default values for iter within the clhs package are 10,000.

sample_clhs(
  mraster = mraster, # input
  nSamp = 200, # desired sample size
  plot = TRUE, # plot
  iter = 100
) # number of iterations

The cost parameter defines the mraster covariate, which is used to constrain the clhs sampling. An example could be the distance a pixel is from road access (e.g. from calculate_distance() see example below), terrain slope, the output from calculate_coobs(), or many others.

#--- cost constrained examples ---#
#--- calculate distance to access layer for each pixel in mr ---#
mr.c <- calculate_distance(
  raster = mraster, # input
  access = access, # define access road network
  plot = TRUE
) # plot
#> |---------|---------|---------|---------|=========================================                                          

sample_clhs(
  mraster = mr.c, # input
  nSamp = 250, # desired sample size
  iter = 100, # number of iterations
  cost = "dist2access", # cost parameter - name defined in calculate_distance()
  plot = TRUE
) # plot

sample_balanced

The sample_balanced() algorithm performs a balanced sampling methodology from the stratifyR / SamplingBigData packages.

sample_balanced(
  mraster = mraster, # input
  nSamp = 200, # desired sample size
  plot = TRUE
) # plot

#> Simple feature collection with 200 features and 0 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431130 ymin: 5337730 xmax: 438550 ymax: 5343230
#> Projected CRS: +proj=utm +zone=17 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
#> First 10 features:
#>                  geometry
#> 1  POINT (434410 5343230)
#> 2  POINT (434170 5343210)
#> 3  POINT (431790 5343150)
#> 4  POINT (433830 5343110)
#> 5  POINT (435610 5343110)
#> 6  POINT (434630 5343090)
#> 7  POINT (437590 5343070)
#> 8  POINT (433170 5343030)
#> 9  POINT (432170 5343010)
#> 10 POINT (436910 5342990)
sample_balanced(
  mraster = mraster, # input
  nSamp = 100, # desired sample size
  algorithm = "lcube", # algorithm type
  access = access, # define access road network
  buff_inner = 50, # inner buffer - no sample units within this distance from road
  buff_outer = 200
) # outer buffer - no sample units further than this distance from road
#> Simple feature collection with 100 features and 0 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431390 ymin: 5337810 xmax: 438550 ymax: 5343210
#> Projected CRS: +proj=utm +zone=17 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
#> First 10 features:
#>                  geometry
#> 1  POINT (434270 5343210)
#> 2  POINT (434050 5343190)
#> 3  POINT (435830 5343150)
#> 4  POINT (434010 5343010)
#> 5  POINT (431970 5342970)
#> 6  POINT (434590 5342970)
#> 7  POINT (436370 5342950)
#> 8  POINT (432270 5342930)
#> 9  POINT (434810 5342890)
#> 10 POINT (437850 5342870)

sample_ahels

The sample_ahels() function performs the adapted Hypercube Evaluation of a Legacy Sample (ahels) algorithm usingexisting sample data and an mraster. New sample units are allocated based on quantile ratios between the existing sample and mraster covariate dataset.

This algorithm was adapted from that presented in the paper below, which we highly recommend.

Malone BP, Minansy B, Brungard C. 2019. Some methods to improve the utility of conditioned Latin hypercube sampling. PeerJ 7:e6451 DOI 10.7717/peerj.6451

This algorithm:

  1. Determines the quantile distributions of existing sample units and mraster covariates.

  2. Determines quantiles where there is a disparity between sample units and covariates.

  3. Prioritizes sampling within those quantile to improve representation.

To use this function, user must first specify the number of quantiles (nQuant) followed by either the nSamp (total number of desired sample units to be added) or the threshold (sampling ratio vs. covariate coverage ratio for quantiles - default is 0.9) parameters.

#--- remove `type` variable from existing  - causes plotting issues ---#

existing <- existing %>% select(-type)

sample_ahels(
  mraster = mraster,
  existing = existing, # existing sample
  plot = TRUE
) # plot
#> Simple feature collection with 360 features and 7 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431110 ymin: 5337730 xmax: 438530 ymax: 5343170
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>      type.x zq90 pzabove2  zsd strata type.y  rule               geometry
#> 1  existing 3.41     25.5 0.82      1    new rule1 POINT (437570 5342770)
#> 2  existing 3.05     17.9 0.56      1    new rule1 POINT (433510 5340890)
#> 3  existing 2.43      9.1 0.35      1    new rule1 POINT (437630 5338990)
#> 4  existing 7.06      9.1 1.85      1    new rule1 POINT (433510 5341070)
#> 5  existing 2.15      1.8 0.26      1    new rule1 POINT (437790 5342710)
#> 6  existing 7.21     40.4 1.87      1    new rule1 POINT (435450 5342070)
#> 7  existing 7.47     35.5 1.97      1    new rule1 POINT (438270 5339550)
#> 8  existing 5.15     17.6 1.24      1    new rule1 POINT (435450 5342850)
#> 9  existing 7.08     78.7 1.52      1    new rule1 POINT (437110 5338070)
#> 10 existing 9.58     35.9 2.62      1    new rule1 POINT (433390 5341410)

TIP!

Notice that no threshold, nSamp, or nQuant were defined. That is because the default setting for threshold = 0.9 and nQuant = 10.

The first matrix output shows the quantile ratios between the sample and the covariates. A value of 1.0 indicates that the sample is representative of quantile coverage. Values > 1.0 indicate over representation of sample units, while < 1.0 indicate under representation.

sample_ahels(
  mraster = mraster,
  existing = existing, # existing sample
  nQuant = 20, # define 20 quantiles
  nSamp = 300
) # desired sample size
#> Simple feature collection with 500 features and 7 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431110 ymin: 5337710 xmax: 438530 ymax: 5343190
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>      type.x zq90 pzabove2  zsd strata type.y  rule               geometry
#> 1  existing 3.41     25.5 0.82      1    new rule1 POINT (437570 5342770)
#> 2  existing 3.05     17.9 0.56      1    new rule1 POINT (433510 5340890)
#> 3  existing 2.43      9.1 0.35      1    new rule1 POINT (437630 5338990)
#> 4  existing 7.06      9.1 1.85      1    new rule1 POINT (433510 5341070)
#> 5  existing 2.15      1.8 0.26      1    new rule1 POINT (437790 5342710)
#> 6  existing 7.21     40.4 1.87      1    new rule1 POINT (435450 5342070)
#> 7  existing 7.47     35.5 1.97      1    new rule1 POINT (438270 5339550)
#> 8  existing 5.15     17.6 1.24      1    new rule1 POINT (435450 5342850)
#> 9  existing 7.08     78.7 1.52      1    new rule1 POINT (437110 5338070)
#> 10 existing 9.58     35.9 2.62      1    new rule1 POINT (433390 5341410)

Notice that the total number of samples is 500. This value is the sum of existing units (200) and number of sample units defined by nSamp = 300.

sample_existing

Acknowledging that existing sample networks are common is important. There is significant investment into these samples, and in order to keep inventories up-to-date, we often need to collect new data for sample units. The sample_existing algorithm provides the user with methods for sub-sampling an existing sample network should the financial / logistical resources not be available to collect data at all sample units. The functions allows users to choose between algorithm types using (type = "clhs" - default, type = "balanced", type = "srs", type = "strat"). Differences in type result in calling internal sample_existing_*() functions (sample_existing_clhs() (default), sample_existing_balanced(), sample_existing_srs(), sample_existing_strat()). These functions are not exported to be used stand-alone, however they employ the same functionality as their sample_clhs() etc counterparts.

While using sample_existing(), should the user wish to specify algorithm specific parameters (e.g. algorithm = "lcube" in sample_balanced() or allocation = "equal" in sample_strat()), they can specify within sample_existing() as if calling the function directly.

I give applied examples for all methods below that are based on the following scenario:

  • We have a systematic sample where sample units are 200m apart.

  • We know we only have resources to sample 300 of them.

  • We have some ALS data available (mraster), which we can use to improve knowledge of the metric populations.

See our existing sample for the scenario below.

#--- generate existing samples and extract metrics ---#
existing <- sample_systematic(raster = mraster, cellsize = 200, plot = TRUE)


#--- sub sample using ---#
e <- existing %>%
  extract_metrics(mraster = mraster, existing = .)

sample_existing(type = "clhs")

The algorithm is unique in that it has two fundamental approaches:

  1. Sample exclusively using existing and the attributes it contains.
#--- sub sample using ---#
sample_existing(existing = e, nSamp = 300, type = "clhs")
#> Simple feature collection with 300 features and 3 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431127.7 ymin: 5337731 xmax: 438522.7 ymax: 5343201
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>      zq90 pzabove2  zsd                 geometry
#> 624 18.10     89.1 6.05 POINT (435306.4 5341335)
#> 826 12.90     81.7 3.29 POINT (438163.9 5341522)
#> 68  11.40     66.5 3.04 POINT (434574.5 5337759)
#> 310 10.00     57.4 2.62 POINT (435593.8 5339296)
#> 517 11.00      4.5 3.42 POINT (435277.8 5340703)
#> 505 10.70     54.4 2.84 POINT (432664.3 5341708)
#> 218 15.60     93.4 4.04 POINT (437618.6 5337874)
#> 837  4.02     33.5 0.80 POINT (435995.5 5342570)
#> 238  6.47     17.0 1.68 POINT (435076.9 5339066)
#> 220 20.10     96.0 6.38   POINT (437992 5337731)
  1. Sub-sampling using raster distributions

Our systematic sample of ~900 plots is fairly comprehensive, however we can generate a true population distribution through the inclusion of the ALS metrics in the sampling process. The metrics will be included in internal latin hypercube sampling to help guide sub-sampling of existing.

#--- sub sample using ---#
sample_existing(
  existing = existing, # our existing sample
  nSamp = 300, # desired sample size
  raster = mraster, # include mraster metrics to guide sampling of existing
  plot = TRUE
) # plot
#> Simple feature collection with 300 features and 3 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431113.9 ymin: 5337716 xmax: 438551.4 ymax: 5343230
#> Projected CRS: +proj=utm +zone=17 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
#> First 10 features:
#>        zq90 pzabove2  zsd                 geometry
#> 91798 18.50     84.9 5.72 POINT (438408.1 5339928)
#> 91759 13.00     78.6 3.83   POINT (437963 5339885)
#> 91622 14.90     81.9 3.53 POINT (432147.4 5341478)
#> 91753 17.10     86.2 4.26 POINT (436656.3 5340387)
#> 91578  3.07      9.0 0.52 POINT (437977.5 5338808)
#> 91986 15.90     90.5 3.30   POINT (436412 5341981)
#> 91293 12.80     49.8 3.64 POINT (434904.7 5338061)
#> 91526 16.30     90.4 3.46 POINT (433238.8 5340415)
#> 91428 16.50     90.8 4.23 POINT (434143.5 5339425)
#> 91977 11.90     64.1 3.31 POINT (434545.2 5342699)

The sample distribution again mimics the population distribution quite well! Now lets try using a cost variable to constrain the sub-sample.

#--- create distance from roads metric ---#
dist <- calculate_distance(raster = mraster, access = access)
#> |---------|---------|---------|---------|=========================================                                          
#--- sub sample using ---#
sample_existing(
  existing = existing, # our existing sample
  nSamp = 300, # desired sample size
  raster = dist, # include mraster metrics to guide sampling of existing
  cost = 4, # either provide the index (band number) or the name of the cost layer
  plot = TRUE
) # plot
#> Simple feature collection with 300 features and 4 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431127.7 ymin: 5337716 xmax: 438523 ymax: 5343230
#> Projected CRS: +proj=utm +zone=17 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
#> First 10 features:
#>        zq90 pzabove2  zsd dist2access                 geometry
#> 91493 12.70     84.9 2.73   462.34228   POINT (433167 5340229)
#> 91781  3.52     27.0 0.67    34.10512 POINT (434674.6 5341363)
#> 92020 26.20     90.3 8.16    44.12010 POINT (437977.2 5341593)
#> 91789 18.20     72.8 3.61   260.31160 POINT (436541.4 5340645)
#> 91699 16.60     92.7 3.87    80.57519 POINT (432477.6 5341779)
#> 91349 23.10     95.9 4.89    92.21993 POINT (436541.7 5337860)
#> 91507 12.30     85.6 3.32    60.25314 POINT (435967.2 5339152)
#> 91440  8.45     52.1 2.02    31.96237 POINT (437130.4 5338276)
#> 91762 15.90     92.5 3.90   378.10387   POINT (438523 5339669)
#> 91899 16.40     73.4 3.33    48.68666 POINT (436383.3 5341349)

Finally, should the user wish to further constrain the sample based on access like other sampling approaches in sgsR that is also possible.

#--- ensure access and existing are in the same CRS ---#

sf::st_crs(existing) <- sf::st_crs(access)

#--- sub sample using ---#
sample_existing(
  existing = existing, # our existing sample
  nSamp = 300, # desired sample size
  raster = dist, # include mraster metrics to guide sampling of existing
  cost = 4, # either provide the index (band number) or the name of the cost layer
  access = access, # roads layer
  buff_inner = 50, # inner buffer - no sample units within this distance from road
  buff_outer = 300, # outer buffer - no sample units further than this distance from road
  plot = TRUE
) # plot
#> Simple feature collection with 300 features and 4 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431127.7 ymin: 5337716 xmax: 438537.5 ymax: 5343230
#> Projected CRS: +proj=utm +zone=17 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
#> First 10 features:
#>        zq90 pzabove2  zsd dist2access                 geometry
#> 91319  5.98     50.0 1.44    73.00914 POINT (431415.1 5340688)
#> 91539  4.80     41.1 1.27    71.19559 POINT (434631.5 5341808)
#> 91653 13.90     91.3 2.82   165.10225 POINT (437632.5 5342369)
#> 91504 19.00     92.0 3.59   287.19337 POINT (435981.3 5340861)
#> 91467 21.30     97.2 6.49    72.76406 POINT (436211.1 5340344)
#> 91477  6.31     12.5 1.65   176.07778 POINT (433109.4 5341751)
#> 91592 18.10     83.5 4.60    76.81471 POINT (436713.6 5341651)
#> 91232 15.30     93.5 3.20   122.48509 POINT (435278.1 5337917)
#> 91414  3.46     16.6 1.53    59.06977 POINT (433267.4 5341047)
#> 91305 16.50     90.8 4.23   184.24413 POINT (434143.5 5339425)

TIP!

The greater constraints we add to sampling, the less likely we will have strong correlations between the population and sample, so its always important to understand these limitations and plan accordingly.

sample_existing(type = "balanced")

When type = "balanced" users can define all parameters that are found within sample_balanced(). This means that one can change the algorithm, p etc.

sample_existing(existing = e, nSamp = 300, type = "balanced")
#> Simple feature collection with 300 features and 3 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431128 ymin: 5337701 xmax: 438537.5 ymax: 5343201
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>    zq90 pzabove2  zsd                 geometry
#> 1  20.3     91.5 5.56   POINT (431157 5337787)
#> 3  20.6     95.7 5.96 POINT (431228.8 5337974)
#> 6  15.0     95.8 2.87 POINT (431788.8 5337759)
#> 10 18.2     89.9 5.34 POINT (431673.9 5338017)
#> 19 21.1     88.5 6.60 POINT (431932.4 5338132)
#> 20 13.1     84.4 3.60   POINT (432119 5338060)
#> 25 29.0     92.3 8.92 POINT (433052.4 5337701)
#> 26 19.7     96.5 3.65 POINT (431257.4 5338606)
#> 27 17.5     94.0 3.27 POINT (431444.1 5338534)
#> 29 18.8     43.4 5.13 POINT (431817.5 5338391)
sample_existing(existing = e, nSamp = 300, type = "balanced", algorithm = "lcube")
#> Simple feature collection with 300 features and 3 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431171.2 ymin: 5337730 xmax: 438508.9 ymax: 5343216
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>    zq90 pzabove2  zsd                 geometry
#> 3  20.6     95.7 5.96 POINT (431228.8 5337974)
#> 5  13.2     94.2 2.99 POINT (431602.1 5337831)
#> 9  15.8     90.9 4.00 POINT (431487.2 5338089)
#> 10 18.2     89.9 5.34 POINT (431673.9 5338017)
#> 14 14.1     77.1 3.99 POINT (432420.6 5337730)
#> 17 17.0     88.0 4.03   POINT (431559 5338276)
#> 18 19.7     90.2 6.39 POINT (431745.7 5338204)
#> 21 14.9     97.6 2.96 POINT (432305.7 5337989)
#> 24 27.1     95.1 7.11 POINT (432865.7 5337773)
#> 28 18.4     94.0 3.25 POINT (431630.8 5338462)

sample_existing(type = "srs")

The simplest, type = srs, randomly selects sample units.

sample_existing(existing = e, nSamp = 300, type = "srs")
#> Simple feature collection with 300 features and 3 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431128 ymin: 5337701 xmax: 438551.7 ymax: 5343230
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>     zq90 pzabove2  zsd                 geometry
#> 1  13.50     58.5 3.83 POINT (435148.7 5339252)
#> 2   6.39      8.7 1.55 POINT (434445.1 5339094)
#> 3   8.56     56.9 2.22 POINT (435981.6 5338075)
#> 4  17.20     86.1 3.98 POINT (432319.9 5339697)
#> 5  17.00     95.2 4.01 POINT (431759.9 5339913)
#> 6  11.70     96.7 2.45 POINT (436240.1 5338190)
#> 7   5.14     11.8 1.17 POINT (438551.7 5340301)
#> 8  17.40     89.1 4.81 POINT (433454.4 5338190)
#> 9  16.10     86.4 4.31 POINT (432822.3 5341004)
#> 10 11.90     94.4 2.70 POINT (432003.8 5341104)

sample_existing(type = "strat")

When type = "strat", existing must have an attribute named strata (just like how sample_strat() requires a strata layer). If it doesnt exist you will get an error. Lets define an sraster so that we are compliant.

sraster <- strat_kmeans(mraster = mraster, nStrata = 4)

e_strata <- extract_strata(sraster = sraster, existing = e)

When we do have a strata attribute, the function works very much the same as sample_strat() in that is allows the user to define the allocation method ("prop" - defaults, "optim", "manual", "equal").

#--- proportional stratified sampling of existing ---#
sample_existing(existing = e_strata, nSamp = 300, type = "strat", allocation = "prop")
#> Simple feature collection with 299 features and 4 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431113.9 ymin: 5337716 xmax: 438522.7 ymax: 5343230
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>    strata zq90 pzabove2  zsd                 geometry
#> 1       4 17.4     72.2 4.99 POINT (433353.7 5340157)
#> 2       4 21.7     80.1 4.38 POINT (436728.1 5340574)
#> 3       4 21.2     70.4 6.34 POINT (437474.8 5340287)
#> 4       4 19.7     88.5 5.17 POINT (438235.9 5338923)
#> 5       4 18.2     89.9 5.34 POINT (431673.9 5338017)
#> 6       4 20.4     93.3 4.69 POINT (433511.7 5339453)
#> 7       4 19.4     88.5 4.79 POINT (434976.5 5338247)
#> 8       4 22.2     87.2 5.86 POINT (436857.1 5342024)
#> 9       4 16.6     82.9 5.20 POINT (435679.8 5341191)
#> 10      4 21.1     94.3 4.48 POINT (434990.7 5339956)

TIP!

Remember that when allocation = "equal", the nSamp value will be allocated for each strata.

We get 400 sample units in our output below because we have 4 strata and nSamp = 100.

#--- equal stratified sampling of existing ---#
sample_existing(existing = e_strata, nSamp = 100, type = "strat", allocation = "equal")
#> Simple feature collection with 400 features and 4 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431128 ymin: 5337702 xmax: 438551.7 ymax: 5343215
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>    strata zq90 pzabove2  zsd                 geometry
#> 1       4 21.1     88.5 6.60 POINT (431932.4 5338132)
#> 2       4 18.5     84.9 5.72 POINT (438408.1 5339928)
#> 3       4 19.9     96.3 4.82 POINT (431616.3 5339539)
#> 4       4 21.7     80.1 4.38 POINT (436728.1 5340574)
#> 5       4 19.4     96.1 4.84   POINT (431645 5340171)
#> 6       4 21.2     81.1 6.45 POINT (438120.7 5341967)
#> 7       4 20.2     95.0 5.73 POINT (431788.5 5340544)
#> 8       4 26.1     90.2 6.12 POINT (435651.1 5340559)
#> 9       4 21.6     78.1 6.77   POINT (431774 5341621)
#> 10      4 26.3     88.8 8.52 POINT (434071.8 5339238)
#--- manual stratified sampling of existing with user defined weights ---#
s <- sample_existing(existing = e_strata, nSamp = 100, type = "strat", allocation = "manual", weights = c(0.2, 0.6, 0.1, 0.1))

We can check the proportion of samples from each strata with:

#--- check proportions match weights ---#
table(s$strata) / 100
#> 
#>   1   2   3   4 
#> 0.2 0.6 0.1 0.1

Finally, type = "optim allows for the user to define a raster metric to be used to optimize within strata variances.

#--- manual stratified sampling of existing with user defined weights ---#
sample_existing(existing = e_strata, nSamp = 100, type = "strat", allocation = "optim", raster = mraster, metric = "zq90")
#> Simple feature collection with 100 features and 4 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 431128 ymin: 5337787 xmax: 438551.7 ymax: 5343143
#> Projected CRS: UTM Zone 17, Northern Hemisphere
#> First 10 features:
#>    strata zq90 pzabove2  zsd                 geometry
#> 1       4 22.0     90.1 5.49 POINT (437043.8 5341952)
#> 2       4 18.4     86.4 6.01 POINT (438221.5 5339999)
#> 3       4 26.1     90.2 6.12 POINT (435651.1 5340559)
#> 4       4 21.0     57.7 6.87 POINT (436124.9 5341234)
#> 5       4 22.2     94.1 7.23 POINT (433483.1 5338822)
#> 6       4 19.4     96.1 4.84   POINT (431645 5340171)
#> 7       4 20.4     93.3 4.69 POINT (433511.7 5339453)
#> 8       4 22.4     91.2 4.78 POINT (432635.6 5341076)
#> 9       4 28.3     92.6 8.71 POINT (434789.5 5341105)
#> 10      4 20.3     91.5 5.56   POINT (431157 5337787)

We see from the output that we get 300 sample units that are a sub-sample of existing. The plotted output shows cumulative frequency distributions of the population (all existing samples) and the sub-sample (the 300 samples we requested).