Skip to content

Instantly share code, notes, and snippets.

@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 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 <- sample(0:100, 100, replace = T)
@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 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 May 29, 2022 00:23
ggplot2: theme_black()

Black theme for ggplot2

This is an additional theme for ggplot2 that generates an inverse black and white color scheme.

Example

ggplot(mtcars, aes(wt, mpg)) + geom_point()
# Add theme_black()
ggplot(mtcars, aes(wt, mpg)) + geom_point(color = "white") + theme_black()
@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)
 )