Why is the random effect for classID in the first model identical to the random effect for the interaction classID:school
in the 2nd ?
That is because classID
and classID:school
are indeed the same, in this dataset. That is, the nesting structure is explicit in the way that the factor classID
is coded - each level of classID
"belongs" to one and only one level of school
. That is precisely why the answer to that linked question has this line of code:
> dt$classID <- paste(dt$school, dt$class, sep=".")
which creates a new factor for which the levels are unique to each level of school
. As explained in that answer, the original dataset does not have explicit nesting - it could be fully cross-classified (or just "crossed" for short) or it could be fully nested.
By specifying random intercepts for the interaction between the two factors, we also make exactly the same structure as the line of code above, but without having to create a new variable to do so.
To see this more clearly:
> dt <- read.table("http://bayes.acs.unt.edu:8083/BayesContent/class/Jon/R_SC/Module9/lmm.data.txt", header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)
> xtabs(~ school + class, dt)
class
school a b c d
I 50 50 50 50
II 50 50 50 50
III 50 50 50 50
IV 50 50 50 50
V 50 50 50 50
VI 50 50 50 50
there is an ambiguity about whether these are (fully) nested, or (fully) cross-classified (crossed). If we know that they are nested then we specify the random intercepts in lme4
as:
(1|School/Class)
or equivalently (1|School) + (1|Class:School)
whereas if they are crossed then we specify the random intercepts as
(1|School) + (1|Class)
Notice that, in the answer given to the linked question, this problem is resolved, if the data are nested, by introducing a new variable, classID
such that each class ID is unique to the school to which it "belongs" (which is the definition of nesting):
> dt$classID <- paste(dt$school, dt$class, sep=".")
> xtabs(~ school + classID, dt)
classID
school I.a I.b I.c I.d II.a II.b II.c II.d III.a III.b III.c III.d IV.a IV.b
I 50 50 50 50 0 0 0 0 0 0 0 0 0 0
II 0 0 0 0 50 50 50 50 0 0 0 0 0 0
III 0 0 0 0 0 0 0 0 50 50 50 50 0 0
IV 0 0 0 0 0 0 0 0 0 0 0 0 50 50
V 0 0 0 0 0 0 0 0 0 0 0 0 0 0
VI 0 0 0 0 0 0 0 0 0 0 0 0 0 0
classID
school IV.c IV.d V.a V.b V.c V.d VI.a VI.b VI.c VI.d
I 0 0 0 0 0 0 0 0 0 0
II 0 0 0 0 0 0 0 0 0 0
III 0 0 0 0 0 0 0 0 0 0
IV 50 50 0 0 0 0 0 0 0 0
V 0 0 50 50 50 50 0 0 0 0
VI 0 0 0 0 0 0 50 50 50 50
which shows that each level of class occurs only in one level of school.
However, if instead, we form the interaction between class and school manually:
> dt$int.school.class <- interaction(dt$school, dt$class)
> xtabs(~ school + int.school.class, dt)
then we obtain exactly the same cross-tabulation:
> xtabs(~ school + int.school.class, dt)
int.school.class
school I.a II.a III.a IV.a V.a VI.a I.b II.b III.b IV.b V.b VI.b I.c II.c III.c
I 50 0 0 0 0 0 50 0 0 0 0 0 50 0 0
II 0 50 0 0 0 0 0 50 0 0 0 0 0 50 0
III 0 0 50 0 0 0 0 0 50 0 0 0 0 0 50
IV 0 0 0 50 0 0 0 0 0 50 0 0 0 0 0
V 0 0 0 0 50 0 0 0 0 0 50 0 0 0 0
VI 0 0 0 0 0 50 0 0 0 0 0 50 0 0 0
int.school.class
school IV.c V.c VI.c I.d II.d III.d IV.d V.d VI.d
I 0 0 0 50 0 0 0 0 0
II 0 0 0 0 50 0 0 0 0
III 0 0 0 0 0 50 0 0 0
IV 50 0 0 0 0 0 50 0 0
V 0 50 0 0 0 0 0 50 0
VI 0 0 50 0 0 0 0 0 50
[The cross tabulations might look different at first glance but this is solely because the ordering of the columns is different]
When we specify the interaction in the random part of the model formula (1|Class:School)
that is what lme4
is doing internally - without the need to creating a new factor.