Introduction

The following exercises are part of the Rcompadre package vignettes. Since the GitHub page for the Rcompadre package is updated separately from this website, this page will include a brief summary of each exercise instead of the full text and code.

Links for each page are included in the summaries below.

Getting started with Rcompadre

GitHub page with code here.

This broad introduction to the Rcompadre package covers the following items:

- Downloading the COMPADRE and COMADRE databases and loading them into R
- Exploring the data (variable names, summary plots, etc.)
- Searching for species in the databases
- Accessing the matrix population models
- Subsetting the databases
- Potential issues with MPMs
- Making calculations from matrices

Using Rcompadre with the tidyverse

GitHub page with code here.

The "tidyverse" is a popular group of R packages geared toward data analysis, including dplyr (a grammar of data manipulation) and ggplot2 (a grammar of graphics). This exercise covers manipulation of the databases using packages within the tidyverse.

Age-from-stage analyses

GitHub page with code here.

This exercise demonstrates how to obtain age-specific schedules of survivorship and reproduction from MPMs regardless of whether the stage classes are based on age, size, and/or ontogeny.

**Important note:** As of this writing, the 'Rage' package is not available. In order to complete the exercise, two functions ('ageSpecificSurv' and 'ageSpecificRepro') must be manually defined. Use the code below to first define the functions, then use them as indicated in the exercise.

ageSpecificSurv <- function(matU, startLife, N) {

matUtempatU

lx <- vector(mode = 'numeric', length = N)

for (i in 1:N) {

lx[i] <- sum(matUtemp[,startLife])

matUtemp <- matUtemp %*% matU

}

return(c(1, lx))

}

ageSpecificRepro <- function(matU, matR, startLife, N) {

matUtemp <- matU

mx <- vector(mode = 'numeric', length = N)

for (i in 1:N) {

# stageDist equivalent to: matUtemp %*% solve(diag(colSums(matUtemp)))

stageDist <- apply(matUtemp, 2, function(x) x / sum(x))

phi <- matR %*% stageDist

mx[i] <- sum(phi[,startLife])

matUtemp <- matUtemp %*% matU

}

mx[is.nan(mx)] <- 0

return(c(0, mx))

}

Ternary plots

GitHub page with code here.

This exercise demonstrates how to produce ternary plots (Silvertown & Franco 1993) with population growth rate as the "fourth" dimension.

**Important note:** As of this writing, the 'Rage' package is not available. In order to complete the exercise, the function 'matrixElementPerturbation' must be manually defined. Use the code below to first define the function, then use it as indicated in the exercise.

matrixElementPerturbation <- function(matU, matF, matC = NULL, pert = 1e-6,

demogstat = "lambda", ...) {

# get statfun

if (demogstat == "lambda") {

statfun <- popbio::lambda

} else {

statfun <- try(match.fun(demogstat, descend = FALSE), silent = TRUE)

if (class(statfun) == "try-error") {

stop("demogstat must be 'lambda' or the name of a function that ",

"returns a single numeric value", call. = FALSE)

}

}

# matrix dimension

matDim <- nrow(matU)

# if matC null, convert to zeros

if (is.null(matC)) {

matC <- matrix(0, matDim, matDim)

}

# combine components into matA

matA <- matU + matF + matC

# statfun

stat <- statfun(matA, ...)

propU <- matU / matA

propU[is.nan(propU)] <- NA

propProg <- propRetrog <- propU

propProg[upper.tri(propU, diag = TRUE)] <- NA

propRetrog[lower.tri(propU, diag = TRUE)] <- NA

propStasis <- diag(matDim) * diag(propU)

propF <- matF / matA

propF[is.nan(propF)] <- NA

propC <- matC / matA

propC[is.nan(propC)] <- NA

# initialize sensitivity matrix

sensA <- matrix(NA, matDim, matDim)

# matrix perturbation

for (i in 1:matDim) {

for (j in 1:matDim) {

fakeA <- matA

fakeA[i, j] <- fakeA[i, j] + pert

statPert <- statfun(fakeA, ...)

sensA[i, j] <- (stat - statPert) / (matA[i, j] - fakeA[i, j])

}

}

sensA <- Re(sensA)

elasA <- sensA * matA / stat

out <- data.frame(

SStasis = sum(sensA * propStasis, na.rm = TRUE),

SRetrogression = sum(sensA * propRetrog, na.rm = TRUE),

SProgression = sum(sensA * propProg, na.rm = TRUE),

SFecundity = sum(sensA * propF, na.rm = TRUE),

SClonality = sum(sensA * propC, na.rm = TRUE),

EStasis = sum(elasA * propStasis, na.rm = TRUE),

EProgression = sum(elasA * propProg, na.rm = TRUE),

ERetrogression = sum(elasA * propRetrog, na.rm = TRUE),

EFecundity = sum(elasA * propF, na.rm = TRUE),

EClonality = sum(elasA * propC, na.rm = TRUE)

)

return(out)

}

Vectorizing with Rcompadre

GitHub page with code here.

Vectorized code generally runs faster than loops, and many R users find that vectorized code is easier to write and understand. This exercise covers the process of vectorizing data in the COMPADRE and COMADRE databases.

Obtaining references

GitHub page with code here.

This exercise illustrates how to find fully formatted citations for most of the publications used in COMPADRE and COMADRE. This example relies on the package 'rcrossref' and uses the Digitial Object Identifiers (DOIs) included in the metadata.