[R] question about custom contrasts in ANOVA

From: Scot W McNary <smcnary_at_charm.net>
Date: Thu 25 Aug 2005 - 13:43:48 EST


I have a problem in which I have test score data on students from a number of schools. In each school I have a measure of whether or not they received special programming. I am interested in the interaction between school and attendance to the programming, but in a very select set of comparisons. I'd like to cast the test as one in which students in each school who attend are compared with students who don't across all schools. So, I would be comparing school 1 attenders with school 1 non-attenders, school 2 attenders with school 2 non-attenders, etc. The reason for the custom contrast is that the between school comparisons (e.g., school 1 attenders vs. school 2 non-attenders) are of less interest.

This seems to require a custom contrast statement for the interaction term. I have a toy example that seems to work as it should, but wonder if I've correctly created the contrast needed.

Here is a toy example (code put together from bits taken from MASS ch 6, and various R-help postings, (e.g.,

# toy interaction contrast example, 10 schools, 100 kids, 5 attenders (1)
# and 5 non-attenders (2) in each school

# make the data

school <- gl(10, 10)
attend <- gl(2, 5, 100)
# creates an interaction with schools 6 and 7
y <- c(sample(seq(450, 650, 1), 50), rep(c(rep(650, 5), rep(450, 5)), 2),

         sample(seq(450, 650, 1), 30))

# anova

summary(aov(y ~ school * attend))

# graphically

Means <- tapply(y, list(school, attend), mean)

plot(Means[,1], col="red", type = "l", ylim = c(400,700))

points(Means[,2], col="blue", type = "l")

# create contrasts for hypothesis of interest
# school i attend j - school i attend j'
# for all schools

sxa <- interaction(school, attend)
sxam <- as.matrix(rbind(diag(1,10), diag(1,10) * -1)) contrasts(sxa) <- sxam

summary(aov(y ~ sxa), split=list(sxa=1:10), expand.split = T)

The actual problem has a few more schools, other covariates, considerably more students, and is somewhat unbalanced.



   Scot W. McNary  email:smcnary@charm.net

R-help@stat.math.ethz.ch mailing list
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Thu Aug 25 13:52:15 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:39:55 EST