How can I generate many random community matrices and then apply a function to all of them?

53 Views Asked by At

I have species' abundance data for different sites, and I'm basically interested in assessing whether indices derived from current assemblages are any different from indices derived from randomly generated communities. Right now my problem is that I can't find clear information on how to (1) generate random community matrices that only randomise the species present but keeps being matrices with abundance data and not just presence/absense; and (2) apply a function such that returns calculated indices for all these random communities, I'm interested in calculating functional diversity indices from packages such as FD or mFD, but any type of taxonomic or phylogenetic diversity index should serve just as well as an example.

I tried using the commsim and simulate functions from {vegan} package, but I only managed to generate random communities with presence/absense data. At this point I'm wholly confused with the help page for the function

library(vegan)
data("mite")
community<-mite
nm = vegan::nullmodel(x = community, method = "swap") #generated the dimensionality from my observed community matrix which is 35col by 75rows
sm = simulate(nm, nsim = 999)
2

There are 2 best solutions below

1
Juan C. Franco On

If you want to find Mean Phylogenetic Distance (MPD) or Mean Nearest Taxon Distance (MNTD), you can use package picante.

ses.mpd(comm, phy.dist, null.model = "richness", abundance.weighted = FALSE, runs = 999)

The runs comand is the number of random communities generated.

0
Ben Bolker On

I'm not sure if this is what you want, but r2dtable() simulates a table of counts with specified row and column margins.

library(vegan)
data("mite")
sims <- r2dtable(n = 1000, r = rowSums(mite), c = colSums(mite))
inds  <- vapply(sims, \(m) diversity(m, index = "shannon"), 
    FUN.VALUE = numeric(nrow(mite)))

This returns a 70 × 1000 matrix; each row is a vector of 1000 computed Shannon indices, for a given site.