Statistics 333
Random Effects Models and Nested Factors
Spring 2003
In class, I described the ideas of random effects and fixed effects. It is appropriate to model a factor as a
random effect if you wish to think of the levels as having been randomly selected from a larger population whereas
fixed effects are factors where the levels in the problem are the only ones of interest. We also talked about the
difference between factors that are crossed or nested. Two factors are crossed if there is at least one observation
for each pair of levels. Two factors are nested if the levels of one factor are completely contained in the levels of
another factor. For the chimp example in Chapter 14, the factors sign and chimp are crossed because each chimp
is measured at each sign. If we created a factor sex, then chimp would be nested within sex as the levels Bruno
and Booee are nested within male and Cindy and Thelma are female.
For this problem, it makes sense to consider treating chimp as a random effect. However, R is quite limited
in the types of mixed effects models that can be fit. Fitting nested factors works quite well, but fitting crossed
factors when one or more are random is far from straight-forward. So, I’m not going to show you how to do it.
But here is an example of fitting a model with nested factors adding the factor sex. In the formula, we indicate
the nesting of chimp within sex by sex/CHIMP. We will have to create the sex factor within R. (Alternatively, we
could edit the file in which we read in the data). The strategy will be to create a variable that is female everywhere
and then change the proper observations to male.
> case1401 <- read.table("sleuth/case1401.csv", header = T, sep = ",")
> attach(case1401)
> sex = rep("female", nrow(case1401))
> sex[CHIMP == "BOOEE"] <- "male"
> sex[CHIMP == "BRUNO"] <- "male"
> sex <- factor(sex)
> new1401 <- data.frame(case1401, sex)
> detach()
> attach(new1401)
> fit1 <- lm(log(MINUTES) ~ SIGN + CHIMP)
> fit2 <- lm(log(MINUTES) ~ SIGN + sex/CHIMP)
> anova(fit1)
Anal