`R/cdb_collapse.R`

`cdb_collapse.Rd`

Collapses a CompadreDB object by averaging matrices over levels of one or more grouping variables (e.g. SpeciesAuthor, MatrixPopulation).

For a given study and species, a COM(P)ADRE database may contain multiple matrices, reflecting different combinations of population, year, and/or treatment. Collapsing allows a user to obtain a single 'grand mean matrix' for each group of interest (e.g. MatrixPopulation), and therefore limit pseudoreplication.

All members of a group *must* have the same matrix dimension (consider
adding MatrixDimension as a grouping variable). All members of a group
*should* have the same ProjectionInterval and matrix stage class
definitions (see `cdb_id_stages`

). Note that Seasonal matrices
should not be collapsed using this method (they should be matrix-multiplied
rather than averaged).

`cdb_collapse(cdb, columns)`

- cdb
A CompadreDB object

- columns
Vector of grouping variables to collapse over (corresponding to columns within

`cdb`

)

A CompadreDB object

Will give a warning if members of any group do not all share the same
ProjectionInterval or stage class definitions, or if `cdb`

contains any
rows with a MatrixComposite value of "Seasonal".

Prior to collapsing, columns of class 'factor' will be coerced to
'character', and any list-column apart from `mat`

will be removed.

Within a group, rows of a given column are collapsed as follows:

`mat`

: matrices are averaged using mpm_mean, and stage class definitions are taken from the first group member`MatrixComposite`

: return original value if N = 1, else return "Collapsed"`Lat`

: re-calculated by averaging Lat column (if available)`Lon`

: re-calculated by averaging Lon column (if available)`SurvivalIssue`

: re-calculated from the collapsed`mat`

(`max(colSums(matU))`

)`others`

: if all elements equal return that unique value, else paste together all unique values separated by "; "

Other data checking:
`cdb_check_species()`

,
`cdb_compare()`

,
`cdb_flag()`

,
`mpm_methods`

```
# filter out Seasonal matrices
CompSub <- subset(Compadre, MatrixComposite != "Seasonal")
# add column identifying unique stage class definitions
CompSub$id_stage <- cdb_id_stages(CompSub, "MatrixClassOrganized")
# collapse
CompCollapse <- cdb_collapse(CompSub, columns = c("id_stage"))
```