[R-lang] question about model interpretation using lmer

Nathaniel Smith njs at pobox.com
Thu Apr 24 17:05:02 PDT 2008


On Wed, Apr 23, 2008 at 07:59:22PM -0400, Austin Frank wrote:
> > is it possible to specify which level of a factor is withheld in lmer?
> 
> Sure.  This
> 
> --8<---------------cut here---------------start------------->8---
> data$factor1 <- relevel(data$factor1, ref="none")
> --8<---------------cut here---------------end--------------->8---
> 
> will change the factor so that the level coded as "none" is the
> reference level.  If you care about the ordering of the levels (this can
> be important for graphing the results, for example), you can try
> 
> --8<---------------cut here---------------start------------->8---
> data$factor1 <- factor(data$factor1,
>                        levels=c("none", "vowel", "vce", "vceless", "son"))
> --8<---------------cut here---------------end--------------->8---
> 
> This will place the levels of the factor in that specific order.  If you
> don't do this, R defaults to ordering the levels of the factor
> alphabetically.  Even if you use relevel(), R will still order the
> remaining levels alphabetically.

In addition to reordering the levels of your factor, you can also
explicitly tell contr.treatment which level to use as the "base".
Unfortunately, it wants to know the *index* of the level, not its
name, so if "none" is the third level, you can use
  factor1 <- C(factor1, base=3)  # Set it permanently
  lm(y ~ C(factor1, base=3))  # Use it for just this fit
or whatever.

Stupid R tricks:

  my.contr.treatment <- function(n, base=1, contrasts=TRUE, ref)
  {
    if (!missing(ref)) base <- match(ref, levels(n))
    contr.treatment(n, base=base, contrasts=contrasts)
  }
  lm(y ~ C(factor1, my.contr.treatment, ref="none")

-- Nathaniel

-- 
Electrons find their paths in subtle ways.



More information about the R-lang mailing list