Skip to contents

This function compares phylodiversity metrics calculated in ps_diversity to their null distributions computed by randomizing the community matrix or shuffling the tips of the phylogeny, indicating statistical significance under the assumptions of the null model. Various null model algorithms are available for binary, probability, and count data.

Usage

ps_rand(
  ps,
  metric = c("PD", "PE", "RPE", "CE"),
  fun = "tip_shuffle",
  method = NULL,
  n_iter = 1000,
  n_rand = 100,
  summary = "quantile",
  spatial = TRUE,
  n_cores = 1,
  progress = interactive(),
  site_groups = NULL,
  wt_row = NULL,
  wt_col = NULL,
  ...
)

Arguments

ps

phylospatial object.

metric

Character vector giving one or more diversity metrics to calculate; see ps_diversity for options. Can also specify "all" to calculate all available metrics.

fun

Null model function to use. Must be either "tip_shuffle", "nullmodel", "nullcat", "quantize", or an actual function:

  • "tip_shuffle" (the default): randomly shuffles the identities of terminal taxa.

  • "nullmodel": uses nullmodel and simulate.nullmodel, from the vegan package, which offer a wide range of randomization algorithms with different properties.

  • "nullcat": uses null model algorithms from the nullcat package. Only works with binary community data. This is the recommended path for binary data when mixing diagnostics (via ps_suggest_n_iter()) are desired.

  • "quantize": uses quantize, which converts a quantitative matrix to discrete strata, applies a categorical variant of the selected null model, and then maps randomized strata back to values. Only works with quantitative (probability or abundance) community data.

  • Any other function that accepts a community matrix as its first argument and returns a randomized version of the matrix.

method

Method used by the selected function.

  • For fun = "nullmodel", one of the method options listed under commsim. Be sure to select a method that is appropriate to your community data_type (binary, quantitative, abundance).

  • For fun = "nullcat" or fun = "quantize", one of the categorical algorithms listed under nullcat_methods (e.g. "curvecat", "swapcat").

  • Ignored if fun is "tip_shuffle" or if it is a custom function.

n_iter

Integer giving the number of swap iterations per randomized matrix. Controls how thoroughly each null matrix is mixed before sampling. Default is 1000. Used with fun = "nullcat" (passed as n_iter to nullcat), fun = "quantize" (passed to quantize_prep), and fun = "nullmodel" with sequential methods like curveball (passed as burnin to simulate.nullmodel; ignored for non-sequential vegan methods). Use ps_suggest_n_iter() to estimate an appropriate value for your dataset.

n_rand

Integer giving the number of random communities to generate.

summary

Character vector of one or more summary statistics to return, each as a set of variables named with the statistic's first letter followed by the metric name (e.g. qPE, zPE, tPE):

  • "quantile" (the default): the proportion of randomizations in which the observed diversity metric was greater than the randomized metric, counting ties (randomizations equal to the observed value) as one half. A site whose randomized values all equal its observed value (for example, a site the null model cannot alter) therefore gets a quantile of 0.5.

  • "zscore": a "standardized effect size" or z-score relating the observed value to the mean and standard deviation of the randomizations.

  • "ties": the proportion t of randomizations whose value equals the observed value. Quantiles are bounded within [t/2, 1 - t/2], so a site cannot be significant in either tail at level alpha when t >= 2 * alpha. Ties are common at sites a null model can rarely change, such as sites alone (or nearly so) in their group under site_groups.

Whatever the summary, a warning is given if the randomized values of some sites equal their observed values in every randomization.

spatial

Logical: should the function return a spatial object (TRUE, default) or a matrix (FALSE).

n_cores

Integer giving the number of compute cores to use for parallel processing.

progress

Logical: should a progress bar be displayed?

site_groups

