Package 'lirrr'

Title: Functions collected/wrote by Daijiang Li
Description: To keep all my functions in one place, and to use them more easily.
Authors: Daijiang Li [aut, cre]
Maintainer: Daijiang Li <[email protected]>
License: MIT + file LICENSE
Version: 0.0.2
Built: 2024-11-15 03:36:58 UTC
Source: https://github.com/daijiang/lirrr

Help Index


Make the first letter upper case

Description

Make the first letter to be upper case.

Usage

cap_first_letter(x)

Arguments

x

A vector of species names.

Value

A vector.


Example community data

Description

A data frame with site names as row names, species names as column names, cells are the abundance of each species at each site

Usage

comm_a

Format

a data frame with 15 sites and 15 species


#' Example community data

Description

A data frame with site names as row names, species names as column names, cells are the abundance of each species at each site

Usage

comm_b

Format

a data frame with 15 sites and 9 species


Change a distance matrix or a matrix to a data frame

Description

Convert a distance class object or a matrix to a data frame.

Usage

dist_to_df(x)

Arguments

x

An object of class 'dist' or a matrix.

Value

A data frame with three columns: Var1, Var2, distance.


Calculate number of polytomies

Description

Calculate the number of polytomies a phylogeny has.

Usage

get_n_polytomy(tree)

Arguments

tree

A phylogeny with 'phylo' as class.

Value

A data frame with number of polytomies as columns.

Examples

library(lirrr) 
get_n_polytomy(tree)

Calculate alpha phylogenetic diversity

Description

A function to calculate a bunch of phylo diversity: Faith's PD, MPD, VPD (variance of pairwise distance), MNTD, PSV/PSE. Results of MPD/MNTD from PhyloMeasures are equal with those from Phylocom/Picante with abundance.weight = FALSE Results of PD from PhyloMeasures are unrooted; PD from Phylocom are rooted.

Usage

get_pd_alpha(
  samp_wide,
  tree,
  samp_long,
  null.model.pd.root = FALSE,
  null.model.phylomeasures = TRUE,
  null.model.phylocom = FALSE,
  null.type.phylomeasures = "uniform",
  null.type.phylocom = 0,
  null.type.picante = "taxa.labels",
  n.item = 999,
  abund.weight = FALSE,
  verbose = TRUE,
  vpd = FALSE,
  ...
)

Arguments

samp_wide

Wide version of samp_long, row.names are sites, colnames are species.

tree

A phylogeny with class of 'phylo'.

samp_long

A 3-column data frame, site, freq, sp.

null.model.pd.root

Whether to run null models for rooted PD?

null.model.phylomeasures

Whether to run null models using PhyloMeasures package?

null.model.phylocom

Whether to run null models using phylocomr package?

null.type.phylomeasures

If null.model is TRUE, which null model to use for PhyloMeasure?

null.type.phylocom

If null.model is TRUE, which null model to use for Phylocom? - 0: phylogeny shuffle. - 1: maintain site richness and draw from species actually observed in sites. - 2: maintain site richness and draw from the phylogeny - 3: independent swap. See ?phylocomr::ph_comstruct for details

null.type.picante

If null.model is TRUE, which null model to use for Picante? Not used yet.

n.item

The number of randomization.

abund.weight

Should abundance information used when calculating pd with Phylocom/Picante? Default is FALSE.

verbose

Do you want to see relevant information?

vpd

To calculate vpd (varance of pairwise distance) or not?

...

Additional arguments.

Value

A data frame.


Highlight all branches linking a subset of species in a phylogeny

Description

Highlight all branches linking a subset of species in a phylogeny

Usage

highlight_subset_sp_in_phylogeny(phy, subset_sp, highlight_color = "red")

Arguments

phy

The large phylogeny to plot.

subset_sp

A vector of species names to be highlighted on the plot of phylogeny.

highlight_color

The color to highlight the branches, the default is red.

Value

A 'ggplot2' object.


Logit transformation

Description

Conduct logit-transformation for proportion data.

Usage

logit_tran(x, add_num = 0.01)

Arguments

x

A vector of proportions (either in form of 0.20 or 20).

add_num

The number to add for 0s, the dafult is 0.01.

Value

A vector has been logit transformed.


MPD and VPD (mean and variance of pairwise distance)

Description

