This function is designed to populate a raster with simulated values. To implement the function, a (blank) raster should be supplied. A user-defined function, or list of functions, is evaluated across this raster, or across sub-regions of this raster, to generate a new raster with simulated values.

sim_surface(
  blank,
  n = 1L,
  sim_values,
  mask = NULL,
  mask_inside = FALSE,
  plot = 1:2L
)

Arguments

blank

A raster.

n

An integer that defines the number of (approximately equal area) pieces into which to split blank.

sim_values

A function or, if n > 1L, a list of functions, that, for a given number of cells, simulate new values for those cells.

mask, mask_inside

Arguments required to implement a spatial mask via mask_io.

plot

An integer that defines whether or not to plot a histogram of simulated values (1L), a heat map of the simulated raster (2L) or both (1:2L).

Value

The function returns a raster, with the same properties as blank, with values generated from the sim_values function(s).

Author

Edward Lavender

Examples

#### Example (1): Simulate values across the whole raster
sim_surface(dat_gebco,
  sim_values = function(n) stats::runif(n = n, 0, 1)
)
#> prettyGraphics::pretty_map() CRS taken as: '+proj=longlat +datum=WGS84 +no_defs'.

#> class      : RasterLayer 
#> dimensions : 36, 36, 1296  (nrow, ncol, ncell)
#> resolution : 0.004166667, 0.004166667  (x, y)
#> extent     : -5.545833, -5.395833, 56.34167, 56.49167  (xmin, xmax, ymin, ymax)
#> crs        : +proj=longlat +datum=WGS84 +no_defs 
#> source     : memory
#> names      : layer 
#> values     : 1.444947e-05, 0.9999136  (min, max)
#> 
sim_surface(dat_gebco,
  sim_values = function(n) stats::rnorm(n = n, 0, 1)
)
#> prettyGraphics::pretty_map() CRS taken as: '+proj=longlat +datum=WGS84 +no_defs'.

#> class      : RasterLayer 
#> dimensions : 36, 36, 1296  (nrow, ncol, ncell)
#> resolution : 0.004166667, 0.004166667  (x, y)
#> extent     : -5.545833, -5.395833, 56.34167, 56.49167  (xmin, xmax, ymin, ymax)
#> crs        : +proj=longlat +datum=WGS84 +no_defs 
#> source     : memory
#> names      : layer 
#> values     : -2.774515, 2.830973  (min, max)
#> 

#### Example (2): Simulate values differently across different areas
# .. by defining the number of areas into which to split the raster
# .. and a list of function(s)
sim_surface(dat_gebco,
  n = 2, sim_values = list(
    function(n) stats::runif(n = n, 0, 1),
    function(n) stats::runif(n = n, 10, 11)
  )
)
#> prettyGraphics::pretty_map() CRS taken as: '+proj=longlat +datum=WGS84 +no_defs'.

#> class      : RasterLayer 
#> dimensions : 36, 34, 1224  (nrow, ncol, ncell)
#> resolution : 0.004166667, 0.004166667  (x, y)
#> extent     : -5.541667, -5.4, 56.34167, 56.49167  (xmin, xmax, ymin, ymax)
#> crs        : +proj=longlat +datum=WGS84 +no_defs 
#> source     : memory
#> names      : layer 
#> values     : 0.0009381943, 10.99998  (min, max)
#> 

#### Example (3): Include a spatial mask
sim_surface(dat_gebco,
  n = 2, sim_values = list(
    function(n) stats::runif(n = n, 9, 10),
    function(n) stats::runif(n = n, 10, 11)
  ),
  mask = dat_coast, mask_inside = TRUE
)
#> Warning: GEOS support is provided by the sf and terra packages among others
#> Warning: spgeom1 and spgeom2 have different proj4 strings
#> prettyGraphics::pretty_map() CRS taken as: '+proj=longlat +datum=WGS84 +no_defs'.

#> class      : RasterLayer 
#> dimensions : 36, 34, 1224  (nrow, ncol, ncell)
#> resolution : 0.004166667, 0.004166667  (x, y)
#> extent     : -5.541667, -5.4, 56.34167, 56.49167  (xmin, xmax, ymin, ymax)
#> crs        : +proj=longlat +datum=WGS84 +no_defs 
#> source     : memory
#> names      : layer 
#> values     : 9.001353, 10.99626  (min, max)
#>