residual correlation matrix from spOccupancy

30 views
Skip to first unread message

Claudio

unread,
Mar 24, 2025, 2:26:00 PMMar 24
to spOccupancy and spAbundance users
Hi all,
after fitting JSDM models with spOccupancy (sfJSDM and lfJSDM), I got the best model. Now I would like to obtain the (residual) correlation matrix in order to have infomation on co-occurrence patterns, but I do not know how to do this.
Thanks in advance for your help.

Jeffrey Doser

unread,
Mar 25, 2025, 7:49:11 AMMar 25
to spOccupancy and spAbundance users
Hi Claudio,

You can derive the residual correlation matrix from the latent factor loadings lambda, which is briefly described in the paper introducing this functionality. Here's some code to do that, assuming that the model output name is "out":

# out is the model object from the factor model run.
N <- nrow(out$y)
n.samples <- nrow(out$beta.samples)
cor.mat.samples <- array(NA, dim = c(n.samples, N, N))
n.factors <- out$q
for (l in 1:n.samples) {
  curr.lambda.mat <- matrix(out$lambda.samples[l, ], N, n.factors)
  cor.mat.samples[l, , ] <- cov2cor(curr.lambda.mat %*% t(curr.lambda.mat))
}
# Summarize cor.mat.samples just like any other set of MCMC samples, e.g.,
apply(cor.mat.samples, c(2, 3), mean)


Jeff

Claudio

unread,
Mar 25, 2025, 11:41:38 AMMar 25
to spOccupancy and spAbundance users
Great! Thanks.
Now I have just to understand better how it works.
Claudio
Reply all
Reply to author
Forward
0 new messages