[R] Checking for orthogonal contrasts
Ista Zahn
izahn at psych.rochester.edu
Fri Dec 3 16:06:01 CET 2010
Hi Steve,
The short answer is that there is typically no reason to check (beyond
looking to see what contrasts have been defined for the factors in the
model; see ?contrasts) because the rules are pretty simple.
1) the design matrix is orthogonal on the row-basis (i.e., the columns
sum to zero) when using contr.sum, contr.helmert, or manually set
contrasts in which the contrasts sum to zero.
2) if the design matrix is orthogonal and cell sizes are equal, the
model matrix will also be orthogonal.
However, you can of course check, as in the following example:
dat <- data.frame(y=rnorm(100), x=factor(rep(c("a", "a", "b", "c"), 25)))
m <- lm(y ~ x, data=dat)
## model matrix orthoganal on row-basis?
apply(model.matrix(m)[,grep("x", colnames(model.matrix(m)))], 2, sum)
## No, sum is non-zero
## design matrix orthoganal on row-basis?
apply(contrasts(dat$x), 2, sum) ## No, sum is non-zero
## change contrasts
contrasts(dat$x) <- contr.sum(n=3)
m2 <- lm(y ~ x, data=dat)
## model matrix orthoganal on row-basis?
apply(model.matrix(m2)[,grep("x", colnames(model.matrix(m2)))], 2,
sum) ## No, sum is non-zero (because of unbalenced cell sizes)
## model matrix orthoganal on row-basis?
apply(contrasts(dat$x), 2, sum) ## Yes, sum is zero.
HTH,
Ista
On Fri, Dec 3, 2010 at 8:47 AM, S Ellison <S.Ellison at lgc.co.uk> wrote:
>
> A common point made in discussion of contrasts, type I, II, III SS etc
> is that for sensible comparisons one should use contrasts that are
> 'orthogonal in the row-basis of the model matrix' (to quote from
> http://finzi.psych.upenn.edu/R/Rhelp02/archive/111550.html)
>
> Question: How would one check, in R, that this is so for a particular
> fitted linear model object?
>
> Steve Ellison
>
>
>
> *******************************************************************
> This email and any attachments are confidential. Any u...{{dropped:18}}
More information about the R-help
mailing list