[R] question about custom contrasts in ANOVA

Scot W McNary smcnary at charm.net
Thu Aug 25 05:43:48 CEST 2005


Hi,

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., 
http://finzi.psych.upenn.edu/R/Rhelp02a/archive/49077.html)):

# 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.

Thanks,

Scot


--
   Scot W. McNary  email:smcnary at charm.net




More information about the R-help mailing list