Skip to contents

This function generates a list of n MPMs according to the specified criteria. Criteria include the archetype, and the acceptable constraining criteria, which could include lambda, generation time or any other metric derived from an A matrix. The function attempts to find matrices that fulfil the criteria, discarding unacceptable matrices. By default, if it takes more than 1000 attempts to find a suitable matrix model, then an error is produced. However, the number of attempts can be altered with the attempts parameter.

Usage

rand_lefko_set(
  n_models = 5,
  n_stages = 3,
  archetype = 1,
  fecundity = 1.5,
  output = "Type1",
  max_surv = 0.99,
  constraint = NULL,
  attempts = 1000
)

Arguments

n_models

An integer indicating the number of MPMs to generate.

n_stages

The number of stages for the MPMs. Default is 3.

archetype

The archetype of the MPMs. Default is 1.

fecundity

The average number of offspring produced (fecundity). Values can be provided in 4 ways:

  • An numeric vector of length 1 providing a single measure of fecundity to the top right corner of the matrix model only.

  • A numeric vector of integers of length equal to n_stages to provide fecundity estimates for the whole top row of the matrix model. Use 0 for cases with no fecundity.

  • A matrix of numeric values of the same dimension as n_stages to provide fecundity estimates for the entire matrix model. Use 0 for cases with no fecundity.

  • A list of two matrices of numeric values, both with the same dimension as n_stages, to provide lower and upper limits of mean fecundity for the entire matrix model.

In the latter case, a fecundity value will be drawn from a uniform distribution for the defined range. If there is no fecundity in a particular age class, use a value of 0 for both the lower and upper limit.

output

Character string indicating the type of output.

  • Type1: A compadreDB Object containing MPMs split into the submatrices (i.e. A, U, F and C).

  • Type2: A compadreDB Object containing MPMs that are not split into submatrices (i.e. only the A matrix is included).

  • Type3: A list of MPMs arranged so that each element of the list contains a model and associated submatrices (i.e. the nth element contains the nth A matrix alongside the nth U and F matrices).

  • Type4: A list of MPMs arranged so that the list contains 3 lists for the A matrix and the U and F submatrices respectively.

  • Type5: A list of MPMs, including only the A matrix.

max_surv

The maximum acceptable survival value, calculated across all transitions from a stage. Defaults to 0.99. This is only used the output splits a matrix into the submatrices.

constraint

An optional data frame with 4 columns named fun, arg, lower and upper. These columns specify (1) a function that outputs a metric derived from an A matrix and (2) an argument for the function (NA, if no argument supplied) (3) the lower acceptable bound for the metric and (4) upper acceptable bound for the metric.

attempts

An integer indicating the number of attempts To be made when simulating matrix model. The default is 1000. If it takes more than 1000 attempts to make a matrix that satisfies the conditions set by the other arguments, then a warning is produced.

Value

A compadreDB object or list of MPMs that meet the specified criteria.

References

Caswell, H. (2001). Matrix Population Models: Construction, Analysis, and Interpretation. Sinauer.

Lefkovitch, L. P. (1965). The study of population growth in organisms grouped by stages. Biometrics, 21(1), 1.

Takada, T., Kawai, Y., & Salguero-Gómez, R. (2018). A cautionary note on elasticity analyses in a ternary plot using randomly generated population matrices. Population Ecology, 60(1), 37–47.

See also

rand_lefko_mpm() which this function is essentially a wrapper for.

Other Lefkovitch matrices: generate_mpm_set(), rand_lefko_mpm(), random_mpm()

Author

Owen Jones jones@biology.sdu.dk

Examples

set.seed(42) # set seed for repeatability

