# Pig diet data - compare fixed and random effects

We look briefly at the pig diet data again to make a comparision between using fixed and random effects for the blocks (i.e. litters).

options(digits = 3)
pigs <- data.frame(Gain = c(89, 78, 114, 79, 68, 59, 85, 61, 62, 61, 83, 82),
Litter = rep(c("I", "II", "III", "IV"), 3),
Diet = rep(c("A", "B", "C"), each = 4))
pigs
##    Gain Litter Diet
## 1    89      I    A
## 2    78     II    A
## 3   114    III    A
## 4    79     IV    A
## 5    68      I    B
## 6    59     II    B
## 7    85    III    B
## 8    61     IV    B
## 9    62      I    C
## 10   61     II    C
## 11   83    III    C
## 12   82     IV    C

Rather than using the usual baseline idea of $$\alpha_1=0$$ (where the $$\alpha_i$$ are the parameters for different levels of a categorical variable) here we’ll use sum contrasts which instead impose the constraint $$\sum_i \alpha_i=0$$ to determine parameter estimates of the $$\alpha_i$$ (i.e. we replace the constraint $$\alpha_1=0$$ by $$\sum_i \alpha_i=0$$). These sum-to-zero contrasts make it easy to see the shrinkage toward zero with random effects relative to a similar model with fixed effects.

options(contrasts = c("contr.sum", "contr.poly"))
# options("contrasts") will show the contrasts in use
# options(contrasts = c("contr.treatment", "contr.poly")) will revert to the
# usual treatment contrasts (alpha_1 = 0)

# fixed effects for both diet and litter:
pigs.lm <- lm(Gain ~ Litter + Diet, data = pigs)

# fixed effects for diet and random effects for litter:
library(nlme)
pigs.lme <- lme(Gain ~ Diet, random =  ~ 1 | Litter, data = pigs)
coef(pigs.lm)
## (Intercept)     Litter1     Litter2     Litter3       Diet1       Diet2
##       76.75       -3.75      -10.75       17.25       13.25       -8.50
# Because of the sum-to-zero constraint, the Litter4 coefficient is:
-sum(coef(pigs.lm)[2:4])
## [1] -2.75

Observe that the four litter values below, from the random effects model, are all shrunk toward zero (i.e. closer to zero) compared with the corresponding fixed effects for litters above.

ranef(pigs.lme)
##     (Intercept)
## I         -3.18
## II        -9.11
## III       14.61
## IV        -2.33