# Basic Usage of NLMR

#### 2018-04-03

NLMR is a R package designed to generate neutral landscape models (NLMs), simulated landscapes used to explore landscape scale ecological patterns and processes. The NLMR package was designed with a similar philosophy to the Python package NLMpy (see ???), offering a general numeric framework allowing for a high degree of flexibility. Most of the common NLMs, as described by the relevant literature, can be produced using NLMR. Additionally, NLMR allows users to merge multiple landscapes, classify landscape elements categorically and measure basic landscape level metrics. All NLMs produced take the form of two-dimensional raster arrays with specified row and column dimensions and cell values ranging between 0 and 1. By returning raster arrays, NLMs are easily integrated into the workflow of many useful spatial analysis packages, notably the raster package.

## Basic landscape generation

NLMR supplies 16 NLM algorithms. The algorithms differ from each other in spatial auto-correlation, from no auto-correlation (random NLM) to a constant gradient (planar gradients) (see Palmer 1992).

The 16 NLM algorithms are:

3. hierarchical curdling
4. wheyed hierarchical curdling
5. midpoint displacement
6. neighbourhood clustering
8. random
9. random cluster nearest-neighbour
10. random element
11. random mosaic fields
12. random polygonal landscapes
13. random percolation
14. random rectangular cluster
15. spatially correlated random fields (Gaussian random fields)
16. two-dimensional fractional Brownian motion

The basic syntax used to produce a NLM landscape is:

nlm_modeltype(ncol, nrow, resolution, ...)

For example, to produce a simple random neutral landscape one could use the following code:

x <- NLMR::nlm_random(20,20)
rasterVis::levelplot(x, margin = FALSE, par.settings = viridisTheme())

## Merging landscapes

Multiple NLM rasters can be merged or merged together to create new landscape patterns. A single primary or base raster can be merged with any number of additional secondary rasters, with optional scaling factors used to control the influence of the secondary rasters.

The util_merge function is used to merge the rasters as in the example below:

  #Create primary landscape raster
nrow = 100)

rasterVis::levelplot(pL, margin = FALSE, par.settings = viridisTheme())

  #Create secondary landscape rasters
nrow = 100,
origin = c(10, 10, 10, 10))
sL2 <- NLMR::nlm_random(ncol = 100,
nrow = 100)

mL1 <- pL + (sL1 + sL2)

rasterVis::levelplot(mL1, margin = FALSE, par.settings = viridisTheme())

## Classifying categories

Landscape rasters generated by NLMR contain continuous values between 0 and 1, though these can be converted into categorical values using util_classify. The number of classes and the proportion of the NLM raster assigned to each class is determined using a weighting vector. If the weighting vector does not sum to 1, the sum of the weightings is divided by the number of vector elements and these are used to define the class proportions. By default classes are numerical starting from 1. If non-numerical levels are required, level_names can be specified. These classes can be plotted by selecting discrete = TRUE in util_plot. The number of cells contained by each class can be calculated using metric_area.

nr <- NLMR::nlm_randomcluster(ncol = 50,
nrow = 50,
resolution = 1,
neighbourhood = 4,
p = 0.5)
## Loading required namespace: igraph
breaks <- classIntervals(raster::getValues(nr), n=5, style="jenks")
## Warning in classIntervals(raster::getValues(nr), n = 5, style = "jenks"):
## n greater than number of different finite values\nn reset to number of
## different finite values
## Warning in classIntervals(raster::getValues(nr), n = 5, style = "jenks"): n
## same as number of different finite values\neach different finite value is a
## separate class
cr <-  raster::cut(nr, breaks=breaks\$brks, include.lowest=T)

rasterVis::levelplot(cr, margin = FALSE, par.settings = viridisTheme())

## References

Palmer, Michael W. 1992. “The Coexistence of Species in Fractal Landscapes.” The American Naturalist 139 (2): 375–97. doi:10.1086/285332.