Calculate MPD and VPD. The distance matrix can be from functional traits or phylogeny. This function is the same as 'picante::mpd' when 'abundance.weighted = FALSE'; it is, however, different when 'abundance.weighted = TRUE' because this function does not include the diagonal values while 'picante::mpd' does, which is actually equal to Rao's Q instead of MPD (de Bello et al, 2016, Oecologia).

Usage

mvpd(samp, dis, abundance.weighted = FALSE)

Arguments

samp

A site by species data frame, site names as row names.

dis

A distance matrix.

abundance.weighted

Whether to weight by species abundance? Default is 'FALSE'.

Value

A data frame with three columns: site, mpd, and vpd.


panel.cor to plot absolute value of correlations

Description

panel.cor to plot absolute value of correlations

Usage

panel.cor(x, y, digits = 2, prefix = "", cex.cor, color.threshold = 0.5, ...)

panel.hist to plot absolute value of correlations

Description

panel.hist to plot absolute value of correlations

Usage

panel.hist(x, ...)

Faith's PD

Description

Calculate faith's pd, this is a wrapper of 'phylocomr::ph_pd' and 'picante::pd'.

Usage

pd2(comm, tree, include.root = TRUE, comm_long)

Arguments

comm

A site by species data frame, site names as row names, only works for presence/absence data.

tree

A phylogeny of class "phylo".

include.root

Whether include root in picante::pd; phylocomr::ph_pd always include root.

comm_long

Long format of comm, 3-columns: site, freq, sp; optional.

Value

A data frame of PD for each site.


Phylogenetic beta diversity partition

Description

Calculate Phylogenetic beta diversity and its partition, adapted from betapart::phylo.belt.xx(). Since the pairwise and multisie version share the same core computation process, it makes more sense to return both. Then we can choose which one to use.

Usage

phylo_betapart(comm = dat_1, tree)

Arguments

comm

A site by species data frame, site names as row names.

tree

A phylogeny of class "phylo".

Value

A list of four: pairwise beta of jaccard and sorense; and multisite beta of jaccard and sorensen. Pairwise beta has three distance matrix. For jaccard, phylo.beta.jtu is the turnover-fraction of Jaccard, phylo.beta.jne is the nestedness-fraction. For sorensen, phylo.beta.sim is the turnover part measured as Simpson derived pairwise dissimilarity, phylo.beta.sne is the nestedness-fraction. Similarly for the multisite version.


Randomization tests

Description

Perform randomization test between two vectors.

Usage

rand_test(x, y, n = 1000)

Arguments

x

A numeric vector.

y

A numeric vector.

n

The number of randomization, default is 1000.

Value

a data frame with mean, rank, p.value, etc.


Remove site that has no observations (site by sp matrix)

Description

Remove site that has no obsrevations of any species.

Usage

rm_site_noobs(df)

Arguments

df

A data frame in wide form, i.e. site by species data frame, with site names as row name.

Value

A data frame.


Remove sp that not observed at any site (site by sp matrix)

Description

Remove species that not observed in any site.

Usage

rm_sp_noobs(df)

Arguments

df

A data frame in wide form, i.e. site by species data frame, with site names as row name.

Value

A data frame.


Match taxa names with the Open Tree of Life

Description

This function will try to do batch match based on rotl::tnrs_match_names(), which only allows <= 250 species each time.

Usage

tnrs_match_names_2(taxa, n_per_req = 20, ...)

Arguments

taxa

A vector of species names to match.

n_per_req

Number of species to match per requery, must be <= 250.

Value

A data frame.


#' Example phylogeny

Description

A phylogeny with more species than the community data

Usage

tree

Format

Newick format


unifrac

Description

calculate unifrac of pairwise site. This is based on picante::unifrac, but with phylocomr::ph_pd to calculate pd, which can improve speed dramatically.

Usage

unifrac2(comm, tree, comm_long)

Arguments

comm

A site by sp data frame, row names are site names.

tree

A phylogeny with 'phylo' class.

comm_long

A long format of comm, can be missing.

Value

A site by site distance object.


Function to add a column as row names, and remove it from columns

Description

Convert a column of a data frame to be its row name.

Usage

var_to_rownames(df, var = "site")

Arguments

df

A data frame.

var

The column name (as character) you want to put as row name.

Value

A data frame.