[Rd] lm with factors for which only one level occurs

Gabor Grothendieck ggrothendieck at gmail.com
Sat Feb 9 04:54:07 CET 2008


Consider the following:

> lm(conc ~ Type, CO2, subset = Plant == "Quebec")
Error in `contrasts<-`(`*tmp*`, value = "contr.treatment") :
  contrasts can be applied only to factors with 2 or more levels

Here Type is a factor for which only one level occurs
within the "Quebec" subset.   This is something that
one would think would be possible to handle in lm.  (Note
that singular.ok = TRUE is the default.)

I came across this when automatically performing lm's
on the levels of a conditioning variable, such as Plant in the above
example, and found I had to unexpectedly special case situations
like the above.

lmList() in lme4 is likely adversely affected by this as well.



More information about the R-devel mailing list