[R-sig-ME] glmer error: contrasts can only be applied to factors of 2 or more levels

Ben Bolker bbolker at gmail.com
Mon Sep 1 23:36:28 CEST 2014


Dan McCloy <drmccloy at ...> writes:

> 
> I'm getting the following error, despite the fact that all my fixed
> effects were converted to factors and had their contrast attributes
> set prior to calling glmer.
> 
> Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
>   contrasts can be applied only to factors with 2 or more levels
> Calls: glmer ... model.matrix -> model.matrix.default -> contrasts<-
> 
> Here is the model specification:
> glmer(press ~ truth*adj*idn*num + (1|subj), data=wl,
> family=binomial(link="probit"))
> 
> Here are the predictors. As you can see, they are all already factors
> and have contrasts set, so I can't figure out why glmer is trying to
> set contrasts anyway, much less why it thinks any of these have fewer
> than two levels:
> 
> R> head(wl$truth)
> [1] neither neither neither neither neither neither
> attr(,"contrasts")
>         target foil
> neither      0    0
> target       1    0
> foil         0    1
> Levels: neither target foil
> R> head(wl$adj)
> [1] TRUE TRUE TRUE TRUE TRUE TRUE
> attr(,"contrasts")
>       TRUE
> TRUE     1
> FALSE   -1
> Levels: TRUE FALSE
> R> head(wl$idn)
> [1] FALSE FALSE FALSE FALSE FALSE FALSE
> attr(,"contrasts")
>       TRUE
> TRUE     1
> FALSE   -1
> Levels: TRUE FALSE
> R> head(wl$num)
> [1] six six six six six six
> attr(,"contrasts")
>       three
> three     1
> six      -1
> Levels: three six
> 
> Here is the session info:
> R version 3.1.1 (2014-07-10)
> Platform: x86_64-pc-linux-gnu (64-bit)

[snip]

> other attached packages:
> [1] lme4_1.1-7   Rcpp_0.11.2  Matrix_1.1-4
> 

[snip]

> Can anybody shed some light on what is going on here, and / or how to
> work around it?  I've run similar models with very similar data on
> slightly older versions of lme4 (but still >1.0) and never run into
> this, so I'm wondering if a bug was introduced in the latest version.

Unfortunately it's really hard to say without a reproducible example.
I haven't encountered anything like this before and don't
see anything obviously wrong either (which is about the only
way that it is *ever* possible to debug without a reproducible example ...)
A couple of semi-wild guesses:

* some of your factors despite having >1 level may only have a single
level _represented_
* you may have a pattern of NA values that leads to the reduced (non-NA)
data set having only a single factor level within it.

What are the results of 

fvars <- c("truth","adj","idn","num")
lapply(na.omit(wl)[,fvars], table)?

(the cross-classification would be useful too)
?

It would be great if you could come up with a reproducible example.



More information about the R-sig-mixed-models mailing list