Compute the pooled Gini-Simpson index of the rows in a matrix of compositional vectors
Source:R/fava_functions.R
gini_simpson_pooled.Rd
This function computes the Gini-Simpson index of a "pooled" vector equal to colMeans(relab_matrix)
. Values of 0 are achieved when this pooled vector is a permutation of (1,0,..., 0). The value approaches 1 as the number of categories K increases when this pooled vector is equal to (1/K, 1/K, ..., 1/K).
Arguments
- relab_matrix
A matrix or data frame with rows containing non-negative entries that sum to 1. Each row represents a sample, each column represents a category, and each entry represents the abundance of that category in the sample. If
relab_matrix
contains any metadata, it must be on the left-hand side of the matrix, the rightK
entries of each row must sum to 1, andK
must be specified. Otherwise, all entries of each row must sum to 1.- K
Optional; an integer specifying the number of categories in the data. Default is
K=ncol(relab_matrix)
.- S
Optional; a K x K similarity matrix with diagonal elements equal to 1 and off-diagonal elements between 0 and 1. Entry
S[i,k]
fori!=k
is the similarity between category andi
and categoryk
, equalling 1 if the categories are to be treated as identical and equaling 0 if they are to be treated as totally dissimilar. The default value isS = diag(ncol(relab_matrix))
.- w
Optional; a vector of length
I
with non-negative entries that sum to 1. Entryw[i]
represents the weight placed on rowi
in the computation of the mean abundance of each category across rows. The default value isw = rep(1/nrow(relab_matrix), nrow(relab_matrix))
.- time
Optional; a string specifying the name of the column that describes the sampling time for each row. Include if you wish to weight FAVA by the distance between samples.
- group
Optional; a string (or vector of strings) specifying the name(s) of the column(s) that describes which group(s) each row (sample) belongs to. Use if
relab_matrix
is a single matrix containing multiple groups of samples you wish to compare.
Examples
# To compute the pooled Gini-Simpson index of
# the following compositional vectors...
q1 = c(1, 0, 0, 0)
q2 = c(0.5, 0.5, 0, 0)
q3 = c(1/4, 1/4, 1/4, 1/4)
q4 = c(0, 0, 1, 0)
# we could compute the mean manually:
qPooled = (q1 + q2 + q3 + q4)/4
gini_simpson(qPooled)
#> [1] 0.671875
# Or we could use gini_simpson_pooled:
relative_abundances = matrix(c(q1, q2, q3, q4),
byrow = TRUE, nrow = 4)
gini_simpson_pooled(relative_abundances)
#> [1] 0.671875
# Incoporating weights:
# Compute pooled Gini-Simpson index ignoring
# rows 2 and 3
row_weights = c(0.5, 0, 0, 0.5)
gini_simpson_pooled(relative_abundances, w = row_weights)
#> [1] 0.5
# Compute pooled Gini-Simpson index assuming that
# categories 1 and 2 are identical:
similarity_matrix = diag(4)
similarity_matrix[1,2] = 1
similarity_matrix[2,1] = 1
gini_simpson_pooled(relative_abundances, S = similarity_matrix)
#> [1] 0.5078125
# Assume categories 1 and 2 are identical AND
# ignore rows 2 and 4:
row_weights = c(0.5, 0, 0.5, 0)
gini_simpson_pooled(relative_abundances, w = row_weights, S = similarity_matrix)
#> [1] 0.40625