# Installation of packages if necessary
InstallPackages <- function(Packages) {
  InstallPackage <- function(Package) {
    if (!Package %in% installed.packages()[, 1]) {
      install.packages(Package, repos="https://cran.rstudio.com/")
    }
  }
  invisible(sapply(Packages, InstallPackage))
}
InstallPackages(c("magrittr", "spatstat", "dbmss", "SpatDiv"))
library("magrittr")
# knitr options
knitr::opts_chunk$set(
  cache =   TRUE,  # Cache chunk results
  echo =    TRUE,  # Show R chunks
  warning = FALSE, # Hide warnings
  message = FALSE, # Hide warnings
  fig.align='center', out.width='80%', fig.dim=c(7, 5),
  # Code chunk format
  tidy=TRUE, tidy.opts=list(blank=FALSE, width.cutoff=80)
  )
# Random seed
set.seed(97310)

SpatDiv allows drawing random, spatialized communities as a whole, or species by species for more control.

Drawing random communities

The function rSpCommunity() draws a community (which is a weighted, marked, planar point pattern, i.e. a wmppp object defined in package dbmss) or a list of communities (an SpCommunities object) if argument n, the number of communities, is greater than 1.

library("SpatDiv")
spCommunity <- rSpCommunity(1, size=30, S=5)
autoplot(spCommunity)

The random community obtained is made of 30 individuals in 5 species. All arguments are set by default:

Species abundances

Several species abundance distributions (SAD) are available:

Individual size

The distribution of sizes is chosen by the argument Sizes:

Drawing species separately

The function rSpSpecies() draws a single species spatial pattern, which is a a wmppp object.

spSpecies <- rSpSpecies(50, Spatial = "Thomas", Sizes = "Exponential")
autoplot(spSpecies)

The point pattern of a a species is aggregated. It follows a Thomas point process with default arguments.

Available point processes are:

More complex spatial distributions can be simulated in package spatstat. Spatial can be an object of class ppp (a planar point pattern) obtained that way.

Combining point patterns

Spatial communities stored in wmppp and SpCommunities objects can be superimposed by the superimpose() function from package dbmss.

library("dbmss")
superimpose(spSpecies, spCommunity)  %>% 
  autoplot()

References



EricMarcon/SpatDiv documentation built on Nov. 28, 2024, 5:23 a.m.