Calculate a standard deviation over a list of matrices or CompadreMat objects
Source:R/mpm_sd.R
mpm_sd.Rd
Calculates an element-wise standard deviation over a list of matrices or CompadreMat objects of constant dimension.
Details
The difference between function mat_sd
) and (mpm_sd
is that
mat_sd
takes input as a list of matrices (e.g., a list of **A**
matrices) while mat_sd
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**).
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
.
See also
Other data management:
cdb_flatten()
,
cdb_id_stages()
,
cdb_id_studies()
,
cdb_id()
,
cdb_mean_matF()
,
cdb_rbind()
,
cdb_unflatten()
,
cdb_unnest()
,
mpm_elementwise_apply()
,
mpm_mean()
,
mpm_median()
,
string_representation
Examples
# set seed for repeatability
set.seed(42)
# create a function that generates a matrix with random values
create_matrix <- function() {
matrix(runif(9, 0, 1), nrow = 3)
}
# use replicate() to call the create_matrix() function 20 times
mat_list <- replicate(20, create_matrix(), simplify = FALSE)
# get the sd matrix
mat_sd(mat_list)
#> [,1] [,2] [,3]
#> [1,] 0.2853435 0.3059959 0.2933340
#> [2,] 0.2953802 0.2985387 0.3559057
#> [3,] 0.2796275 0.2465055 0.2627704
# If the matrices are in an RCompadre object, extract them using `matA` before
# passing to `mat_sd`
my_compadre <- cdb_build_cdb(mat_a = mat_list)
#> Warning: Metadata does not include a `SpeciesAccepted` column, so number
#> of species not provided when viewing object.
mat_sd(matA(my_compadre))
#> [,1] [,2] [,3]
#> [1,] 0.2853435 0.3059959 0.2933340
#> [2,] 0.2953802 0.2985387 0.3559057
#> [3,] 0.2796275 0.2465055 0.2627704