Skip to content
Prev 243846 / 398506 Next

Checking for orthogonal contrasts

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: