Skip to content

Anova and unbalanced designs

5 messages · Tal Galili, John Fox, Greg Snow

#
Dear Tal,
SS
the
The computational approach taken in Anova() makes it simpler to include the
intercept in the "type-III" tests and not to include it in the "type-II"
tests.
The test for the intercept is rarely of interest. A "type-II" test for the
intercept would test that the unconditional mean of the response is 0; a
"type-III" test for the intercept would test that the constant term in the
full model fit to the data is 0. The latter depends upon the parametrization
of the model (in the case of an ANOVA model, what kind of "contrasts" are
used). You state that the example that you give is taken from ?Anova but
there's a crucial detail that's omitted: The help file only gives the
"type-II" tests; the "type-III" tests are also reasonable here, but they
depend upon having used "contr.sum" (or another set of contrasts that's
orthogonal in the row basis of the model matrix) for the between-subject
factors, treatment and gender. This detail is in the data set:
[1] M M M F F M M F F M M M F F F F
attr(,"contrasts")
[1] contr.sum
Levels: F M
[1] control control control control control A       A       A       A
B       B      
[12] B       B       B       B       B      
attr(,"contrasts")
        [,1] [,2]
control   -2    0
A          1   -1
B          1    1
Levels: control A B

With proper contrast coding, the "type-III" test for the intercept tests
that the mean of the cell means (the "grand mean") is 0.

Had the default dummy-coded contrasts (from contr.treatment) been used, the
tests would not have tested reasonable hypotheses. My advice, from the help
file: "Be very careful in formulating the model for type-III tests, or the
hypotheses tested will not make sense."

I hope this helps,
 John
a
balanced,
treatment*gender,
*
***
.
***
***
.
.
***
.
***
coding
SS
reasonable
for
SPSS!
the
5.121e-
Entr.B
#
Dear Tal,

A complete explanation of this issue is too long for an email; I do address
it in my Applied Regression Analysis and Generalized Linear Models text. The
question seems to come up so frequently that Georges Monette and I are
writing a paper on it (and related issues) for the upcoming useR conference.

Briefly, any set of "contrasts" that are orthogonal in the row basis of the
model matrix (essentially, composed from what you see when you use the
contrasts() function in R) will produce the same sums of squares (or, in the
multivariate case, sums of squares and products). This include Hermert
(contr.helmert), sigma-constrained (contr.sum), and orthogonal-polynomial
(contr.poly) contrasts, but not dummy-coded "contrasts" (contr.treatment).
(Actually, if you look carefully, you'll see that the contrasts defined for
treatment in the OBrienKaiser data are custom orthogonal contrasts similar
to Helmert contrasts.) Consequently, if all you're concerned with is the
ANOVA table, it doesn't matter which of these you use. If, however, you're
interested in the individual contrasts, it does of course matter which you
use, and in particular the orthogonal polynomial contrasts are not sensible
if the levels of the factor aren't ordered.

Regards,
 John

------------------------------
John Fox, Professor
Department of Sociology
McMaster University
Hamilton, Ontario, Canada
web: socserv.mcmaster.ca/jfox
differently
this
mean
"III"))
for
end
type
include
"type-
be
for
0;
in
"contrasts"
the
that's
A
used,
the
or
experiment
command).
wrote:
contrast
As
contr.helmert(),
to
got
'
'
look
means
parametrizations:
Anova(betweenanova,
den
8
8
8
8
'
Farimagsgade
1 day later
#
Tal,

The reparametirized cell means model can help with the understanding of what the individual terms mean in an analysis with contrasts.  The cell means model is y=W*mu + e, where mu is the vector of the cell means (the mean for each group) and W is just a stretched identity matrix, this model just fits the means of each cell without any comparisons/contrasts.  The reparameterized cell means model is y=W*Ai * A*mu + e, where Ai and A are inverses of each other and determine the set of contrasts (I tend to think of A as the contrast matrix and Ai as the dummy variable encoding matrix, but in some cases Ai is called the contrast matrix).  Basically this leads to x=W*Ai and beta= A*mu for the standard regression model of y=x*beta+e, so R is creating x for you and we just need to find A (the inverse of Ai) to see what beta really means.

We can start by creating some labels (assuming 5 groups for example) and loading the MASS package for some prettiness later:

library(MASS)

betas <- paste('b',0:4, sep='' )
mus <- paste('mu',1:5, sep='' )


Now, let's look at what Helmert contrasts give us:


Ai1 <- cbind(1, contr.helmert(5))
A1 <- solve(Ai1)

A1txt <- as.character(fractions(A1))

paste( betas, '=', apply(A1txt, 1, paste, mus, sep='*', collapse=' + '))

So beta0 (the intercept) is just the mean of the 5 groups, beta1 compares the first group to the second (actually half the first to half the second, this matters for interpreting the beta or confidence intervals, but not hypothesis tests).
Then beta2 compares the average of the first 2 groups to the 3rd group (with an extra 1/3rd in there, this makes the original Ai matrix and x matrix prettier).  Beta3 and beta4 compare groups 4 and 5 to the mean of the previous ones respectively.

Now look at summation contrasts (the contrasts sum to 0)

Ai2 <- cbind(1, contr.sum(5))
A2 <- solve(Ai2)
dimnames(A2) <- list(betas,mus)

fractions(A2)

The beta0 coefficient is still the overall mean and with a little algebra it can be seen that the other rows/betas measure the difference between the cell means (except the last) and the overall mean (just replace 4/5 with 1-1/5).

Now for the non-orthogonal treatment contrasts:

Ai3 <- cbind(1, contr.treatment(5))
A3 <- solve(Ai3)
dimnames(A3) <- list(betas, mus)
fractions(A3)

Now beta0 is not a mean of all the groups, but the mean of the first (reference) group.  The other betas are then the differences between the other groups and the reference group.


Polynomial contrasts are a bit more difficult to interpret:

Ai4 <- cbind(1, contr.poly(5))
A4 <- solve(Ai4)
dimnames(A4) <- list(betas, mus)
zapsmall(A4)
matplot(1:5, t(A4), type='b')

The graph is probably the easiest to interpret, the intercept is still the overall mean, beta1 shows a linear relationship, beta2 follows a quadratic, etc.  These are only meaningful if the groups are ordered and the same distance apart.

We can use the same idea in reverse to create our own contrasts, suppose we want to compare group 1 (control) to the mean of the rest, then compare groups 2 and 3, compare groups 4 and 5, then compare the mean of groups 2 and 3 to the mean of groups 4 and 5, we can do either of the following (depending on what we want beta0 to mean):


A6 <- rbind(1/5,
                c(-4,  1,  1,  1,  1)/4,
                c( 0, -1,  1,  0,  0),
                c( 0,  0,  0, -1,  1),
                c( 0, -1, -1,  1,  1)/2 )
fractions(A6)
zapsmall(solve(A6))

A7 <- rbind(c(1, 0,  0,  0,  0),
                c(-4,  1,  1,  1,  1)/4,
                c( 0, -1,  1,  0,  0),
                c( 0,  0,  0, -1,  1),
                c( 0, -1, -1,  1,  1)/2 )
fractions(A7)
zapsmall(solve(A7))


Just use the above Ai matricies to create x (use the C (note capital) or contrasts functions) and the individual terms will have the desired interpretations.

Hope this helps,



--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111