Skip to contents

Convert the wide [J sites, N species] response matrix used by Hmsc (and any single-visit JSDM workflow) into the long-format (series, time, y, ...) layout that mvgam() / jsdgam() with a simplex (diri(), multi(), categ()), continuous-response (mvn(), mvt()), or bernoulli / binomial / poisson family expects.

Usage

pivot_species_matrix(
  Y,
  site_covs = NULL,
  traits = NULL,
  site_id_name = "time",
  series_name = "series",
  values_to = "y"
)

Arguments

Y

A [J_sites, N_species] matrix or data.frame. Column names become the series factor levels; missing column names default to sp1, sp2, ....

site_covs

Optional data.frame of site-level covariates with nrow == J_sites. The Hmsc XData slot maps here.

traits

Optional data.frame of per-species traits with nrow == N_species. Broadcast across sites. The Hmsc TrData slot maps here.

site_id_name

Name of the site identifier in the output. Defaults to "time".

series_name

Name of the species column. Defaults to "series".

values_to

Name of the response column. Defaults to "y".

Value

A long-format data.frame with J * N rows, each carrying series, time, y, site covariates broadcast across species and species traits broadcast across sites.

Author

Nicholas J Clark

Examples

# Counts of three species at five sites, one column per species
Y <- matrix(c(3, 0, 5, 2, 7,
              1, 1, 0, 4, 2,
              0, 6, 2, 3, 1), nrow = 5,
            dimnames = list(NULL, c("ant", "bee", "wasp")))
long <- pivot_species_matrix(
  Y,
  site_covs = data.frame(elev = c(120, 340, 560, 780, 910)),
  traits = data.frame(body_size = c(2.1, 11.5, 16.0))
)
# One row per site and species, each carrying its site's elevation
# and its species' body size
head(long)
#>   series time y elev body_size
#> 1    ant    1 3  120       2.1
#> 2    bee    1 1  120      11.5
#> 3   wasp    1 0  120      16.0
#> 4    ant    2 0  340       2.1
#> 5    bee    2 1  340      11.5
#> 6   wasp    2 6  340      16.0

# Next, fit the long frame with jsdgam(unit = time, species = series)