[R-lang] contrast coding

T. Florian Jaeger tiflo at csli.stanford.edu
Mon Sep 8 13:54:00 PDT 2008


Hey Bob,

yes, if you sum-code (aka contrast code) the main effects, the way you
describe it below, you will be all set (at least for balanced data; for
unbalanced data, you may want to center the variable). The interaction just
multiplies the value of the two main predictors (leading to the values you
give below), and -for balanced data- the interaction should be orthogonal to
the main effects. The fitted coefficients will indeed differ from the
default dummy coding (which is 0 vs. 1 coding, which is *not* centered).

HTH,
Florian

On Fri, Sep 5, 2008 at 4:27 PM, Bob Slevc <slevc at rice.edu> wrote:

> Hi there R-language-gurus,
>
> I have what I think is a simple question – maybe even a stupid question
> (and there are too stupid questions) – that's related to recent discussions
> on this list.  Imagine, if you will, that I have a full-factorial design,
> and want to set up a set of orthogonal contrasts rather than using R's
> default dummy coding.  For a simple 2x2 design, I want something like this,
> where contrasts 1 and 2 are the main effects for A and B, and contrast 3 is
> the interaction:
>
> a1 a1 a2 a2
> b1 b2 b1 b2
> contrast1 1 1 -1 -1
> contrast2 1 -1 1 -1
> contrast3 1 -1 -1 1
>
> I haven't found any contrast function (e.g., contr.poly / contr.sum / etc.)
> that'll automatically create a matrix for this kind of contrast, but can I
> just specify the individual factor contrasts and assume that R will just
> multiply them to give nice orthogonal interaction contrasts?  For example,
> if my factors are called A and B, and I say:
>
> contrasts(datafile$A) <- c(1,-1)
> contrasts(datafile$B) <- c(1,-1)
>
> and then run a model (on log(RTs) apparently):
>
> model <- lmer(log(RT) ~ A*B + (1|subj) + (1|item), data=datafile)
>
> Then am I set?  I'm a little unsure, partially because it gives me slightly
> different results than the default dummy coding does (though it does seem to
> be orthogonal as the correlations between fixed effects are all zero...)
>
> Thanks much,
> Bob
>
> ---
> L. Robert (Bob) Slevc, Ph.D.
> Rice University, Dept. of Psychology • 6100 Main Street • Houston, TX 77005
> http://www.ruf.rice.edu/~slevc/ <http://www.ruf.rice.edu/%7Eslevc/>
>
>
> _______________________________________________
> R-lang mailing list
> R-lang at ling.ucsd.edu
> http://pidgin.ucsd.edu/mailman/listinfo/r-lang
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://pidgin.ucsd.edu/pipermail/r-lang/attachments/20080908/1a7e9c31/attachment.htm 


More information about the R-lang mailing list