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


###################################################
### chunk number 2: Machinesplot
###################################################
print(dotplot(reorder(Worker, score) ~ score, Machines,
              groups = Machine, 
              xlab = "Quality and productivity score", 
              ylab = "Worker", type = c("p","a"), 
              auto.key = list(columns = 3, lines = TRUE), 
              jitter.y = TRUE))


###################################################
### chunk number 3: MachinesModel1
###################################################
print(fm1 <- lmer(score ~ Machine + (1|Worker) + (1|Worker:Machine),
                  Machines), corr = FALSE)


###################################################
### chunk number 4: MachinesModel1
###################################################
print(fm2 <- lmer(score ~ Machine + (0+Machine|Worker), Machines),
      corr = FALSE)


###################################################
### chunk number 5: Machinesanova
###################################################
fm2M <- update(fm2, REML = FALSE)
fm1M <- update(fm1, REML = FALSE)
anova(fm2M, fm1M)


###################################################
### chunk number 6: MachineswoB6
###################################################
Machines1 <- subset(Machines, Worker != "6" | Machine != 'B')
xtabs(~ Machine + Worker, Machines1)


###################################################
### chunk number 7: Machines1plot
###################################################
print(dotplot(reorder(Worker, score) ~ score, Machines1,
              groups = Machine, 
              xlab = "Quality and productivity score", 
              ylab = "Worker", type = c("p","a"), 
              auto.key = list(columns = 3, lines = TRUE), 
              jitter.y = TRUE))


###################################################
### chunk number 8: fm1aM
###################################################
fm1aM <- lmer(score ~ Machine + (1|Worker) + (1|Worker:Machine), Machines1, REML = FALSE)
fm2aM <- lmer(score ~ Machine + (0 + Machine|Worker), Machines1, REML = FALSE)
anova(fm2aM, fm1aM)


