###################################################
### chunk number 1: preliminaries
###################################################
options(width=65,show.signif.stars=FALSE)
library(lattice)
library(Matrix)
library(lme4)
data(Early, package = "mlmRev")
#lattice.options(default.theme = standard.theme(color = FALSE))


###################################################
### chunk number 2: Dyestuff2plot
###################################################
print(dotplot(reorder(Batch, Yield) ~ Yield, Dyestuff2,
              ylab = "Batch", jitter.y = TRUE,
              xlab = "Yield of dyestuff (grams of standard color)",
              type = c("p", "a")))


###################################################
### chunk number 3: Dyestuff2mod
###################################################
lmer(Yield ~ 1 + (1|Batch), Dyestuff2)


###################################################
### chunk number 4: EarlyData
###################################################
print(xyplot(cog ~ age | id, Early, type = c('g', 'b'), aspect = 'xy',
             layout = c(29,4), between = list(y = c(0,0.5)),
             xlab = "Age (yr)",
             ylab = "Cognitive development score",
             scales = list(x = list(tick.number = 3, alternating = TRUE,
                           labels = c("1","","2"), at = c(1,1.5,2))),
             par.strip.text = list(cex = 0.6)))


###################################################
### chunk number 5: ttest
###################################################
with(subset(Early, age == 1 & trt == 'N'), summary(cog))
t.test(subset(Early, age == 1 & trt == 'N')$cog, mu = 100)$p.value


###################################################
### chunk number 6: EarlyBoxW
###################################################
print(bwplot(factor(age) ~ cog | trt, Early,
             strip = FALSE, strip.left = TRUE, layout = c(1, 2),
             ylab = "Age (yr) within group",
             xlab = "Cognitive development score"),
      position = c(0, 0, 1, 0.55), more = TRUE)
print(bwplot(trt ~ cog | factor(age), Early,
             strip = FALSE, strip.left = TRUE, layout = c(1, 3),
             ylab = "Group within Age (yr)", xlab = NULL),
      position = c(0, 0.5, 1, 1), more = FALSE)


###################################################
### chunk number 7: EarlyDensity
###################################################
print(densityplot(~ cog|trt, Early, plot.points = FALSE,
                  ref = TRUE, layout = c(1,2),
                  xlab = "Cognitive development score",
                  groups = age, auto.key = list(columns = 3),
                  strip = FALSE, strip.left=TRUE))


###################################################
### chunk number 8: EarlyDensity2
###################################################
print(densityplot(~ cog|factor(age), Early, plot.points = FALSE,
                  ref = TRUE, layout = c(1,3),
                  xlab = "Cognitive development score",
                  groups = trt, auto.key = list(columns = 2),
                  strip = FALSE, strip.left=TRUE))


###################################################
### chunk number 9: Earlymodel
###################################################
Early <- within(Early, tos <- age - 0.5)
fm1 <- lmer(cog ~ tos*trt + (tos|id), Early, verbose = TRUE)


###################################################
### chunk number 10: Earlymodelsummary
###################################################
print(fm1, corr = FALSE)


###################################################
### chunk number 11: EarlyModel2
###################################################
print(fm2 <- update(fm1, . ~ . - trt, verbose = FALSE), corr = FALSE)


###################################################
### chunk number 12: EarlyCondMode
###################################################
print(plot(ranef(fm2), type = c("g", "p"), aspect = 1)$id)


###################################################
### chunk number 13: Earlyup
###################################################
fm2M <- update(fm2, REML = FALSE)
fm3M <- lmer(cog ~ tos + tos:trt + (1|id) + (0+tos|id), Early, REML = FALSE)
fm4M <- lmer(cog ~ tos + tos:trt + (1|id), Early, REML = FALSE)


###################################################
### chunk number 14: Earlyup
###################################################
anova(fm3M, fm2M)
anova(fm4M, fm2M)


