[R] Factor level comparisons in lme

Douglas Bates bates at stat.wisc.edu
Mon Feb 10 17:11:06 CET 2003

Martin Biuw <emb7 at st-andrews.ac.uk> writes:

> Hello,
> I'm trying to fit a linear mixed effects model of the form:
> lme(y ~ x * Sex * Year, random=x|subject)

Did you mean 

lme(y ~ x * Sex * Year, random= ~ x|subject)

The random argument should be a formula or a list.

> where Sex and Year are factors with two and three levels respectively. I 
> want to compare the fixed effects for each level to the overall mean, but 
> the default in R is to compare to the first level. This can be changed by 
> adding the term -1 to the righthand side of the model formula. But what I 
> can't figure out is how to do this for both factors simultaneously. If I 
> specify the model as:
> lme(y ~ x * Sex * Year-1, random=x|subject)
> the output gives me the fixed effects for each level of "Sex" compared to 
> the overall mean, but still only gives me the effects of the second two 
> levels in the "Year" factor compared to the first level. How do I specify 
> the fixed effects structure to allow comparisons to the overall mean for 
> each level of both factors?

This isn't really an lme question - it is a question about the
parameterization used in the model matrix for a linear model formula.

I'm not sure what you mean by "allow comparisons to the overall
mean".  You may find that setting

 options(contrasts=c('contr.sum', 'contr.poly'))

does what you want or what you want to do may be impossible.  With two
levels of Sex and, say, 4 levels of year, the number of degrees of
freedom in the crossed factors plus intercept is
  1(constant) + 1(Sex) + 3(Year) + 3(Sex * Year) = 8
If you want to get separate means for each level of Sex and Year and
Sex*Year you would need 
  2(Sex) + 4(Year) + 8(Sex*Year) = 14 
degrees of freedom.

More information about the R-help mailing list