Optional grouping of sites for a spatially constrained null model, used only with fun = "nullcat" or fun = "quantize". Sites are randomized independently within each group, so taxa are only redistributed among sites in the same group. Either a vector giving each site's group (e.g. a region), applied to every randomization, or a matrix with one column per randomization (n_rand columns), giving a different grouping for each; ps_tile_groups() generates such a matrix of random spatial tilings, which pools each site with its neighbors. Rows (or elements) correspond either to all sites, as for the site_weight argument of phylospatial(), or to occupied sites only, as returned by ps_tile_groups(). With site_groups, n_iter iterations are run within every group; the number needed can be more or fewer than for the full matrix, so pass the same site_groups to ps_suggest_n_iter() to estimate it. See nullcat::nullcat() for details.

wt_row, wt_col

No longer supported, and must be NULL (the default); supplying a weight matrix gives an error. Weighting which pairs of sites exchange values does not constrain the null model spatially; use site_groups instead. See vignette("alpha-diversity").

...

Additional arguments passed to the selected function: quantize, nullcat, simulate.nullmodel, or a custom function. Note that the nsim argument to simulate.nullmodel should not be used here; specify n_rand instead.

Value

A matrix with a row for every row of x, a column for every metric specified in metric, and values for the summary statistic. Or if spatial = TRUE, a sf or SpatRaster object containing these data.

Details

If ps has site weights (see the site_weight argument of phylospatial()), they are used in calculating range sizes for endemism metrics, in both the observed and the randomized communities. Note, however, that null model algorithms that randomize the community matrix preserve taxon totals across sites, not weighted range sizes. With unequal site weights, a randomized taxon can therefore have a larger or smaller range size than the observed taxon, depending on which sites it is moved into, so null distributions of endemism metrics do not hold range sizes fixed. The default "tip_shuffle" method is not affected, since it moves intact terminal ranges among taxa.

Examples

# \donttest{
# simulate a `phylospatial` data set and run randomization with default settings
ps <- ps_simulate(data_type = "prob")
rand <- ps_rand(ps)

# using the `quantize` function with the `curvecat` algorithm
if(requireNamespace("nullcat")){
    rand <- ps_rand(ps,
      fun = "quantize", method = "curvecat",
      transform = sqrt, n_strata = 4, fixed = "cell")
}
#> Warning: 17 of 349 rows and 0 of 10 columns have all their values in a single stratum, so the randomization cannot change them under `fixed = "cell"`: their randomized values will equal the observed values. To randomize them, separate low values into their own strata (e.g. `zero_stratum = TRUE`, or a `transform`), or use a different `fixed` mode.
#> Warning: At some sites, every randomized value equaled the observed value, so the null model provided no comparison for them (PD: 17 sites). Their quantiles are 0.5 and their z-scores undefined. This happens, for example, at sites that are alone in their group under `site_groups`, or whose values the null model cannot rearrange; use `summary = "ties"` to see the share of tied randomizations at each site.

# binary data with nullcat's curvecat algorithm
ps2 <- ps_simulate(data_type = "binary")
if(requireNamespace("nullcat")){
    rand <- ps_rand(ps2, fun = "nullcat", method = "curvecat", n_iter = 1000)
}

# using binary data, with a vegan `nullmodel` algorithm
rand <- ps_rand(ps2, "PD", "nullmodel", "r2")

# using abundance data
ps3 <- ps_simulate(data_type = "abund")
rand <- ps_rand(ps3, metric = c("ShPD", "SiPD"),
      fun = "nullmodel", method = "abuswap_c")

# spatially constrained null model: randomize within a different random
# tiling (with 500 km spacing) in each randomization
if(requireNamespace("nullcat")){
    ps4 <- moss()
    tiles <- ps_tile_groups(ps4, spacing = 500e3, n = 20)
    rand <- ps_rand(ps4, metric = "PE", fun = "quantize", n_rand = 20,
                    zero_stratum = TRUE, n_iter = 5000,
                    site_groups = tiles, summary = c("quantile", "ties"))
}
# }