# [R] Comparison of age categories using contrasts

Dylan Beaudette dylan.beaudette at gmail.com
Tue Feb 17 04:18:41 CET 2009

```On Mon, Feb 16, 2009 at 5:28 PM, Patrick Giraudoux
<patrick.giraudoux at univ-fcomte.fr> wrote:
> Greg Snow a écrit :
>> One approach is to create your own contrasts matrix:
>>
>>
>>> mycmat <- diag(8)
>>> mycmat[ row(mycmat) == col(mycmat) + 1 ] <- -1
>>> mycmati <- solve(mycmat)
>>> contrasts(agefactor) <- mycmati[,-1]
>>>
>>
>> Now when you use agefactor, the intercept will be the first age group and the slopes will be the differences between the pairs of groups (make sure that the order of the levels of agefactor is correct).
>>
>> The difference between this method and the contr.sdif function in MASS is how the intercept will end up being interpreted (and the dimnames).
>>
>> Hope this helps,
>>
>>
>
> Actually, exactly what I needed including the reference to contr.sdif in
> MASS I did not spot before (although I am a faithful reader of the
> yellow book... but so many things still escape to me). Again thanks a lot.
>
> Patrick
>

Glad you were able to solve your problem. Frank replied earlier with
the suggestion to use contrast.Design() to perform the tests after the
initial model has been fit. Perhaps I am a little denser than normal,
but I cannot figure out how to apply contrast.Design() to a similar
model with several levels of a factor. Example data:

# need these
library(lattice)
library(Design)

# replicate an important experimental dataset
set.seed(10101010)
x <- rnorm(100)
y1 <- x[1:25] * 2 + rnorm(25, mean=1)
y2 <- x[26:50] * 2.6 + rnorm(25, mean=1.5)
y3 <- x[51:75] * 2.9 + rnorm(25, mean=5)
y4 <- x[76:100] * 3.5 + rnorm(25, mean=5.5)

d <- data.frame(x=x, y=c(y1,y2,y3,y4), f=factor(rep(letters[1:4], each=25)))

# plot
xyplot(y ~ x, groups=f, data=d, auto.key=list(columns=4, title='Beard
Type', lines=TRUE, points=FALSE, cex=0.75), type=c('p','r'),
ylab='Number of Pirates', xlab='Distance from Land')

# standard comparison to base level of f
summary(lm(y ~ x * f, data=d))

# compare to level 4 of f
summary(lm(y ~ x * C(f, base=4), data=d))

# now try with contrast.Design():
l <- ols(y ~ x * f, data=d)

# probably the wrong approach, as the results do not look too familiar:
contrast(l, list(f=levels(d\$f)))

x          f Contrast  S.E.      Lower       Upper     t     Pr(>|t|)
-0.3449623 a 0.3966682 0.1961059 0.007184856 0.7861515  2.02 0.0460
-0.3449623 b 0.5587395 0.1889173 0.183533422 0.9339456  2.96 0.0039
-0.3449623 c 4.1511677 0.1889170 3.775962254 4.5263730 21.97 0.0000
-0.3449623 d 4.3510407 0.1888820 3.975904726 4.7261766 23.04 0.0000

This is adjusting the output to a given value of 'x'... Am I trying to
use contrast.Design() for something that it was not intended for? Any
tips Frank?

Cheers,
Dylan

```