I have a list of formulas (> 10,000) for linear mixed models (lme4) that I fitted to a data set. Successfully, I have used lapply() and a custom function that incorporated tryCatch() to fit these models. Now I would like to extract the P-values and lsmeans for all of these models. I have successfully extracted the P-values, but the lsmeans function is encountering errors.
library(lme4)
library(lmerTest)
library(pbkrtest)
library(lsmeans)
formulaS <- list() #Not going to detail generation of list, generically: 'Yvar~X1*X2+(1|subject)'
dataSET <- X #dataframe with first 3 columns containing fixed and random factors,
# as well as >10,000 columns of variables of interest
modelSeq <- function (x, dat) {
return(tryCatch(lmer(x, data = dat), error=function(e) NULL))
}
modelsOutput <- lapply(formulaS, function(x) modelSeq(x, dat = dataSET))
lsmeans(modelsOutput[[1]], pairwise ~ X1:X2) #recieves error
Error in solve.default(L %% V0 %% t(L), L) : Lapack routine dgesv: system is exactly singular: U[1,1] = 0
The reason I don't think it's a model problem is that if I fit the models individually I can extract the lsmeans just fine. Is there any commentary on 1) why I cannot extract lsmeans, 2) how to efficiently extract means, or 3) an alternative, efficient method.
Thanks!
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
UPDATE & EDIT: This is RNAseq data with repeated samples of subjects over time I am playing with, so the >10,000 models have the same fixed and random effects that describe the experimental design. The response (a gene) is the only variable that varies. I have tried to make that more explicit in the code below. Recognizing that a mixed model with an identity link might not be ideal for the data, I have the new wrapper below. I'm still having issues extracting means. Also, any commentary on more appropriate, time-efficient methods for computing P-values is appreciated.
library(lme4)
library(blmeco)
library(ggeffects)
formulaS <- list() #Not going to detail generation of list, generically: 'GeneI~TRT*TIME+(1|subject)'
dataSET <- X #dataframe with first 3 columns containing fixed and random factors,
# as well as >10,000 columns of variables of interest (gene TPM)
wrap.glmer.nb <- function (modelForm, dat) {
m <- tryCatch(glmer.nb(formula = modelForm, data = dat), error = function(e) NULL)
if (!is.null(m)) {
m.disp <- tryCatch(dispersion_glmer(m), error = function(e) NULL)
m.wald <- tryCatch(anova(m), error = function(e) NULL)
m.means.c <- tryCatch(ggemmeans(model = m, terms = c('TRT')), error = function(e) NULL)
m.means.e <- tryCatch(ggemmeans(model = m, terms = c('TIME')), error = function(e) NULL)
m.means.cxe <- tryCatch(ggemmeans(model = m, terms = c('TRT', 'TIME')), error = function(e) NULL)
x <- list(m.disp, m.wald, m.means.c, m.means.e, m.means.cxe)
print(paste0('Done with a model at ', Sys.time()))
return(x)
} else{
x <- m
return(x)
}
}
startTime <- Sys.time()
modelOUTPUTS <- lapply(formulaS, function(modelForm) wrap.glmer.nb(modelForm, dat = dataSET))
endTime <- Sys.time()
print(paste('Victory! The analysis took:', endTime - startTime))