if(require(lava) && require(nlme)){
#### Random effect ####
set.seed(10)
dL <- sampleRem(1e2, n.times = 3, format = "long")
e.lmm1 <- lmm(Y ~ X1+X2+X3 + (1|id), repetition = ~visit|id, data = dL)
nlme::ranef(e.lmm1, se = TRUE)
e.ranef <- estimate(e.lmm1, f = function(p){nlme::ranef(e.lmm1, p = p)})
e.ranef
if(require(ggplot2)){
df.gg <- cbind(index = 1:NROW(e.ranef), e.ranef)
gg.ranef <- ggplot(df.gg, aes(x = index, y=estimate, ymin=lower, ymax = upper))
gg.ranef + geom_point() + geom_errorbar() + ylab("estimated random effect") + xlab("id")
}
#### ANCOVA via mixed model ####
set.seed(10)
d <- sampleRem(1e2, n.time = 2)
e.ANCOVA1 <- lm(Y2~Y1+X1, data = d)
if(require(reshape2)){
dL2 <- melt(d, id.vars = c("id","X1"), measure.vars = c("Y1","Y2"),
value.name = "Y", variable.name = "time")
dL2$time <- factor(dL2$time, levels = c("Y1","Y2"), labels = c("1","2"))
## estimated treatment effect (no baseline constraint)
e.lmm <- lmm(Y ~ time + time:X1, data = dL2, repetition = ~time|id)
e.delta <- estimate(e.lmm, function(p){
c(Y1 = p["rho(1,2)"]*p["k.2"],
X1 = p["time2:X1"]-p["k.2"]*p["rho(1,2)"]*p["time1:X1"])
}) ## same estimate and similar standard errors.
e.delta ## Degrees of freedom are a bit off though
cbind(summary(e.ANCOVA1)$coef, df = df.residual(e.ANCOVA1))
## estimated treatment effect (baseline constraint)
dL2$time2 <- as.numeric(dL2$time=="2")
e.lmmC <- lmm(Y ~ time2 + time2:X1, data = dL2, repetition = ~time|id)
e.deltaC <- estimate(e.lmmC, function(p){
c(Y1 = p["rho(1,2)"]*p["k.2"],
X1 = p["time2:X1"])
})
e.deltaC ## Degrees of freedom are a bit more accurate
}
}
Run the code above in your browser using DataLab