R/PRE_FATE.speciesDistanceCombine.R
PRE_FATE.speciesDistanceCombine.RdThis script is designed to create a distance matrix between species, combining several dissimilarity distance matrices.
PRE_FATE.speciesDistanceCombine(
list.mat.dist,
opt.min.noMat = length(list.mat.dist),
opt.normal = TRUE,
opt.weights = NULL
)a list of matrices containing dissimilarity
distance values between each pair of species.
(optional) default length(list.mat.dist).
An integer corresponding to the minimal number of distance matrices for
which each species should have values
(optional) default TRUE.
If TRUE, all given distance matrices will be normalized
(see Details)
(optional) default NULL.
A vector of double (between 0 and 1)
corresponding to the weights for each distance matrix provided in
list.mat.dist. They must sum up to 1.
A matrix containing the weighted (or not) combination of
the different transformed (or not) distance matrices given.
The information for the combination of all distances is written in
PRE_FATE_DOMINANT_speciesDistance.csv file.
This function allows to obtain a distance matrix between species, based on several dissimilarity distance matrices combined according to the following formula :
$$\text{mat.DIST} = \Sigma (\text{wei.i} * \text{mat.DIST}_{i})$$
If opt.normal = TRUE, two normalization steps are applied
to each distance matrix before combining them :
a non-paranormal (npn) transformation
(huge.npn function) to obtain Gaussian distributions
for all dissimilarity matrices used
a range normalization to bring the values back between
0 and 1 :
$$\text{mat.DIST}_{i} = \frac{\text{mat.DIST}_{i} - min(\text{mat.DIST}_{i})}{max(\text{mat.DIST}_{i}) - min(\text{mat.DIST}_{i})}$$
## Load example data
Champsaur_PFG = .loadData('Champsaur_PFG', 'RData')
## Species traits
tab.traits = Champsaur_PFG$sp.traits
tab.traits = tab.traits[, c('species', 'GROUP', 'MATURITY', 'LONGEVITY'
, 'HEIGHT', 'DISPERSAL', 'LIGHT', 'NITROGEN')]
str(tab.traits)
## Species niche overlap (dissimilarity distances)
DIST.overlap = Champsaur_PFG$mat.overlap
DIST.overlap[1:5, 1:5]
## Species functional distances (dissimilarity)
DIST.traits = PRE_FATE.speciesDistanceTraits(mat.traits = tab.traits
, opt.maxPercent.NA = 0.05
, opt.maxPercent.similarSpecies = 0.3
, opt.min.sd = 0.3)
DIST.traits$Chamaephyte[1:5, 1:5]
## Combine distances ---------------------------------------------------------
list.DIST = list(DIST.overlap, DIST.traits$Chamaephyte)
sp.DIST.n = PRE_FATE.speciesDistanceCombine(list.mat.dist = list.DIST
, opt.weights = c(0.2, 0.8))
sp.DIST.un = PRE_FATE.speciesDistanceCombine(list.mat.dist = list.DIST
, opt.norm = FALSE
, opt.weights = c(0.2, 0.8))
str(sp.DIST.n)
if (FALSE) { # \dontrun{
require(corrplot)
list.DIST = list(DIST.overlap, DIST.traits$Chamaephyte
, sp.DIST.un, sp.DIST.n)
names(list.DIST) = c('overlap', 'traits', 'un-normed', 'normed')
par(mfrow = c(2, 2))
for (li in 1:length(list.DIST))
{
tmp = list.DIST[[li]]
tmp = tmp[colnames(sp.DIST.n), colnames(sp.DIST.n)]
corrplot(tmp, method = 'shade'
, type = 'lower', cl.lim = c(0, 1)
, is.corr = FALSE, title = names(list.DIST)[li])
}
require(foreach); require(ggplot2); require(ggdendro)
hc = hclust(as.dist(sp.DIST.n))
pp = ggdendrogram(hc, rotate = TRUE) +
labs(title = 'Hierarchical clustering based on species distances')
plot(pp)
} # }