Effect of %in% in a model formula? What is the meaning and effect of %in% in a model formula?
It is apparently used for nesting of one variable into another in a variety of analysis (manova, anova, regressions) in a few published articles.
From ?formula, b%in%a is a:b, so why use %in%?
How is a:b nesting?
I am probably mistaken, but my understanding is that nesting b in a should not lead to the same mean square as the interaction of a and b denoted by a:b?
library(lme4)  




with(sleepstudy, Days%in%Subject)
  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE ...  




fit<-aov(data=sleepstudy, Reaction~Days + Days%in%Subject)
anova(fit)


               Df Sum Sq Mean Sq F value    Pr(>F)    
 Days           1 162703  162703  193.23 < 2.2e-16 ***
 Days:Subject  17 269685   15864   18.84 < 2.2e-16 ***
 Residuals    161 135567     842




fm1 <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy)
anova(fm1)


      Df Sum Sq Mean Sq F value
 Days  1  29986   29986  45.785




fm1 <- lmer(Reaction~Days + Days%in%Subject + (1|Subject), sleepstudy)
anova(fm1)

Analysis of Variance Table
             Df Sum Sq Mean Sq  F value
Days          1 162703  162703 248.4233
Days:Subject 17  73391    4317   6.5916

 A: Note that %in% works differently in commands than in formulas, just like + and * work differently, so the with example is doing something different from the other examples.
Another thing to think about is how the data is coded, assume that the data we are looking at is schools and students with students nested in schools.  We can give every student a unique identifier, e.g. student 1 in school 1 would have an identifier like 01001 and student 1 in school 2 would have identifier 02001.  Or we can give students an ID that is unique in their school, but has to be combined with their school info to uniquely identify them, e.g. student 1 in school 1 has ID 1, and student 1 in school 2 also has ID 1, but they are 2 different students.  In this latter case the : combines the school ID with the Student ID to create unique identifiers for each student (so long as both are stored as factors, if they are both numeric then this causes more problems).  The %in% syntax gives the same result as the : but some people thought that it showed better the relationship.  The mixed effects approach (lmer) uses a different way to represent and do the computations that requires fewer assumptions and conditions and does not use the %in% syntax.
Overall it is best to not use %in% in a formula, but rather use the mixed models functions and syntax.
