Search code examples
rlme4mixed-modelslsmeans

Error in lmer model and lsmeans output


I'm running an LME model with the lme4 package and then following up with pairwise comparisons using the lsmeans package.

Here is my code:

lmer_full <- lmer (VOT ~ Place*Laryngeal + (1+Place+Laryngeal|Sp), 
    data = LME,control=lmerControl(optCtrl=list(maxfun=50000)))

lsmeans (lmer_full, pairwise~Laryngeal|Place)

However, I get the following error message after running the lmer:

fixed-effect model matrix is rank deficient so dropping 1 column / coefficient
Warning messages:
1: In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : unable to evaluate scaled gradient
2: In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model failed to converge: degenerate Hessian with 1 negative eigenvalues

Then another error after running lsmeans:

Error in base::chol2inv(x, ...) : 'a' must be a numeric matrix

Here is the structure of my data:

data.structure

I would really appreciate if someone can tell me what's wrong with the model.


Solution

  • tl;dr I can't exactly reproduce your error, but I can come pretty close. Your data set is most likely too small/noisy for the model you want to fit (you're getting a singular model); using the emmeans package (which is the successor to lsmeans) will help a bit, but you should probably simplify your model.

    1. Starting with a large, fully crossed data set:
    library(lme4)
    library(emmeans)
    dd <- expand.grid(Place=factor(1:3),Laryngeal=factor(1:3),
                      Sp=factor(1:10),rep=6)
    set.seed(101)
    dd$y <- rnorm(nrow(dd))
    
    This works fine:
    
    m1 <- lmer(y~Place*Laryngeal + (1+Place+Laryngeal|Sp), dd)
    emmeans(m1,pairwise~Laryngeal|Place)  ## lsmeans() also works
    
    1. Reducing the data set (to drop one combination of levels) causes the "fixed-effect model matrix is rank deficient" message, but everything works otherwise:
    dd_missing <- subset(dd,!(Place=="2" & Laryngeal=="2"))
    m2 <- update(m1, data=dd_missing)
    emmeans(m2,pairwise~Laryngeal|Place) ## lsmeans() also works
    
    1. If we randomly subsample a small fraction of the data we can get the error, although I couldn't do it with my data without also telling lmer to ignore some other issues with the data set (not enough samples for the number of random effects specified):
    set.seed(102)
    dd_small <- dd_missing[sample(1:nrow(dd_missing),
                                  size=round(nrow(dd_missing)*0.3),
                                  replace=FALSE),]
    m3 <- update(m1, data=dd_small,
                 control=lmerControl(check.nobs.vs.nlev="ignore",
                                     check.nobs.vs.nRE="ignore",
                                     optCtrl=list(maxfun=50000)))
    
    
    emmeans(m3,pairwise~Laryngeal|Place)  ## works (sort of)
    lsmeans::lsmeans(m3,pairwise~Laryngeal|Place)  ## fails
    
    1. Finally, simplifying the model makes things a little better:
    m4 <- update(m3, . ~ Place*Laryngeal + (1+Place|Sp))
    emmeans(m4,pairwise~Laryngeal|Place)
    lsmeans::lsmeans(m4,pairwise~Laryngeal|Place)