Calculates an element-wise mean over a list of matrices or CompadreMat objects of constant dimension.

The difference between function mat_mean) and (mpm_mean is that mat_mean takes input as a list of matrices (e.g., a list of **A** matrices) while mat_mean takes input as a list of `CompadreMat` objects and thus calculates the mean matrices for both the **A** matrix and its submatrices (**U**, **F**, **C**).

mat_mean(x, na.rm = FALSE)

mpm_mean(x, na.rm = FALSE)

Arguments

x

A list of matrices or, for mpm_mean a list of `CompadreMat` objects, all of the same dimension

na.rm

Logical indicating whether missing values should be excluded (see Details). Defaults to FALSE.

Value

A matrix (mat_mean) or a CompadreMat object (mpm_mean).

Details

If na.rm == TRUE, missing values are ignored in the calculation of the mean matrix. If na.rm == TRUE and a given element is NA in every matrix within x, the value returned for that element will be 0.

Author

Patrick Barks <patrick.barks@gmail.com>

Owen R. Jones <jones@biology.sdu.dk>

Examples

# there are four rows for species 'Haplopappus_radiatus' in Compadre
mpms <- Compadre$mat[Compadre$SpeciesAuthor == "Haplopappus_radiatus"]

#The object mpms is a list, containing compadre objects
class(mpms)
#> [1] "list"
class(mpms[[1]])
#> [1] "CompadreMat"
#> attr(,"package")
#> [1] "Rcompadre"

mpm_mean(mpms)
#>   MatrixClassOrganized  MatrixClassAuthor
#> 1               active           Seedling
#> 2               active Small reproductive
#> 3               active Large reproductive
#> 4               active       Reproductive
#> 
#> matA:
#>         1      2       3       4
#> 1 0.00000 0.0000 0.00000 1.96575
#> 2 0.19725 0.4090 0.17650 0.15400
#> 3 0.03250 0.1605 0.38175 0.37500
#> 4 0.00000 0.1175 0.26100 0.37150
#> 
#> matU:
#>         1      2       3      4
#> 1 0.00000 0.0000 0.00000 0.0000
#> 2 0.19725 0.4090 0.17650 0.1540
#> 3 0.03250 0.1605 0.38175 0.3750
#> 4 0.00000 0.1175 0.26100 0.3715
#> 
#> matF:
#>   1 2 3       4
#> 1 0 0 0 1.96575
#> 2 0 0 0 0.00000
#> 3 0 0 0 0.00000
#> 4 0 0 0 0.00000
#> 
#> matC:
#>   1 2 3 4
#> 1 0 0 0 0
#> 2 0 0 0 0
#> 3 0 0 0 0
#> 4 0 0 0 0
#> 

# extract list of matA and take mean
mats <- matA(mpms)
mat_mean(mats)
#>         A1     A2      A3      A4
#> A1 0.00000 0.0000 0.00000 1.96575
#> A2 0.19725 0.4090 0.17650 0.15400
#> A3 0.03250 0.1605 0.38175 0.37500
#> A4 0.00000 0.1175 0.26100 0.37150