Nested ANOVA: Example
3 cages
4 mosquitoes within each cage
2 independent measurements per mosquito
Cage I
1
Cage II
58.5 77.8 84.0 70.1
59.5 80.9 83.6 68.3
Cage III
4
69.8 56.0 50.7 63.8
69.8 54.5 49.3 65.8
56.6 77.8 69.9 62.1
57.5 79.2 69.2 64.5
Nested ANOVA: models
Yijk = + i + ij + ijk
= overall mean
i = effect for ith cage
ij = effect for jth mosquito within ith cage
ijk = random error
Random effects model
i Normal(0, A2 )
Mixed effects model
i fixed;
i = 0
ij Normal(0, B2 |A)
ij Normal(0, B2 |A)
ijk Normal(0, 2)
ijk Normal(0, 2)
The model
40
50
60
70
80
90
40
50
60
Cages
30
20
10
10
20
30
30
20
10
Individuals
30
20
10
70
80
90
100
Cages
10
20
30
10
20
30
Individuals
10
20
30
30
20
Residuals
10
Residuals
Example: sample means
Cage I
1
ij
Y
i
Y
Y
Cage II
4
Cage III
4
58.5 77.8 84.0 70.1
59.5 80.9 83.6 68.3
69.8 56.0 50.7 63.8
69.8 54.5 49.3 65.8
56.6 77.8 69.9 62.1
57.5 79.2 69.2 64.5
59.00
69.80
57.05
79.35
83.80
72.84
69.20
55.25
50.00
59.96
66.63
64.80
78.50
69.55
67.10
63.30
Calculations (equal sample sizes)
Source
Sum of squares
among groups
SSamong = bn
subgroups within groups
SSsubgr = n
within subgroups
SSwithin =
TOTAL
PPP
i
df
P
2
i (Yi Y )
PP
2
i
j (Yij Yi )
PPP
i
k (Yijk
k (Yijk
ij)2
Y
)2
Y
a1
a (b 1)
a b (n 1)
abn1
ANOVA table
SS
df
MS
expected MS
SSamong
a1
SSamong
a1
MSamong
MSsubgr
2 + n B2 |A + n b A2
SSsubgr
a (b 1)
SSsubgr
a(b 1)
MSsubgr
MSwithin
2 + n B2 |A
SSwithin
a b (n 1)
SSwithin
ab(n 1)
SStotal
abn1
Example
source
df
SS
MS
P-value
among groups
665.68
332.84
1.74
0.23
among subgroups within groups
1720.68
191.19
146.88
< 0.001
within subgroups
12
15.62
1.30
TOTAL
23
2401.97
[Link] <- aov(length cage / individual, data=mosq)
summary([Link])
Variance components
Within subgroups (error; between measurements on each female)
s2 = MSwithin = 1.30
s=
1.30 = 1.14
Among subgroups within groups (among females within cages)
s2B|A =
MSsubgr MSwithin 191.19 1.30
=
= 94.94
n
2
sB|A =
94.94 = 9.74
17.71 = 4.21
Among groups (among cages)
s2A =
MSamong MSsubgr 332.84 191.19
=
= 17.71
nb
8
sA =
Variance components (2)
s2 + s2B|A + s2A = 1.30 + 94.94 + 17.71 = 113.95.
represents
1.30
= 1.1%
113.95
s2B|A represents
94.94
= 83.3%
113.95
s2A
17.71
= 15.6%
113.95
s2
represents
Note:
var(Y) = 2 + B2 |A + A2
var(Y | A) = 2 + B2 |A
var(Y | A, B) = 2
Mosquito averages
I-1
I-2
I-3
I-4
II-1
II-2
II-3
II-4
III-1 III-2 III-3 III-4
58.5 77.8 84.0 70.1
69.8 56.0 50.7 63.8
56.6 77.8 69.9 62.1
59.5 80.9 83.6 68.3
69.8 54.5 49.3 65.8
57.5 79.2 69.2 64.5
ave 59.0 79.4 83.8 69.2
69.8 55.2 50.0 64.8
57.0 78.5 69.6 63.3
ANOVA table
source
df
SS
MS
between
332.8
166.4
within
860.3
95.6
F P-value
1.74
0.23
[Link] <- aov(avelen cage, data=mosq2)
summary([Link])
Ignoring cages
I-1
I-2
I-3
I-4
II-1
II-2
II-3
II-4
III-1 III-2 III-3 III-4
58.5 77.8 84.0 70.1
69.8 56.0 50.7 63.8
56.6 77.8 69.9 62.1
59.5 80.9 83.6 68.3
69.8 54.5 49.3 65.8
57.5 79.2 69.2 64.5
ANOVA table
source
df
SS
MS
P-value
between
11
2386.4
216.9
166.7
< 0.001
within
12
15.6
1.3
mosq$ind2 <- factor(paste(mosq$cage,mosq$individual, sep=":"))
[Link] <- aov(length ind2, data=mosq)
summary([Link])
Ignoring individual mosquitoes
ANOVA table
Cage I Cage II Cage III
58.5
69.8
56.6
59.5
69.8
57.5
77.8
56.0
77.8
80.9
54.5
79.2
84.0
50.7
69.9
83.6
49.3
69.2
70.1
63.8
62.1
68.3
65.8
64.5
source
df
between
within
SS
MS
F P-value
665.7 332.8 4.03
21 1736.3
0.033
86.7
This is wrong!
[Link] <- aov(length cage, data=mosq)
summary([Link])
Example: mixed effects
Jar
Strain Jar
means
LDD
27.000
27.750
26.625
33.375
38.125
31.250
27.500
26.625
28.500
31.750
31.750
35.250
OL
NH
RKS
Strain
Jar
means
Strain
Strain Jar
means
LC
28.500
26.875
27.000
29.500
30.375
28.250
30.125
29.625
31.750
27.875
25.625
27.500
27.125
RH
34.250
NKS
27.452
BS
32.917
means
27.458
29.375
30.500
37.000
Results
source
df
SS
MS
P-value
1323.42
189.06
8.47
< 0.001
16
357.25
22.33
0.80
0.68
168
4663.25
27.76
among strains
among jars within strains
within jars
Note: 8 strains; 3 jars per strain; 8 flies per jar
The expected mean squares are
n B2 |A
2 + n B2 |A
2
2
+ nb
a1
Higher-level nested ANOVA models
You can have as many levels as you like. For example, here is a
three-level nested mixed ANOVA model:
Yijkl = + i + Bij + Cijk + ijkl
Assumptions:
Bij N(0,B2 |A),
Cijk N(0,C2 |B),
ijkl N(0, 2).
Calculations
Source
Sum of squares
among groups
SSamong = b c n
among subgroups
SSsubgr = c n
among subsubgroups
SSsubsubgr = n
within subsubgroups
SSsubsubgr =
df
P
2
i (Yi Y )
a1
PP
2
i
j (Yij Yi )
PPP
2
i
j
k (Yijk Yij )
PPP P
i
l (Yijkl
ijk)2
Y
a (b 1)
a b (c 1)
a b c (n 1)
ANOVA table
SS
MS
bcn
SSamong
SSsubgr
SSsubsubgr
SSwithin
cn
Y)
2
a1
a (Y A
P P
a
Y
)2
A
a(b 1)
b (YB
P P P
Y
)2
B
ab(c 1)
c (Y C
P P P P
a
n (Y
abc(n 1)
MSamong
MSsubgr
expected MS
nC2 B
ncB2 A
+ ncb
MSsubgr
MSsubsubgr
2 + nC2 B + ncB2 A
MSsubsubgr
MSwithin
2 + nC2 B
)2
Y
C
2
a1
Unequal sample size
It is best to design your experiments such that you have equal
sample sizes in each cell. However, once in a while this is not
possible.
In the case of unequal sample sizes, the calculations become really painful (though the R function aov() does all of the calculations
for you).
Even worse, the F tests for the upper levels in the ANOVA table no
longer have a clear null distribution.
Well ignore the details...seek advice if you are in such a situation.