# Basic operation, without splitting matrices and with no constraints
rand_lefko_set(
  n_models = 3, n_stages = 5, fecundity = c(0, 0, 4, 8, 10),
  archetype = 4, output = "Type5"
)
#> [[1]]
#>            [,1]       [,2]      [,3]      [,4]       [,5]
#> [1,] 0.04028499 0.00000000 4.0000000 8.0000000 10.0000000
#> [2,] 0.71907852 0.73066610 0.0000000 0.0000000  0.0000000
#> [3,] 0.00000000 0.06797628 0.5129277 0.0000000  0.0000000
#> [4,] 0.00000000 0.00000000 0.2895418 0.8589953  0.0000000
#> [5,] 0.00000000 0.00000000 0.0000000 0.1331578  0.9997924
#> 
#> [[2]]
#>            [,1]       [,2]      [,3]      [,4]       [,5]
#> [1,] 0.04993404 0.00000000 4.0000000 8.0000000 10.0000000
#> [2,] 0.35213116 0.38662704 0.0000000 0.0000000  0.0000000
#> [3,] 0.00000000 0.04413311 0.2022055 0.0000000  0.0000000
#> [4,] 0.00000000 0.00000000 0.4116914 0.4473679  0.0000000
#> [5,] 0.00000000 0.00000000 0.0000000 0.4300040  0.9673925
#> 
#> [[3]]
#>           [,1]      [,2]      [,3]      [,4]       [,5]
#> [1,] 0.3908713 0.0000000 4.0000000 8.0000000 10.0000000
#> [2,] 0.1593942 0.3008004 0.0000000 0.0000000  0.0000000
#> [3,] 0.0000000 0.4611680 0.5792468 0.0000000  0.0000000
#> [4,] 0.0000000 0.0000000 0.2741919 0.2238051  0.0000000
#> [5,] 0.0000000 0.0000000 0.0000000 0.6835265  0.9941976
#> 

