Friday, January 8, 2016

phylocurve: an R function for generating a rarefaction curve of Phylogenetic Diversity

Here's a function I have written for the R statistical environment that calculates a rarefaction curve giving expected phylogenetic diversity (mean and variance) for multiple values of sampling effort. Sampling effort can be defined in terms of the number of individuals, sites or species. Expected phylogenetic diversity is calculated using an exact analytical formulation (Nipperess & Matsen 2013) that is both more accurate and more computationally efficient than randomisation methods. I am providing it for free and without warranty under the GNU General Public License. You need to be familiar with R to use this function. The function also requires that the ape package be installed. To load the function, place the file in your working folder and type ‘source(“phylocurve.R”)’.

UPDATE (8th Jan 2016): I have finally implemented the exact solution for the variance of PD under rarefaction! A special acknowledgement to Florent Mazel for sharing his code with me. The function could probably be more efficient but it does the job and is still substantially faster (and more precise) than Monte Carlo subsampling.

UPDATE (21st Jan 2014): When using sampling without replacement (classic rarefaction), older versions of this function would not work with datasets that have a large number of objects (individuals/sites/species) to be rarefied. This is because phylocurve must calculate the number of possible combinations of each subset of objects in the total and this can be a very large number. It is possible, therefore, to exceed the largest number that R can handle. If that occurred, no warning would be issued but the output would include NAs instead of expected values of Phylogenetic Diversity. I have fixed this problem such that phylocurve should now be able to handle very large numbers (I had to re-learn some high school maths but I got there eventually). It might still be possible to exceed the limits of R but you should have a lot more headroom. If you do still encounter this problem, your options at this point are to: 1) sample with replacement (replace=TRUE); 2) try using a more powerful computer (although this is unlikely to make much difference); or 3) use the much slower phylocurve.perm function instead.

phylocurve (x, phy, stepm=1, subsampling = “individual”, replace = FALSE)
x is a community data table (as in the vegan package) with species/OTUs as columns and samples/sites as rows. Columns are labelled with the names of the species/OTUs. Rows are labelled with the names of the samples/sites. Data can be either abundance or incidence (0/1). Column labels must match tip labels in the phylogenetic tree exactly!
phy is a rooted phylogenetic tree with branch lengths stored as a phylo object (as in the ape package) with terminal nodes labelled with names matching those of the community data table. Note that the function trims away any terminal taxa not present in the community data table, so it is not necessary to do this beforehand.
stepm is the size of the interval in a sequence of numbers of individuals, sites or species to which x is to be rarefied.
subsampling indicates whether the subsampling will be by “individual” (default), “site” or “species”. When there are multiple sites, rarefaction by individuals or species is done by first pooling the sites.
replace is a boolean indicating whether subsampling should be done with or without replacement.

phylocurve takes a community data table and a rooted phylogenetic tree (with branch lengths) and calculates expected mean and variance of Phylogenetic Diversity (PD) for every specified value of m individuals, sites or species. m will range from 1 to the total number of individuals/sites/species in increments given by stepm. Calculations are done using the exact analytical formulae (Nipperess & Matsen, 2013) generalised from the classic equation of Hurlbert (1971). When there are multiple sites in the community table and rarefaction is by individuals or species, sites are first pooled.
phylocurve returns a matrix object of three columns giving the expected PD values (mean and variance) for each value of m.
Hurlbert (1971) The nonconcept of Species Diversity: a critique and alternative parameters. Ecology 52: 577-586.
Nipperess & Matsen (2013) The mean and variance of phylogenetic diversity under rarefaction. Methods in Ecology & Evolution 4: 566-572Manuscript also available from

No comments:

Post a Comment