Skip to content

Environmental correlation in gllvm #117

Answered by fhui28
Leitemfa asked this question in Q&A
Discussion options

You must be logged in to vote

Here is a worked example based on the first example in the gllvm package.

library(gllvm)
library(tidyverse)

data(microbialdata)
X <- microbialdata$Xenv
y <- microbialdata$Y[, order(colMeans(microbialdata$Y > 0), decreasing = TRUE)[21:40]]
fit <- gllvm(y, X, formula = ~ pH + Phosp, family = poisson())
fit$logL

# Residual correlation
getResidualCor.gllvm(fit)

# Environmental correlation
modelmat <- model.matrix(fit$formula, data = X) %>% 
   as.matrix %>% 
   {.[,-1]} # Remove intercept
linpred <- tcrossprod(modelmat, fit$params$Xcoef)
envircor <- cov2cor(cov(linpred))

There might well be an easier way to do this [not up to date with the updates!], but I did not spot it.

Good luck!

Replies: 6 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by BertvanderVeen
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
3 participants
Converted from issue

This discussion was converted from issue #61 on June 02, 2023 13:54.