# Constrain outputs to A matrices with lambda between 0.9 and 1.1
library(popbio)
constrain_df <- data.frame(
  fun = "lambda", arg = NA, lower = 0.9, upper =
    1.1
)
rand_lefko_set(
  n_models = 10, n_stages = 5, fecundity = c(0, 0, 4, 8, 10),
  archetype = 4, constraint = constrain_df, output = "Type5"
)
#> [[1]]
#>            [,1]      [,2]       [,3]       [,4]       [,5]
#> [1,] 0.16074427 0.0000000 4.00000000 8.00000000 10.0000000
#> [2,] 0.03843446 0.1920300 0.00000000 0.00000000  0.0000000
#> [3,] 0.00000000 0.5599534 0.83063495 0.00000000  0.0000000
#> [4,] 0.00000000 0.0000000 0.01691095 0.06976802  0.0000000
#> [5,] 0.00000000 0.0000000 0.00000000 0.92957038  0.9997121
#> 
#> [[2]]
#>            [,1]      [,2]      [,3]      [,4]       [,5]
#> [1,] 0.40726303 0.0000000 4.0000000 8.0000000 10.0000000
#> [2,] 0.02472534 0.5507259 0.0000000 0.0000000  0.0000000
#> [3,] 0.00000000 0.1762418 0.4303426 0.0000000  0.0000000
#> [4,] 0.00000000 0.0000000 0.3952927 0.1772480  0.0000000
#> [5,] 0.00000000 0.0000000 0.0000000 0.7574622  0.9818421
#> 
#> [[3]]
#>            [,1]         [,2]      [,3]       [,4]       [,5]
#> [1,] 0.13213712 0.0000000000 4.0000000 8.00000000 10.0000000
#> [2,] 0.01633696 0.0002954688 0.0000000 0.00000000  0.0000000
#> [3,] 0.00000000 0.4951548186 0.2901663 0.00000000  0.0000000
#> [4,] 0.00000000 0.0000000000 0.3561964 0.96238123  0.0000000
#> [5,] 0.00000000 0.0000000000 0.0000000 0.01793056  0.9986862
#> 
#> [[4]]
#>           [,1]       [,2]       [,3]      [,4]      [,5]
#> [1,] 0.2025021 0.00000000 4.00000000 8.0000000 10.000000
#> [2,] 0.1808410 0.51560726 0.00000000 0.0000000  0.000000
#> [3,] 0.0000000 0.05938074 0.69893955 0.0000000  0.000000
#> [4,] 0.0000000 0.00000000 0.04270201 0.1153077  0.000000
#> [5,] 0.0000000 0.00000000 0.00000000 0.6563415  0.905483
#> 
#> [[5]]
#>           [,1]        [,2]       [,3]      [,4]       [,5]
#> [1,] 0.1014989 0.000000000 4.00000000 8.0000000 10.0000000
#> [2,] 0.4079260 0.641405627 0.00000000 0.0000000  0.0000000
#> [3,] 0.0000000 0.007769844 0.07572709 0.0000000  0.0000000
#> [4,] 0.0000000 0.000000000 0.65797642 0.6475288  0.0000000
#> [5,] 0.0000000 0.000000000 0.00000000 0.3319838  0.9979733
#> 
#> [[6]]
#>           [,1]       [,2]       [,3]      [,4]       [,5]
#> [1,] 0.3492059 0.00000000 4.00000000 8.0000000 10.0000000
#> [2,] 0.2623854 0.59588086 0.00000000 0.0000000  0.0000000
#> [3,] 0.0000000 0.02204176 0.03327729 0.0000000  0.0000000
#> [4,] 0.0000000 0.00000000 0.64881290 0.1459793  0.0000000
#> [5,] 0.0000000 0.00000000 0.00000000 0.6109135  0.9411863
#> 
#> [[7]]
#>           [,1]      [,2]       [,3]      [,4]      [,5]
#> [1,] 0.0100986 0.0000000 4.00000000 8.0000000 10.000000
#> [2,] 0.2816060 0.3421699 0.00000000 0.0000000  0.000000
#> [3,] 0.0000000 0.2403473 0.60176000 0.0000000  0.000000
#> [4,] 0.0000000 0.0000000 0.01202352 0.5052573  0.000000
#> [5,] 0.0000000 0.0000000 0.00000000 0.2482741  0.975591
#> 
#> [[8]]
#>            [,1]       [,2]      [,3]      [,4]       [,5]
#> [1,] 0.05277117 0.00000000 4.0000000 8.0000000 10.0000000
#> [2,] 0.14635732 0.14395871 0.0000000 0.0000000  0.0000000
#> [3,] 0.00000000 0.09478692 0.2766104 0.0000000  0.0000000
#> [4,] 0.00000000 0.00000000 0.6115977 0.1849629  0.0000000
#> [5,] 0.00000000 0.00000000 0.0000000 0.7926018  0.9830451
#> 
#> [[9]]
#>           [,1]       [,2]      [,3]      [,4]      [,5]
#> [1,] 0.2695917 0.00000000 4.0000000 8.0000000 10.000000
#> [2,] 0.2267870 0.45112437 0.0000000 0.0000000  0.000000
#> [3,] 0.0000000 0.05989263 0.3118949 0.0000000  0.000000
#> [4,] 0.0000000 0.00000000 0.3091554 0.2289075  0.000000
#> [5,] 0.0000000 0.00000000 0.0000000 0.6927089  0.956369
#> 
#> [[10]]
#>           [,1]      [,2]      [,3]       [,4]       [,5]
#> [1,] 0.2788292 0.0000000 4.0000000 8.00000000 10.0000000
#> [2,] 0.0816387 0.2433186 0.0000000 0.00000000  0.0000000
#> [3,] 0.0000000 0.3089479 0.1000315 0.00000000  0.0000000
#> [4,] 0.0000000 0.0000000 0.4670093 0.73745701  0.0000000
#> [5,] 0.0000000 0.0000000 0.0000000 0.04978961  0.8887938
#> 

# As above, but using popdemo::eigs function instead of popbio::lambda
# to illustrate use of argument
library(popdemo)
constrain_df <- data.frame(
  fun = "eigs", arg = "lambda", lower = 0.9, upper = 1.1
)

