R/get_residual_cor.R
get_residual_cor.Rd
This function use coefficients \((\lambda_{jl}\) with \(j=1,\dots,n_{species}\) and \(l=1,\dots,n_{latent})\), corresponding to latent variables fitted using jSDM
package, to calculate the variance-covariance matrix which controls correlation between species.
get_residual_cor(mod, prob = 0.95, type = "mean")
An object of class "jSDM"
A numeric scalar in the interval \((0,1)\) giving the target probability coverage of the highest posterior density (HPD) intervals, by which to determine whether the correlations are "significant". Defaults to 0.95.
A choice of either the posterior median (type = "median"
) or posterior mean (type = "mean"
), which are then treated as estimates and the fitted values are calculated from.
Default is posterior mean.
results A list including :
Average over the MCMC samples of the variance-covariance matrix, if type = "mean"
.
Median over the MCMC samples of the variance-covariance matrix, if type = "median"
.
A \(n_{species} \times n_{species}\) matrix containing the lower limits of the (\(100 \times prob \%\)) HPD interval of variance-covariance matrices over the MCMC samples.
A \(n_{species} \times n_{species}\) matrix containing the upper limits of the (\(100 \times prob \%\)) HPD interval of variance-covariance matrices over the MCMC samples.
A \(n_{species} \times n_{species}\) matrix containing the value 1 corresponding to the “significant" co-variances and the value 0 corresponding to "non-significant" co-variances, whose (\(100 \times prob \%\)) HPD interval over the MCMC samples contain zero.
Average over the MCMC samples of the residual correlation matrix, if type = "mean"
.
Median over the MCMC samples of the residual correlation matrix, if type = "median"
.
A \(n_{species} \times n_{species}\) matrix containing the lower limits of the (\(100 \times prob \%\)) HPD interval of correlation matrices over the MCMC samples.
A \(n_{species} \times n_{species}\) matrix containing the upper limits of the (\(100 \times prob \%\)) HPD interval of correlation matrices over the MCMC samples.
A \(n_{species} \times n_{species}\) matrix containing the value \(1\) corresponding to the “significant" correlations and the value \(0\) corresponding to "non-significant" correlations, whose (\(100 \times prob \%\)) HPD interval over the MCMC samples contain zero.
After fitting the jSDM with latent variables, the fullspecies residual correlation matrix : \(R=(R_{ij})\) with \(i=1,\ldots, n_{species}\) and \(j=1,\ldots, n_{species}\) can be derived from the covariance in the latent variables such as : \(\Sigma_{ij}=\lambda_i' .\lambda_j\), in the case of a regression with probit, logit or poisson link function and
\(\Sigma_{ij}\) | \(= \lambda_i' .\lambda_j + V\) | if i=j |
\(= \lambda_i' .\lambda_j\) | else, |
, in the case of a linear regression with a response variable such as $$y_{ij} \sim \mathcal{N}(\theta_{ij}, V)$$. Then we compute correlations from covariances : $$R_{ij} = \frac{\Sigma_{ij}}{\sqrt{\Sigma_ii\Sigma _jj}}$$.
Hui FKC (2016). boral: Bayesian Ordination and Regression Analysis of Multivariate Abundance Data in R. Methods in Ecology and Evolution, 7, 744–750.
Ovaskainen and al. (2016). Using latent variable models to identify large networks of species-to-species associations at different spatial scales. Methods in Ecology and Evolution, 7, 549-555.
Pollock and al. (2014). Understanding co-occurrence by modelling species simultaneously with a Joint Species Distribution Model (JSDM). Methods in Ecology and Evolution, 5, 397-406.
library(jSDM)
# frogs data
data(frogs, package="jSDM")
# Arranging data
PA_frogs <- frogs[,4:12]
# Normalized continuous variables
Env_frogs <- cbind(scale(frogs[,1]),frogs[,2],scale(frogs[,3]))
colnames(Env_frogs) <- colnames(frogs[,1:3])
Env_frogs <- as.data.frame(Env_frogs)
# Parameter inference
# Increase the number of iterations to reach MCMC convergence
mod <- jSDM_binomial_probit(# Response variable
presence_data=PA_frogs,
# Explanatory variables
site_formula = ~.,
site_data = Env_frogs,
n_latent=2,
site_effect="random",
# Chains
burnin=100,
mcmc=100,
thin=1,
# Starting values
alpha_start=0,
beta_start=0,
lambda_start=0,
W_start=0,
V_alpha=1,
# Priors
shape=0.5, rate=0.0005,
mu_beta=0, V_beta=10,
mu_lambda=0, V_lambda=10,
# Various
seed=1234, verbose=1)
#>
#> Running the Gibbs sampler. It may be long, please keep cool :)
#>
#> **********:10.0%
#> **********:20.0%
#> **********:30.0%
#> **********:40.0%
#> **********:50.0%
#> **********:60.0%
#> **********:70.0%
#> **********:80.0%
#> **********:90.0%
#> **********:100.0%
# Calcul of residual correlation between species
result <- get_residual_cor(mod, prob=0.95, type="mean")
# Residual variance-covariance matrix
result$cov.mean
#> Species_1 Species_2 Species_3 Species_4 Species_5
#> Species_1 0.028882113 -0.007876548 0.08220407 0.18384722 -0.008027242
#> Species_2 -0.007876548 0.367200048 0.82346102 0.01437183 0.379920728
#> Species_3 0.082204070 0.823461025 3.35851878 1.71555853 1.038780519
#> Species_4 0.183847224 0.014371833 1.71555853 2.89713764 0.116703632
#> Species_5 -0.008027242 0.379920728 1.03878052 0.11670363 0.779678899
#> Species_6 -0.380756356 1.202075719 1.04783414 -4.01290508 1.801916199
#> Species_7 0.036067732 0.786743925 2.78418554 1.10364881 1.030318677
#> Species_8 0.123436037 -0.574512101 -1.31553173 0.76552561 -0.824785688
#> Species_9 -0.132111972 0.921254490 1.95813740 -1.01358309 1.423499077
#> Species_6 Species_7 Species_8 Species_9
#> Species_1 -0.3807564 0.03606773 0.1234360 -0.1321120
#> Species_2 1.2020757 0.78674392 -0.5745121 0.9212545
#> Species_3 1.0478341 2.78418554 -1.3155317 1.9581374
#> Species_4 -4.0129051 1.10364881 0.7655256 -1.0135831
#> Species_5 1.8019162 1.03031868 -0.8247857 1.4234991
#> Species_6 13.1758205 1.95292776 -4.8547877 6.5710279
#> Species_7 1.9529278 2.55760926 -1.4172273 2.1176346
#> Species_8 -4.8547877 -1.41722729 2.4062093 -2.9009848
#> Species_9 6.5710279 2.11763458 -2.9009848 4.3619161
## All non-significant co-variances are set to zero.
result$cov.mean * result$cov.sig
#> Species_1 Species_2 Species_3 Species_4 Species_5 Species_6
#> Species_1 0.02888211 0.0000000 0.08220407 0.1838472 0.0000000 -0.3807564
#> Species_2 0.00000000 0.3672000 0.00000000 0.0000000 0.0000000 0.0000000
#> Species_3 0.08220407 0.0000000 3.35851878 1.7155585 0.0000000 0.0000000
#> Species_4 0.18384722 0.0000000 1.71555853 2.8971376 0.0000000 -4.0129051
#> Species_5 0.00000000 0.0000000 0.00000000 0.0000000 0.7796789 0.0000000
#> Species_6 -0.38075636 0.0000000 0.00000000 -4.0129051 0.0000000 13.1758205
#> Species_7 0.00000000 0.7867439 2.78418554 1.1036488 0.0000000 0.0000000
#> Species_8 0.00000000 -0.5745121 0.00000000 0.0000000 0.0000000 -4.8547877
#> Species_9 -0.13211197 0.0000000 1.95813740 -1.0135831 0.0000000 6.5710279
#> Species_7 Species_8 Species_9
#> Species_1 0.0000000 0.0000000 -0.132112
#> Species_2 0.7867439 -0.5745121 0.000000
#> Species_3 2.7841855 0.0000000 1.958137
#> Species_4 1.1036488 0.0000000 -1.013583
#> Species_5 0.0000000 0.0000000 0.000000
#> Species_6 0.0000000 -4.8547877 6.571028
#> Species_7 2.5576093 -1.4172273 2.117635
#> Species_8 -1.4172273 2.4062093 -2.900985
#> Species_9 2.1176346 -2.9009848 4.361916
# Residual correlation matrix
result$cor.mean
#> Species_1 Species_2 Species_3 Species_4 Species_5 Species_6
#> Species_1 1.0000000 -0.1157971 0.3637104 0.94087340 -0.13250655 -0.8370988
#> Species_2 -0.1157971 1.0000000 0.7610563 0.11203435 0.73890184 0.5629379
#> Species_3 0.3637104 0.7610563 1.0000000 0.59980322 0.67624366 0.1655735
#> Species_4 0.9408734 0.1120343 0.5998032 1.00000000 0.09157163 -0.6619473
#> Species_5 -0.1325066 0.7389018 0.6762437 0.09157163 1.00000000 0.5139636
#> Species_6 -0.8370988 0.5629379 0.1655735 -0.66194732 0.51396360 1.0000000
#> Species_7 0.1865061 0.8292744 0.9651355 0.44529194 0.75206527 0.3439489
#> Species_8 0.5742103 -0.7128097 -0.5101016 0.33582752 -0.66523508 -0.8892333
#> Species_9 -0.6094296 0.7456521 0.4947718 -0.35521988 0.71225706 0.9091312
#> Species_7 Species_8 Species_9
#> Species_1 0.1865061 0.5742103 -0.6094296
#> Species_2 0.8292744 -0.7128097 0.7456521
#> Species_3 0.9651355 -0.5101016 0.4947718
#> Species_4 0.4452919 0.3358275 -0.3552199
#> Species_5 0.7520653 -0.6652351 0.7122571
#> Species_6 0.3439489 -0.8892333 0.9091312
#> Species_7 1.0000000 -0.6457012 0.6451034
#> Species_8 -0.6457012 1.0000000 -0.9473238
#> Species_9 0.6451034 -0.9473238 1.0000000
## All non-significant correlations are set to zero.
result$cor.mean * result$cor.sig
#> Species_1 Species_2 Species_3 Species_4 Species_5 Species_6
#> Species_1 1.0000000 0.0000000 0.3637104 0.9408734 0.0000000 -0.8370988
#> Species_2 0.0000000 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000
#> Species_3 0.3637104 0.0000000 1.0000000 0.5998032 0.0000000 0.0000000
#> Species_4 0.9408734 0.0000000 0.5998032 1.0000000 0.0000000 -0.6619473
#> Species_5 0.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000
#> Species_6 -0.8370988 0.0000000 0.0000000 -0.6619473 0.0000000 1.0000000
#> Species_7 0.0000000 0.8292744 0.9651355 0.4452919 0.7520653 0.0000000
#> Species_8 0.0000000 -0.7128097 -0.5101016 0.0000000 0.0000000 -0.8892333
#> Species_9 -0.6094296 0.7456521 0.4947718 -0.3552199 0.0000000 0.9091312
#> Species_7 Species_8 Species_9
#> Species_1 0.0000000 0.0000000 -0.6094296
#> Species_2 0.8292744 -0.7128097 0.7456521
#> Species_3 0.9651355 -0.5101016 0.4947718
#> Species_4 0.4452919 0.0000000 -0.3552199
#> Species_5 0.7520653 0.0000000 0.0000000
#> Species_6 0.0000000 -0.8892333 0.9091312
#> Species_7 1.0000000 -0.6457012 0.6451034
#> Species_8 -0.6457012 1.0000000 -0.9473238
#> Species_9 0.6451034 -0.9473238 1.0000000