# [R] aov contrasts residual error calculation

Steven Lacey slacey at umich.edu
Fri Apr 21 16:06:59 CEST 2006

```Hi,

I am using aov with an Error component to model some repeated measures data.
By repeated measures I mean the data look something like this...

subj    A        B        C
1       4        11       15
2       3        12       17
3       5        9        14
4       6        10       18

For each subject I have 3 observations, one in each of three conditions (A,
B, C). I want to test the following contrast (1, 0, -1). One solution is to
apply the contrast weights at the subject level explicitly and then call
t.test on the difference scores. However, I am looking for a more robust
solution as I my actual design has more within-subjects factors and one or
more between subjects factors.

A better solution is to specify the contrast in an argument to aov. The
estimated difference of the contrast is the same as that in the paired
t-test, but the residual df are double. While not what I expected, it
follows from the documentation, which explicitly states that these contrasts
are not to be used for any error term. Even though I specify 1 contrast,
there are 2 df for a 3 level factor, and I suspect internally the error term
is calculated by pooling across multiple contrasts.

While very useful, I am wondering if there is way to get aov to calculate
the residual error term only based on the specified contrasts (i.e., not
assume homogeneity of variance and sphericity) for that strata?

If not, I could calculate them directly using model.matrix, but I've never
done that. If that is the preferred solution, I'd also appreciate coding
suggestions to do it efficiently.

How would I do the same thing with a two factor anova where one factor is
within-subjects and one is between...
Condition
Mapping     Subject        A        B        C
1            1             4        11       15
1            2
1            3
1            4
1            5
1            6
1            7
1            8
2            9
2            10

Mapping is a between-subject factor. Condition is a within-subject factor.
There are 5 levels of mapping, 8 subjects nested in each level of mapping.
For each of the 40 combinations of mapping and subject there are 3
observations, one in each level of the condition factor.

I want to estimate the pooled error associated with the following set of 4
orthogonal contrasts:

condition.L:mapping.L
condition.L:mapping.Q
condition.L:mapping.C
condition.L:mapping^4

What is the best way to do this? One way is to estimate the linear contrast
for condition for each subject, create a 40 row matrix where the measure for
each combination of mapping and subject is the linear contrast on condition.
If I pass this dataframe to aov, the mse it returns is the value I am
looking for.

If possible, I would like to obtain the estimate without collapsing the
dataframe, but am not sure how to proceed. Suggestions?

Thanks,
Steve

```