rand_lefko_set(
  n_models = 10, n_stages = 5, fecundity = c(0, 0, 4, 8, 10),
  archetype = 4, constraint = constrain_df, output = "Type5"
)
#> [[1]]
#>           [,1]      [,2]       [,3]      [,4]       [,5]
#> [1,] 0.2772425 0.0000000 4.00000000 8.0000000 10.0000000
#> [2,] 0.1276093 0.4284850 0.00000000 0.0000000  0.0000000
#> [3,] 0.0000000 0.1104893 0.05017521 0.0000000  0.0000000
#> [4,] 0.0000000 0.0000000 0.60407149 0.1454031  0.0000000
#> [5,] 0.0000000 0.0000000 0.00000000 0.7206787  0.8721605
#> 
#> [[2]]
#>            [,1]      [,2]      [,3]       [,4]       [,5]
#> [1,] 0.00794897 0.0000000 4.0000000 8.00000000 10.0000000
#> [2,] 0.10589532 0.2360636 0.0000000 0.00000000  0.0000000
#> [3,] 0.00000000 0.1030530 0.2807865 0.00000000  0.0000000
#> [4,] 0.00000000 0.0000000 0.1883105 0.62282892  0.0000000
#> [5,] 0.00000000 0.0000000 0.0000000 0.08604455  0.9653569
#> 
#> [[3]]
#>           [,1]     [,2]       [,3]      [,4]       [,5]
#> [1,] 0.1495233 0.000000 4.00000000 8.0000000 10.0000000
#> [2,] 0.1741336 0.426369 0.00000000 0.0000000  0.0000000
#> [3,] 0.0000000 0.136707 0.62072487 0.0000000  0.0000000
#> [4,] 0.0000000 0.000000 0.06783148 0.1980795  0.0000000
#> [5,] 0.0000000 0.000000 0.00000000 0.5218206  0.7994516
#> 
#> [[4]]
#>            [,1]      [,2]       [,3]       [,4]       [,5]
#> [1,] 0.05927331 0.0000000 4.00000000 8.00000000 10.0000000
#> [2,] 0.22972930 0.2638984 0.00000000 0.00000000  0.0000000
#> [3,] 0.00000000 0.1044152 0.85977997 0.00000000  0.0000000
#> [4,] 0.00000000 0.0000000 0.01677576 0.02321774  0.0000000
#> [5,] 0.00000000 0.0000000 0.00000000 0.95518252  0.9975017
#> 
#> [[5]]
#>            [,1]       [,2]       [,3]       [,4]       [,5]
#> [1,] 0.04831774 0.00000000 4.00000000 8.00000000 10.0000000
#> [2,] 0.06775510 0.03037236 0.00000000 0.00000000  0.0000000
#> [3,] 0.00000000 0.37177964 0.09800894 0.00000000  0.0000000
#> [4,] 0.00000000 0.00000000 0.33187578 0.74345426  0.0000000
#> [5,] 0.00000000 0.00000000 0.00000000 0.01656937  0.7731065
#> 
#> [[6]]
#>            [,1]      [,2]      [,3]      [,4]       [,5]
#> [1,] 0.33328577 0.0000000 4.0000000 8.0000000 10.0000000
#> [2,] 0.06377826 0.2498128 0.0000000 0.0000000  0.0000000
#> [3,] 0.00000000 0.3337334 0.5963528 0.0000000  0.0000000
#> [4,] 0.00000000 0.0000000 0.1693849 0.5795012  0.0000000
#> [5,] 0.00000000 0.0000000 0.0000000 0.2212670  0.8103362
#> 
#> [[7]]
#>           [,1]       [,2]       [,3]       [,4]       [,5]
#> [1,] 0.0239377 0.00000000 4.00000000 8.00000000 10.0000000
#> [2,] 0.0366482 0.04343731 0.00000000 0.00000000  0.0000000
#> [3,] 0.0000000 0.68395612 0.68478775 0.00000000  0.0000000
#> [4,] 0.0000000 0.00000000 0.08214221 0.06041167  0.0000000
#> [5,] 0.0000000 0.00000000 0.00000000 0.82870382  0.9361283
#> 
#> [[8]]
#>            [,1]      [,2]      [,3]       [,4]       [,5]
#> [1,] 0.03354061 0.0000000 4.0000000 8.00000000 10.0000000
#> [2,] 0.24257199 0.1737578 0.0000000 0.00000000  0.0000000
#> [3,] 0.00000000 0.1323195 0.2086060 0.00000000  0.0000000
#> [4,] 0.00000000 0.0000000 0.3520131 0.61162941  0.0000000
#> [5,] 0.00000000 0.0000000 0.0000000 0.06355464  0.7634507
#> 
#> [[9]]
#>            [,1]      [,2]      [,3]      [,4]       [,5]
#> [1,] 0.18298502 0.0000000 4.0000000 8.0000000 10.0000000
#> [2,] 0.05516901 0.0637293 0.0000000 0.0000000  0.0000000
#> [3,] 0.00000000 0.3666609 0.3365942 0.0000000  0.0000000
#> [4,] 0.00000000 0.0000000 0.3601747 0.2711895  0.0000000
#> [5,] 0.00000000 0.0000000 0.0000000 0.4848073  0.9864156
#> 
#> [[10]]
#>           [,1]       [,2]       [,3]      [,4]       [,5]
#> [1,] 0.1134325 0.00000000 4.00000000 8.0000000 10.0000000
#> [2,] 0.1691937 0.26547788 0.00000000 0.0000000  0.0000000
#> [3,] 0.0000000 0.03683351 0.06710737 0.0000000  0.0000000
#> [4,] 0.0000000 0.00000000 0.48303739 0.3510717  0.0000000
#> [5,] 0.0000000 0.00000000 0.00000000 0.6402862  0.9920856
#> 

