Skip to content

Instantly share code, notes, and snippets.

@jslefche
jslefche / README.md
Last active January 19, 2023 05:21
Hierarchically partition variance in GLMM

Compute semi-variance (partial R[2]) for GLMMs fit used lme4.

library(lme4)

example <- data.frame(
  y = rnorm(100),
  x = rnorm(100),
  nested1 = letters[1:20],
 nested2 = rep(letters[1:5], each = 4),
@jslefche
jslefche / README.md
Last active January 26, 2023 15:45
Decomposition of community thermal index into individual species contributions

A function that takes individual species thermal indices (STIs) and their abundances, and partitions their contributions to the change in the community thermal index through at least two time points.

example <- data.frame(
  site = "site1",
  date = c(rep(as.Date("2001-01-01"), 5), rep(as.Date("2002-01-01"), 5)),
  species = paste0("species", LETTERS[1:5]),
  STI = c(21.2, 23.4, 19.2, 26.1, 22.0),
  abundance = c(0, 0, 10, 8, 3, 1, 3, 5, 0, 0)
 )
@jslefche
jslefche / README.md
Last active February 17, 2021 14:29
covstop: A function for coverage-based stopping of biodiversity sampling

covstop: Coverage-based stopping for biodiversity sampling

A function that takes a sample (rows)-by-species (columns) community matrix and uses coverage-based subsampling to identify the number of samples of individuals required to achieve a given level of coverage of total biodiversity.

From: Chao, Anne, and Lou Jost. "Coverage‐based rarefaction and extrapolation: standardizing samples by completeness rather than size." Ecology 93.12 (2012): 2533-2547.

Example

# generate fake community matrix
vec &lt;- sample(0:100, 100, replace = T)
@jslefche
jslefche / README.md
Last active July 29, 2021 18:08
The Modified Price Equation for Biodiversity and Ecosystem Functioning

The Modified Price Equation

The following is a modified form of the Price equation which allows for a more fair comparison of the richness and composition terms.

Example

# Create example community-by-species matrix
mat <- runif(100, 0, 10)

mat[sample(1:100, 30, replace = F)] <- 0
@jslefche
jslefche / README.md
Last active June 1, 2018 13:48
Simulate communities for BEF

simulateComm: simulate communities with predefined properties

Simulate community-by-species matrix of "functioning" (e.g., biomass) where the total community richness has a predefined correlation with total community biomass (i.e., the row sums).

# Generate community data
ncomms <- 100

nspecies <- 20
@jslefche
jslefche / README.md
Last active July 10, 2019 18:05
Faster coverage-based rarefaction

Faster coverage-based subsampling

A revised function from the iNEXT package (https://github.com/JohnsonHsieh/iNEXT) that calculates coverage-based rarefaction from abundance data. Instead of computing all orders of q it allows the user to specify a single order, and additionally omits bootstrapping of standard errors. The resulting function is much, much faster.

EXAMPLE

# Create fake community-by-species abundance matrix
@jslefche
jslefche / README.md
Last active May 17, 2021 05:58
Rao's quadratic entropy

Computing Rao's Quadratic entropy

Takes a sample-by-species abundance matrix and species-by-species functional distance matrix and returns values of Rao's quadratic entropy. Values are optionally converted into effective numbers of species through the transformation: 1/(1 - D).

EXAMPLE

# Create sample-by-species abundance matrix
abund <- matrix(sample(1:100, 100, replace = T), 10, 10)

colnames(abund) &lt;- paste0("species", 1:10)
@jslefche
jslefche / README.md
Last active March 21, 2020 15:14
Hierarchical variance partitioning

Hierarchical variance partitioning used linear mixed effects models

The function VarCorrCI takes a merMod object and returns variance components and 95% confidence intervals.

Modified from: http://rpubs.com/bbolker/varwald and various other places.

EXAMPLE

library(lme4)
@jslefche
jslefche / README.md
Last active August 21, 2019 17:57
Price Equation for Diversity-Function

The Price Equation for Partitioning Diversity Effects on Ecosystem Function

This function takes a data.frame corresponding to the site-by-species "functioning" matrix (where cells contain the values of the ecosystem function), and returns the five additive components of the Price equation.

EXAMPLE

# Example 1: all species contribute equally to functioning and all occur at the baseline site
# Here, RICH_L should be negative and equal the total number of unshared species at each site
# COMP and CDE terms should be zero
@jslefche
jslefche / README.md
Last active October 23, 2019 15:55
Edgar Equations

Edgar Equations for Estimating Invertebrate Biomass from Size Structured Abundances

Generates estimates of ash-free dry weight from size-fractionated abundances of epifaunal invertebrates.

From:

Edgar, Graham J. "The use of the size structure of benthic macrofaunal communities to estimate faunal biomass
and secondary production." Journal of Experimental Marine Biology and Ecology 137.3 (1990): 195-214.

Example