# Multiple constraints
# Constrain outputs to A matrices with lambda between 0.9 and 1.1, generation
# time between 3 and 5 and damping ratio between 1 and 7.
library(popbio)
constrain_df <- data.frame(
  fun = c("lambda", "generation.time", "damping.ratio"),
  arg = c(NA, NA, NA),
  lower = c(0.9, 3.0, 1.0),
  upper = c(1.1, 5.0, 7.0)
)
rand_lefko_set(
  n_models = 10, n_stages = 5, fecundity = c(0, 0, 4, 8, 10),
  archetype = 4, constraint = constrain_df, output = "Type5"
)
#> [[1]]
#>            [,1]      [,2]       [,3]      [,4]       [,5]
#> [1,] 0.56648471 0.0000000 4.00000000 8.0000000 10.0000000
#> [2,] 0.05281201 0.3000692 0.00000000 0.0000000  0.0000000
#> [3,] 0.00000000 0.3479332 0.01618986 0.0000000  0.0000000
#> [4,] 0.00000000 0.0000000 0.70611327 0.3991537  0.0000000
#> [5,] 0.00000000 0.0000000 0.00000000 0.5322706  0.9656675
#> 
#> [[2]]
#>            [,1]      [,2]       [,3]      [,4]       [,5]
#> [1,] 0.00572345 0.0000000 4.00000000 8.0000000 10.0000000
#> [2,] 0.25867680 0.1849481 0.00000000 0.0000000  0.0000000
#> [3,] 0.00000000 0.2763296 0.47875737 0.0000000  0.0000000
#> [4,] 0.00000000 0.0000000 0.04194771 0.2885805  0.0000000
#> [5,] 0.00000000 0.0000000 0.00000000 0.5292559  0.8627223
#> 
#> [[3]]
#>           [,1]      [,2]      [,3]      [,4]      [,5]
#> [1,] 0.3299881 0.0000000 4.0000000 8.0000000 10.000000
#> [2,] 0.3730901 0.5780448 0.0000000 0.0000000  0.000000
#> [3,] 0.0000000 0.1703570 0.5616340 0.0000000  0.000000
#> [4,] 0.0000000 0.0000000 0.2011312 0.7910559  0.000000
#> [5,] 0.0000000 0.0000000 0.0000000 0.0203304  0.847165
#> 
#> [[4]]
#>           [,1]      [,2]      [,3]      [,4]       [,5]
#> [1,] 0.3403444 0.0000000 4.0000000 8.0000000 10.0000000
#> [2,] 0.1152112 0.1165285 0.0000000 0.0000000  0.0000000
#> [3,] 0.0000000 0.6890762 0.1874684 0.0000000  0.0000000
#> [4,] 0.0000000 0.0000000 0.7050681 0.7296493  0.0000000
#> [5,] 0.0000000 0.0000000 0.0000000 0.1842275  0.9170533
#> 
#> [[5]]
#>           [,1]       [,2]      [,3]       [,4]       [,5]
#> [1,] 0.1689392 0.00000000 4.0000000 8.00000000 10.0000000
#> [2,] 0.2063822 0.30596227 0.0000000 0.00000000  0.0000000
#> [3,] 0.0000000 0.07818665 0.1925666 0.00000000  0.0000000
#> [4,] 0.0000000 0.00000000 0.3427810 0.01826475  0.0000000
#> [5,] 0.0000000 0.00000000 0.0000000 0.63827242  0.9756126
#> 
#> [[6]]
#>            [,1]      [,2]       [,3]      [,4]       [,5]
#> [1,] 0.00652138 0.0000000 4.00000000 8.0000000 10.0000000
#> [2,] 0.00545548 0.2302399 0.00000000 0.0000000  0.0000000
#> [3,] 0.00000000 0.3754500 0.65618224 0.0000000  0.0000000
#> [4,] 0.00000000 0.0000000 0.01014227 0.6273468  0.0000000
#> [5,] 0.00000000 0.0000000 0.00000000 0.3247017  0.9748711
#> 
#> [[7]]
#>           [,1]       [,2]       [,3]      [,4]       [,5]
#> [1,] 0.2948358 0.00000000 4.00000000 8.0000000 10.0000000
#> [2,] 0.2761557 0.01036768 0.00000000 0.0000000  0.0000000
#> [3,] 0.0000000 0.56983696 0.55866742 0.0000000  0.0000000
#> [4,] 0.0000000 0.00000000 0.05776301 0.5119449  0.0000000
#> [5,] 0.0000000 0.00000000 0.00000000 0.1534077  0.7923763
#> 
#> [[8]]
#>           [,1]      [,2]      [,3]      [,4]       [,5]
#> [1,] 0.4268973 0.0000000 4.0000000 8.0000000 10.0000000
#> [2,] 0.1469940 0.1228170 0.0000000 0.0000000  0.0000000
#> [3,] 0.0000000 0.5159129 0.2351381 0.0000000  0.0000000
#> [4,] 0.0000000 0.0000000 0.4172713 0.1631152  0.0000000
#> [5,] 0.0000000 0.0000000 0.0000000 0.6079843  0.9203012
#> 
#> [[9]]
#>           [,1]      [,2]        [,3]      [,4]       [,5]
#> [1,] 0.3658073 0.0000000 4.000000000 8.0000000 10.0000000
#> [2,] 0.3986741 0.7194963 0.000000000 0.0000000  0.0000000
#> [3,] 0.0000000 0.0833023 0.008129479 0.0000000  0.0000000
#> [4,] 0.0000000 0.0000000 0.809932058 0.6717799  0.0000000
#> [5,] 0.0000000 0.0000000 0.000000000 0.2036987  0.8840548
#> 
#> [[10]]
#>           [,1]      [,2]      [,3]      [,4]      [,5]
#> [1,] 0.2012457 0.0000000 4.0000000 8.0000000 10.000000
#> [2,] 0.1674917 0.2097260 0.0000000 0.0000000  0.000000
#> [3,] 0.0000000 0.4571663 0.3774105 0.0000000  0.000000
#> [4,] 0.0000000 0.0000000 0.4973726 0.3033012  0.000000
#> [5,] 0.0000000 0.0000000 0.0000000 0.6690492  0.975605
#>