Add materials
This commit is contained in:
parent
1b3d182923
commit
7b2649be60
157
code/depression.R
Normal file
157
code/depression.R
Normal file
@ -0,0 +1,157 @@
|
|||||||
|
# depression.R
|
||||||
|
#
|
||||||
|
# content: (1) Read data
|
||||||
|
# (2) Visualize data
|
||||||
|
# (3) Fit growth curve model
|
||||||
|
#
|
||||||
|
# input: data/reisby.txt
|
||||||
|
# output: --
|
||||||
|
#
|
||||||
|
# last mod: 2026-04-20, NW
|
||||||
|
|
||||||
|
library("lme4")
|
||||||
|
library("lattice")
|
||||||
|
|
||||||
|
#----- (1) Read data ----------------------------------------------------------
|
||||||
|
dat <- read.table("data/reisby.txt", header = TRUE)
|
||||||
|
dat$id <- factor(dat$id)
|
||||||
|
dat$diag <- factor(dat$diag, levels = c("nonen", "endog"))
|
||||||
|
dat <- na.omit(dat) # drop missing values
|
||||||
|
|
||||||
|
boxplot(hamd ~ week, dat,
|
||||||
|
col = "#3CB4DC",
|
||||||
|
ylab = "HDRS score",
|
||||||
|
xlab = "Time (week)")
|
||||||
|
|
||||||
|
#----- (2) Fitting random slope model -----------------------------------------
|
||||||
|
|
||||||
|
# random slope model
|
||||||
|
lme2 <- lmer(hamd ~ week + (week | id), dat, REML = FALSE)
|
||||||
|
|
||||||
|
xyplot(hamd + predict(lme2) ~ week | id, data = dat,
|
||||||
|
type = c("p", "l", "g"),
|
||||||
|
ratio = "xy",
|
||||||
|
distribute.type = TRUE,
|
||||||
|
layout = c(11, 6),
|
||||||
|
ylab = "HDRS score",
|
||||||
|
xlab = "Time (week)")
|
||||||
|
|
||||||
|
#--------------- (3) Fitting quadratic model ----------------------------------
|
||||||
|
|
||||||
|
# model with quadratic time trend
|
||||||
|
lme3 <- lmer(hamd ~ week + I(week^2) + (week + I(week^2) | id), dat,
|
||||||
|
REML = FALSE)
|
||||||
|
|
||||||
|
xyplot(hamd + predict(lme3) ~ week | id, data = dat,
|
||||||
|
type = c("p", "l", "g"),
|
||||||
|
ratio = "xy",
|
||||||
|
distribute.type = TRUE,
|
||||||
|
layout = c(11, 6),
|
||||||
|
ylab = "HDRS score",
|
||||||
|
xlab = "Time (week)")
|
||||||
|
|
||||||
|
#--------------- (4) Check random effects structure ---------------
|
||||||
|
|
||||||
|
# Catterpillar plots
|
||||||
|
|
||||||
|
dotplot(ranef(lme3), col = "#3CB4DC",
|
||||||
|
scales = list(x = list(relation = "free")))[[1]]
|
||||||
|
|
||||||
|
# Shrinkage plots
|
||||||
|
|
||||||
|
df <- coef(lmList(hamd ~ week_c + I(week_c^2) | id, dat))
|
||||||
|
cc1 <- as.data.frame(coef(lme3reml)$id)
|
||||||
|
names(cc1) <- c("A", "B", "C")
|
||||||
|
|
||||||
|
df <- cbind(df, cc1)
|
||||||
|
ff <- fixef(lme3reml)
|
||||||
|
|
||||||
|
|
||||||
|
## shrinkage intercept and week
|
||||||
|
with(df,
|
||||||
|
xyplot(`(Intercept)` ~ week_c, aspect = 1,
|
||||||
|
x1 = B, y1 = A,
|
||||||
|
panel = function(x, y, x1, y1, subscripts, ...) {
|
||||||
|
panel.grid(h = -1, v = -1)
|
||||||
|
x1 <- x1[subscripts]
|
||||||
|
y1 <- y1[subscripts]
|
||||||
|
larrows(x, y, x1, y1, type = "closed", length = 0.1, fill = "black",
|
||||||
|
angle = 15, ...)
|
||||||
|
lpoints(x, y,
|
||||||
|
pch = 16,
|
||||||
|
col = trellis.par.get("superpose.symbol")$col[2])
|
||||||
|
lpoints(x1, y1,
|
||||||
|
pch = 16,
|
||||||
|
col = trellis.par.get("superpose.symbol")$col[1])
|
||||||
|
lpoints(ff[2], ff[1],
|
||||||
|
pch = 16,
|
||||||
|
col = trellis.par.get("superpose.symbol")$col[3])
|
||||||
|
},
|
||||||
|
xlab = "week_c",
|
||||||
|
ylab = "(Intercept)",
|
||||||
|
key = list(space = "top", columns = 3,
|
||||||
|
text = list(c("Mixed model", "Within-subject", "Population")),
|
||||||
|
points = list(col = trellis.par.get("superpose.symbol")$col[1:3],
|
||||||
|
pch = 16))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
## shrinkage intercept and week^2
|
||||||
|
with(df,
|
||||||
|
xyplot(`(Intercept)` ~ `I(week_c^2)`, aspect = 1,
|
||||||
|
x1 = C, y1 = A,
|
||||||
|
panel = function(x, y, x1, y1, subscripts, ...) {
|
||||||
|
panel.grid(h = -1, v = -1)
|
||||||
|
x1 <- x1[subscripts]
|
||||||
|
y1 <- y1[subscripts]
|
||||||
|
larrows(x, y, x1, y1, type = "closed", length = 0.1, fill = "black",
|
||||||
|
angle = 15, ...)
|
||||||
|
lpoints(x, y,
|
||||||
|
pch = 16,
|
||||||
|
col = trellis.par.get("superpose.symbol")$col[2])
|
||||||
|
lpoints(x1, y1,
|
||||||
|
pch = 16,
|
||||||
|
col = trellis.par.get("superpose.symbol")$col[1])
|
||||||
|
lpoints(ff[3], ff[1],
|
||||||
|
pch = 16,
|
||||||
|
col = trellis.par.get("superpose.symbol")$col[3])
|
||||||
|
},
|
||||||
|
xlab = expression(week_c^2),
|
||||||
|
ylab = "(Intercept)",
|
||||||
|
key = list(space = "top", columns = 3,
|
||||||
|
text = list(c("Mixed model", "Within-subject", "Population")),
|
||||||
|
points = list(col = trellis.par.get("superpose.symbol")$col[1:3],
|
||||||
|
pch = 16))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
## shrinkage week and week^2
|
||||||
|
with(df,
|
||||||
|
xyplot(week_c ~ `I(week_c^2)`, aspect = 1,
|
||||||
|
x1 = C, y1 = B,
|
||||||
|
panel = function(x, y, x1, y1, subscripts, ...) {
|
||||||
|
panel.grid(h = -1, v = -1)
|
||||||
|
x1 <- x1[subscripts]
|
||||||
|
y1 <- y1[subscripts]
|
||||||
|
larrows(x, y, x1, y1, type = "closed", length = 0.1, fill = "black",
|
||||||
|
angle = 15, ...)
|
||||||
|
lpoints(x, y,
|
||||||
|
pch = 16,
|
||||||
|
col = trellis.par.get("superpose.symbol")$col[2])
|
||||||
|
lpoints(x1, y1,
|
||||||
|
pch = 16,
|
||||||
|
col = trellis.par.get("superpose.symbol")$col[1])
|
||||||
|
lpoints(ff[3], ff[2],
|
||||||
|
pch = 16,
|
||||||
|
col = trellis.par.get("superpose.symbol")$col[3])
|
||||||
|
},
|
||||||
|
xlab = expression(week_c^2),
|
||||||
|
ylab = "week_c",
|
||||||
|
key = list(space = "top", columns = 3,
|
||||||
|
text = list(c("Mixed model", "Within-subject", "Population")),
|
||||||
|
points = list(col = trellis.par.get("superpose.symbol")$col[1:3],
|
||||||
|
pch = 16))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
2010
data/moeller.csv
Normal file
2010
data/moeller.csv
Normal file
File diff suppressed because it is too large
Load Diff
399
data/reisby.txt
Normal file
399
data/reisby.txt
Normal file
@ -0,0 +1,399 @@
|
|||||||
|
## Data from Reisby et al., Psychopharmacology 54 (1977) 263-272
|
||||||
|
id hamd week diag endweek
|
||||||
|
101 26 0 nonen 0
|
||||||
|
101 22 1 nonen 0
|
||||||
|
101 18 2 nonen 0
|
||||||
|
101 7 3 nonen 0
|
||||||
|
101 4 4 nonen 0
|
||||||
|
101 3 5 nonen 0
|
||||||
|
103 33 0 nonen 0
|
||||||
|
103 24 1 nonen 0
|
||||||
|
103 15 2 nonen 0
|
||||||
|
103 24 3 nonen 0
|
||||||
|
103 15 4 nonen 0
|
||||||
|
103 13 5 nonen 0
|
||||||
|
104 29 0 endog 0
|
||||||
|
104 22 1 endog 1
|
||||||
|
104 18 2 endog 2
|
||||||
|
104 13 3 endog 3
|
||||||
|
104 19 4 endog 4
|
||||||
|
104 0 5 endog 5
|
||||||
|
105 22 0 nonen 0
|
||||||
|
105 12 1 nonen 0
|
||||||
|
105 16 2 nonen 0
|
||||||
|
105 16 3 nonen 0
|
||||||
|
105 13 4 nonen 0
|
||||||
|
105 9 5 nonen 0
|
||||||
|
106 21 0 endog 0
|
||||||
|
106 25 1 endog 1
|
||||||
|
106 23 2 endog 2
|
||||||
|
106 18 3 endog 3
|
||||||
|
106 20 4 endog 4
|
||||||
|
106 NA 5 endog 5
|
||||||
|
107 21 0 endog 0
|
||||||
|
107 21 1 endog 1
|
||||||
|
107 16 2 endog 2
|
||||||
|
107 19 3 endog 3
|
||||||
|
107 NA 4 endog 4
|
||||||
|
107 6 5 endog 5
|
||||||
|
108 21 0 endog 0
|
||||||
|
108 22 1 endog 1
|
||||||
|
108 11 2 endog 2
|
||||||
|
108 9 3 endog 3
|
||||||
|
108 9 4 endog 4
|
||||||
|
108 7 5 endog 5
|
||||||
|
113 21 0 nonen 0
|
||||||
|
113 23 1 nonen 0
|
||||||
|
113 19 2 nonen 0
|
||||||
|
113 23 3 nonen 0
|
||||||
|
113 23 4 nonen 0
|
||||||
|
113 NA 5 nonen 0
|
||||||
|
114 NA 0 nonen 0
|
||||||
|
114 17 1 nonen 0
|
||||||
|
114 11 2 nonen 0
|
||||||
|
114 13 3 nonen 0
|
||||||
|
114 7 4 nonen 0
|
||||||
|
114 7 5 nonen 0
|
||||||
|
115 NA 0 endog 0
|
||||||
|
115 16 1 endog 1
|
||||||
|
115 16 2 endog 2
|
||||||
|
115 16 3 endog 3
|
||||||
|
115 16 4 endog 4
|
||||||
|
115 11 5 endog 5
|
||||||
|
117 19 0 endog 0
|
||||||
|
117 16 1 endog 1
|
||||||
|
117 13 2 endog 2
|
||||||
|
117 12 3 endog 3
|
||||||
|
117 7 4 endog 4
|
||||||
|
117 6 5 endog 5
|
||||||
|
118 NA 0 endog 0
|
||||||
|
118 26 1 endog 1
|
||||||
|
118 18 2 endog 2
|
||||||
|
118 18 3 endog 3
|
||||||
|
118 14 4 endog 4
|
||||||
|
118 11 5 endog 5
|
||||||
|
120 20 0 nonen 0
|
||||||
|
120 19 1 nonen 0
|
||||||
|
120 17 2 nonen 0
|
||||||
|
120 18 3 nonen 0
|
||||||
|
120 16 4 nonen 0
|
||||||
|
120 17 5 nonen 0
|
||||||
|
121 20 0 nonen 0
|
||||||
|
121 22 1 nonen 0
|
||||||
|
121 19 2 nonen 0
|
||||||
|
121 19 3 nonen 0
|
||||||
|
121 12 4 nonen 0
|
||||||
|
121 14 5 nonen 0
|
||||||
|
123 15 0 nonen 0
|
||||||
|
123 15 1 nonen 0
|
||||||
|
123 15 2 nonen 0
|
||||||
|
123 13 3 nonen 0
|
||||||
|
123 5 4 nonen 0
|
||||||
|
123 5 5 nonen 0
|
||||||
|
501 29 0 endog 0
|
||||||
|
501 30 1 endog 1
|
||||||
|
501 26 2 endog 2
|
||||||
|
501 22 3 endog 3
|
||||||
|
501 19 4 endog 4
|
||||||
|
501 24 5 endog 5
|
||||||
|
502 21 0 endog 0
|
||||||
|
502 22 1 endog 1
|
||||||
|
502 13 2 endog 2
|
||||||
|
502 11 3 endog 3
|
||||||
|
502 2 4 endog 4
|
||||||
|
502 1 5 endog 5
|
||||||
|
504 19 0 nonen 0
|
||||||
|
504 17 1 nonen 0
|
||||||
|
504 15 2 nonen 0
|
||||||
|
504 16 3 nonen 0
|
||||||
|
504 12 4 nonen 0
|
||||||
|
504 12 5 nonen 0
|
||||||
|
505 21 0 nonen 0
|
||||||
|
505 11 1 nonen 0
|
||||||
|
505 18 2 nonen 0
|
||||||
|
505 0 3 nonen 0
|
||||||
|
505 0 4 nonen 0
|
||||||
|
505 4 5 nonen 0
|
||||||
|
507 27 0 endog 0
|
||||||
|
507 26 1 endog 1
|
||||||
|
507 26 2 endog 2
|
||||||
|
507 25 3 endog 3
|
||||||
|
507 24 4 endog 4
|
||||||
|
507 19 5 endog 5
|
||||||
|
603 28 0 nonen 0
|
||||||
|
603 22 1 nonen 0
|
||||||
|
603 18 2 nonen 0
|
||||||
|
603 20 3 nonen 0
|
||||||
|
603 11 4 nonen 0
|
||||||
|
603 13 5 nonen 0
|
||||||
|
604 27 0 nonen 0
|
||||||
|
604 27 1 nonen 0
|
||||||
|
604 13 2 nonen 0
|
||||||
|
604 5 3 nonen 0
|
||||||
|
604 7 4 nonen 0
|
||||||
|
604 NA 5 nonen 0
|
||||||
|
606 19 0 endog 0
|
||||||
|
606 33 1 endog 1
|
||||||
|
606 12 2 endog 2
|
||||||
|
606 12 3 endog 3
|
||||||
|
606 3 4 endog 4
|
||||||
|
606 1 5 endog 5
|
||||||
|
607 30 0 endog 0
|
||||||
|
607 39 1 endog 1
|
||||||
|
607 30 2 endog 2
|
||||||
|
607 27 3 endog 3
|
||||||
|
607 20 4 endog 4
|
||||||
|
607 4 5 endog 5
|
||||||
|
608 24 0 nonen 0
|
||||||
|
608 19 1 nonen 0
|
||||||
|
608 14 2 nonen 0
|
||||||
|
608 12 3 nonen 0
|
||||||
|
608 3 4 nonen 0
|
||||||
|
608 4 5 nonen 0
|
||||||
|
609 NA 0 endog 1
|
||||||
|
609 25 1 endog 1
|
||||||
|
609 22 2 endog 2
|
||||||
|
609 14 3 endog 3
|
||||||
|
609 15 4 endog 4
|
||||||
|
609 2 5 endog 5
|
||||||
|
610 34 0 endog 0
|
||||||
|
610 NA 1 endog 1
|
||||||
|
610 33 2 endog 2
|
||||||
|
610 23 3 endog 3
|
||||||
|
610 NA 4 endog 4
|
||||||
|
610 11 5 endog 5
|
||||||
|
302 18 0 endog 0
|
||||||
|
302 22 1 endog 1
|
||||||
|
302 16 2 endog 2
|
||||||
|
302 8 3 endog 3
|
||||||
|
302 9 4 endog 4
|
||||||
|
302 12 5 endog 5
|
||||||
|
303 21 0 nonen 0
|
||||||
|
303 21 1 nonen 0
|
||||||
|
303 13 2 nonen 0
|
||||||
|
303 14 3 nonen 0
|
||||||
|
303 10 4 nonen 0
|
||||||
|
303 5 5 nonen 0
|
||||||
|
304 21 0 endog 0
|
||||||
|
304 27 1 endog 1
|
||||||
|
304 29 2 endog 2
|
||||||
|
304 NA 3 endog 3
|
||||||
|
304 12 4 endog 4
|
||||||
|
304 24 5 endog 5
|
||||||
|
305 19 0 nonen 0
|
||||||
|
305 17 1 nonen 0
|
||||||
|
305 15 2 nonen 0
|
||||||
|
305 11 3 nonen 0
|
||||||
|
305 5 4 nonen 0
|
||||||
|
305 1 5 nonen 0
|
||||||
|
308 22 0 nonen 0
|
||||||
|
308 21 1 nonen 0
|
||||||
|
308 18 2 nonen 0
|
||||||
|
308 17 3 nonen 0
|
||||||
|
308 12 4 nonen 0
|
||||||
|
308 11 5 nonen 0
|
||||||
|
309 22 0 nonen 0
|
||||||
|
309 22 1 nonen 0
|
||||||
|
309 16 2 nonen 0
|
||||||
|
309 19 3 nonen 0
|
||||||
|
309 20 4 nonen 0
|
||||||
|
309 11 5 nonen 0
|
||||||
|
310 24 0 endog 0
|
||||||
|
310 19 1 endog 1
|
||||||
|
310 11 2 endog 2
|
||||||
|
310 7 3 endog 3
|
||||||
|
310 6 4 endog 4
|
||||||
|
310 NA 5 endog 5
|
||||||
|
311 20 0 endog 0
|
||||||
|
311 16 1 endog 1
|
||||||
|
311 21 2 endog 2
|
||||||
|
311 17 3 endog 3
|
||||||
|
311 NA 4 endog 4
|
||||||
|
311 15 5 endog 5
|
||||||
|
312 17 0 endog 0
|
||||||
|
312 NA 1 endog 1
|
||||||
|
312 18 2 endog 2
|
||||||
|
312 17 3 endog 3
|
||||||
|
312 17 4 endog 4
|
||||||
|
312 6 5 endog 5
|
||||||
|
313 21 0 nonen 0
|
||||||
|
313 19 1 nonen 0
|
||||||
|
313 10 2 nonen 0
|
||||||
|
313 11 3 nonen 0
|
||||||
|
313 11 4 nonen 0
|
||||||
|
313 8 5 nonen 0
|
||||||
|
315 27 0 endog 0
|
||||||
|
315 21 1 endog 1
|
||||||
|
315 17 2 endog 2
|
||||||
|
315 13 3 endog 3
|
||||||
|
315 5 4 endog 4
|
||||||
|
315 NA 5 endog 5
|
||||||
|
316 32 0 endog 0
|
||||||
|
316 26 1 endog 1
|
||||||
|
316 23 2 endog 2
|
||||||
|
316 26 3 endog 3
|
||||||
|
316 23 4 endog 4
|
||||||
|
316 24 5 endog 5
|
||||||
|
318 17 0 endog 0
|
||||||
|
318 18 1 endog 1
|
||||||
|
318 19 2 endog 2
|
||||||
|
318 21 3 endog 3
|
||||||
|
318 17 4 endog 4
|
||||||
|
318 11 5 endog 5
|
||||||
|
319 24 0 endog 0
|
||||||
|
319 18 1 endog 1
|
||||||
|
319 10 2 endog 2
|
||||||
|
319 14 3 endog 3
|
||||||
|
319 13 4 endog 4
|
||||||
|
319 12 5 endog 5
|
||||||
|
322 28 0 endog 0
|
||||||
|
322 21 1 endog 1
|
||||||
|
322 25 2 endog 2
|
||||||
|
322 32 3 endog 3
|
||||||
|
322 34 4 endog 4
|
||||||
|
322 NA 5 endog 5
|
||||||
|
327 17 0 nonen 0
|
||||||
|
327 18 1 nonen 0
|
||||||
|
327 15 2 nonen 0
|
||||||
|
327 8 3 nonen 0
|
||||||
|
327 19 4 nonen 0
|
||||||
|
327 17 5 nonen 0
|
||||||
|
328 22 0 nonen 0
|
||||||
|
328 24 1 nonen 0
|
||||||
|
328 28 2 nonen 0
|
||||||
|
328 26 3 nonen 0
|
||||||
|
328 28 4 nonen 0
|
||||||
|
328 29 5 nonen 0
|
||||||
|
331 19 0 nonen 0
|
||||||
|
331 21 1 nonen 0
|
||||||
|
331 18 2 nonen 0
|
||||||
|
331 16 3 nonen 0
|
||||||
|
331 14 4 nonen 0
|
||||||
|
331 10 5 nonen 0
|
||||||
|
333 23 0 nonen 0
|
||||||
|
333 20 1 nonen 0
|
||||||
|
333 21 2 nonen 0
|
||||||
|
333 20 3 nonen 0
|
||||||
|
333 24 4 nonen 0
|
||||||
|
333 14 5 nonen 0
|
||||||
|
334 31 0 nonen 0
|
||||||
|
334 25 1 nonen 0
|
||||||
|
334 NA 2 nonen 0
|
||||||
|
334 7 3 nonen 0
|
||||||
|
334 8 4 nonen 0
|
||||||
|
334 11 5 nonen 0
|
||||||
|
335 21 0 nonen 0
|
||||||
|
335 21 1 nonen 0
|
||||||
|
335 18 2 nonen 0
|
||||||
|
335 15 3 nonen 0
|
||||||
|
335 12 4 nonen 0
|
||||||
|
335 10 5 nonen 0
|
||||||
|
337 27 0 nonen 0
|
||||||
|
337 22 1 nonen 0
|
||||||
|
337 23 2 nonen 0
|
||||||
|
337 21 3 nonen 0
|
||||||
|
337 12 4 nonen 0
|
||||||
|
337 13 5 nonen 0
|
||||||
|
338 22 0 nonen 0
|
||||||
|
338 20 1 nonen 0
|
||||||
|
338 22 2 nonen 0
|
||||||
|
338 23 3 nonen 0
|
||||||
|
338 19 4 nonen 0
|
||||||
|
338 18 5 nonen 0
|
||||||
|
339 27 0 endog 0
|
||||||
|
339 NA 1 endog 1
|
||||||
|
339 14 2 endog 2
|
||||||
|
339 12 3 endog 3
|
||||||
|
339 11 4 endog 4
|
||||||
|
339 12 5 endog 5
|
||||||
|
344 NA 0 endog 0
|
||||||
|
344 21 1 endog 1
|
||||||
|
344 12 2 endog 2
|
||||||
|
344 13 3 endog 3
|
||||||
|
344 13 4 endog 4
|
||||||
|
344 18 5 endog 5
|
||||||
|
345 29 0 nonen 0
|
||||||
|
345 27 1 nonen 0
|
||||||
|
345 27 2 nonen 0
|
||||||
|
345 22 3 nonen 0
|
||||||
|
345 22 4 nonen 0
|
||||||
|
345 23 5 nonen 0
|
||||||
|
346 25 0 endog 0
|
||||||
|
346 24 1 endog 1
|
||||||
|
346 19 2 endog 2
|
||||||
|
346 23 3 endog 3
|
||||||
|
346 14 4 endog 4
|
||||||
|
346 21 5 endog 5
|
||||||
|
347 18 0 endog 0
|
||||||
|
347 15 1 endog 1
|
||||||
|
347 14 2 endog 2
|
||||||
|
347 10 3 endog 3
|
||||||
|
347 8 4 endog 4
|
||||||
|
347 NA 5 endog 5
|
||||||
|
348 24 0 nonen 0
|
||||||
|
348 21 1 nonen 0
|
||||||
|
348 12 2 nonen 0
|
||||||
|
348 13 3 nonen 0
|
||||||
|
348 12 4 nonen 0
|
||||||
|
348 5 5 nonen 0
|
||||||
|
349 17 0 endog 0
|
||||||
|
349 19 1 endog 1
|
||||||
|
349 15 2 endog 2
|
||||||
|
349 12 3 endog 3
|
||||||
|
349 9 4 endog 4
|
||||||
|
349 13 5 endog 5
|
||||||
|
350 22 0 nonen 0
|
||||||
|
350 25 1 nonen 0
|
||||||
|
350 12 2 nonen 0
|
||||||
|
350 16 3 nonen 0
|
||||||
|
350 10 4 nonen 0
|
||||||
|
350 16 5 nonen 0
|
||||||
|
351 30 0 endog 0
|
||||||
|
351 27 1 endog 1
|
||||||
|
351 23 2 endog 2
|
||||||
|
351 20 3 endog 3
|
||||||
|
351 12 4 endog 4
|
||||||
|
351 11 5 endog 5
|
||||||
|
352 21 0 endog 0
|
||||||
|
352 19 1 endog 1
|
||||||
|
352 18 2 endog 2
|
||||||
|
352 15 3 endog 3
|
||||||
|
352 18 4 endog 4
|
||||||
|
352 19 5 endog 5
|
||||||
|
353 27 0 endog 0
|
||||||
|
353 21 1 endog 1
|
||||||
|
353 24 2 endog 2
|
||||||
|
353 22 3 endog 3
|
||||||
|
353 16 4 endog 4
|
||||||
|
353 11 5 endog 5
|
||||||
|
354 28 0 endog 0
|
||||||
|
354 27 1 endog 1
|
||||||
|
354 27 2 endog 2
|
||||||
|
354 26 3 endog 3
|
||||||
|
354 23 4 endog 4
|
||||||
|
354 NA 5 endog 5
|
||||||
|
355 22 0 endog 0
|
||||||
|
355 26 1 endog 1
|
||||||
|
355 20 2 endog 2
|
||||||
|
355 13 3 endog 3
|
||||||
|
355 10 4 endog 4
|
||||||
|
355 7 5 endog 5
|
||||||
|
357 27 0 endog 0
|
||||||
|
357 22 1 endog 1
|
||||||
|
357 24 2 endog 2
|
||||||
|
357 25 3 endog 3
|
||||||
|
357 19 4 endog 4
|
||||||
|
357 19 5 endog 5
|
||||||
|
360 21 0 endog 0
|
||||||
|
360 28 1 endog 1
|
||||||
|
360 27 2 endog 2
|
||||||
|
360 29 3 endog 3
|
||||||
|
360 28 4 endog 4
|
||||||
|
360 33 5 endog 5
|
||||||
|
361 30 0 endog 0
|
||||||
|
361 22 1 endog 1
|
||||||
|
361 11 2 endog 2
|
||||||
|
361 8 3 endog 3
|
||||||
|
361 7 4 endog 4
|
||||||
|
361 19 5 endog 5
|
||||||
|
|
||||||
60
exercises/schizo.md
Normal file
60
exercises/schizo.md
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
Exercise: Growth curve model with interaction
|
||||||
|
================
|
||||||
|
|
||||||
|
## Risperidone vs. haloperidol and schizophrenia (Möller et al. 2008)
|
||||||
|
|
||||||
|
Analyze the data from [moeller.csv](../data/moeller.csv):
|
||||||
|
|
||||||
|
- `pans`: Positive and Negative Symptom Scale for schizophrenia
|
||||||
|
- `treat`: medication group
|
||||||
|
- `risp`: atypical neuroleptic risperidone
|
||||||
|
- `halo`: conventional neuroleptic haloperidol
|
||||||
|
|
||||||
|
``` r
|
||||||
|
dat <- read.table("../data/moeller.csv", header = TRUE, sep = ",")
|
||||||
|
dat$id <- factor(dat$id)
|
||||||
|
dat$treat <- factor(dat$treat, levels = c("risp", "halo"))
|
||||||
|
|
||||||
|
lattice::xyplot(pans ~ week, data = dat, groups = treat,
|
||||||
|
type = c("g", "p", "a"), auto.key = TRUE)
|
||||||
|
```
|
||||||
|
|
||||||
|
<img src="schizo_files/figure-gfm/unnamed-chunk-2-1.png" alt="" style="display: block; margin: auto;" />
|
||||||
|
|
||||||
|
- What is the sample size in each treatment group?
|
||||||
|
|
||||||
|
- Estimate a random slope model
|
||||||
|
|
||||||
|
- What are the estimates for the fixed effects and variance
|
||||||
|
components?
|
||||||
|
|
||||||
|
- Fit a model with quadratic time trends for the population and
|
||||||
|
individual subjects
|
||||||
|
|
||||||
|
- Add an interaction with treatment for the linear and the quadratic
|
||||||
|
effects for week and test them
|
||||||
|
|
||||||
|
- Interpret your results: Which model would you choose?
|
||||||
|
|
||||||
|
- Refit the model that you chose with a centered week variable:
|
||||||
|
|
||||||
|
- Compare the estimates for your fixed effects and the covariance
|
||||||
|
components
|
||||||
|
- Which estimates change and why?
|
||||||
|
|
||||||
|
### Reference
|
||||||
|
|
||||||
|
<div id="refs" class="references csl-bib-body hanging-indent">
|
||||||
|
|
||||||
|
<div id="ref-Moeller08" class="csl-entry">
|
||||||
|
|
||||||
|
Möller, H.-J., M. Riedel, M. Jäger, F. Wickelmaier, W. Maier, K.-U.
|
||||||
|
Kühn, G. Buchkremer, et al. 2008. “Short-Term Treatment with Risperidone
|
||||||
|
or Haloperidol in First-Episode Schizophrenia: 8-Week Results of a
|
||||||
|
Randomized Controlled Trial Within the German Research Network on
|
||||||
|
Schizophrenia.” *International Journal of Neuropsychopharmacology* 11
|
||||||
|
(7): 985–97. <https://doi.org/10.1017/S1461145708008791>.
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
BIN
exercises/schizo_files/figure-gfm/unnamed-chunk-2-1.png
Normal file
BIN
exercises/schizo_files/figure-gfm/unnamed-chunk-2-1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.6 KiB |
BIN
figures/cubic-gone-bad.pdf
Normal file
BIN
figures/cubic-gone-bad.pdf
Normal file
Binary file not shown.
BIN
figures/cubic.pdf
Normal file
BIN
figures/cubic.pdf
Normal file
Binary file not shown.
BIN
figures/hdrs-box.pdf
Normal file
BIN
figures/hdrs-box.pdf
Normal file
Binary file not shown.
BIN
figures/hdrs-caterpillar.pdf
Normal file
BIN
figures/hdrs-caterpillar.pdf
Normal file
Binary file not shown.
BIN
figures/hdrs-ind_pred-quad.pdf
Normal file
BIN
figures/hdrs-ind_pred-quad.pdf
Normal file
Binary file not shown.
BIN
figures/hdrs-ind_pred-randomslope.pdf
Normal file
BIN
figures/hdrs-ind_pred-randomslope.pdf
Normal file
Binary file not shown.
197
figures/hdrs-plots.R
Normal file
197
figures/hdrs-plots.R
Normal file
@ -0,0 +1,197 @@
|
|||||||
|
# hdrs-plots.R
|
||||||
|
#
|
||||||
|
# Example on growth curve models for Reisby et al. (1977)
|
||||||
|
#
|
||||||
|
# content: (1) Read data
|
||||||
|
# (2) Linear analysis
|
||||||
|
# (3) Fitting quadratic model
|
||||||
|
# (3) Centering of variables
|
||||||
|
# (4) Check random effects structure
|
||||||
|
# (5) Check model assumptions
|
||||||
|
#
|
||||||
|
# input: resiby.txt
|
||||||
|
# output: hdrs-ind_pred-randomslope.pdf
|
||||||
|
# hdrs-ind_pred-quad.pdf
|
||||||
|
# hdrs-caterpillar.pdf
|
||||||
|
# hdrs_shrinkage_int-week.pdf
|
||||||
|
# hdrs_shrinkage_int-weeksq.pdf
|
||||||
|
# hdrs_shrinkage_week-weeksq.pdf
|
||||||
|
#
|
||||||
|
# last mod: 2026-04-20, NW
|
||||||
|
|
||||||
|
library("lme4")
|
||||||
|
library("lattice")
|
||||||
|
|
||||||
|
#--------------- (1) Read data ------------------------------------------------
|
||||||
|
|
||||||
|
dat <- read.table("../data/reisby.txt", header = TRUE)
|
||||||
|
dat$id <- factor(dat$id)
|
||||||
|
dat$diag <- factor(dat$diag, levels = c("nonen", "endog"))
|
||||||
|
dat <- na.omit(dat) # drop missing values
|
||||||
|
|
||||||
|
pdf("hdrs-box.pdf", height = 3.375, width = 3.375, pointsize = 10)
|
||||||
|
|
||||||
|
par(mai = c(.6, .6, .1, .1), mgp = c(2, .4, 0))
|
||||||
|
boxplot(hamd ~ week, dat,
|
||||||
|
col = "#3CB4DC",
|
||||||
|
ylab = "HDRS score",
|
||||||
|
xlab = "Time (week)")
|
||||||
|
|
||||||
|
dev.off()
|
||||||
|
|
||||||
|
#--------------- (2) Linear analysis ------------------------------------------
|
||||||
|
|
||||||
|
# random slope model
|
||||||
|
lme2 <- lmer(hamd ~ week + (week | id), dat, REML = FALSE)
|
||||||
|
|
||||||
|
pdf("hdrs-ind_pred-randomslope.pdf", height = 6, width = 6)
|
||||||
|
|
||||||
|
xyplot(hamd + predict(lme2) ~ week | id, data = dat,
|
||||||
|
type = c("p", "l", "g"),
|
||||||
|
#pch = 16,
|
||||||
|
ratio = "xy",
|
||||||
|
col = c("#3CB4DC", "#FF6900"),
|
||||||
|
distribute.type = TRUE,
|
||||||
|
layout = c(11, 6),
|
||||||
|
ylab = "HDRS score",
|
||||||
|
xlab = "Time (week)")
|
||||||
|
|
||||||
|
dev.off()
|
||||||
|
|
||||||
|
#--------------- (3) Fitting quadratic model ----------------------------------
|
||||||
|
|
||||||
|
# model with quadratic time trend
|
||||||
|
lme3 <- lmer(hamd ~ week + I(week^2) + (week + I(week^2) | id), dat,
|
||||||
|
REML = FALSE)
|
||||||
|
|
||||||
|
pdf("hdrs-ind_pred-quad.pdf", height = 6, width = 6)
|
||||||
|
|
||||||
|
xyplot(hamd + predict(lme3) ~ week | id, data = dat,
|
||||||
|
type = c("p", "l", "g"),
|
||||||
|
#pch = 16,
|
||||||
|
ratio = "xy",
|
||||||
|
col = c("#3CB4DC", "#FF6900"),
|
||||||
|
distribute.type = TRUE,
|
||||||
|
layout = c(11, 6),
|
||||||
|
ylab = "HDRS score",
|
||||||
|
xlab = "Time (week)")
|
||||||
|
|
||||||
|
dev.off()
|
||||||
|
|
||||||
|
#--------------- (4) Check random effects structure ---------------
|
||||||
|
|
||||||
|
pdf("hdrs-caterpillar.pdf", height = 8, width = 20)
|
||||||
|
|
||||||
|
dotplot(ranef(lme3), col = "#3CB4DC",
|
||||||
|
scales = list(x = list(relation = "free")))[[1]]
|
||||||
|
|
||||||
|
dev.off()
|
||||||
|
|
||||||
|
# Shrinkage plots
|
||||||
|
|
||||||
|
df <- coef(lmList(hamd ~ week_c + I(week_c^2) | id, dat))
|
||||||
|
cc1 <- as.data.frame(coef(lme3reml)$id)
|
||||||
|
names(cc1) <- c("A", "B", "C")
|
||||||
|
|
||||||
|
df <- cbind(df, cc1)
|
||||||
|
ff <- fixef(lme3reml)
|
||||||
|
|
||||||
|
|
||||||
|
pdf("hdrs_shrinkage_int-week.pdf", height = 6, width = 6, pointsize = 10)
|
||||||
|
|
||||||
|
with(df,
|
||||||
|
xyplot(`(Intercept)` ~ week_c, aspect = 1,
|
||||||
|
x1 = B, y1 = A,
|
||||||
|
panel = function(x, y, x1, y1, subscripts, ...) {
|
||||||
|
panel.grid(h = -1, v = -1)
|
||||||
|
x1 <- x1[subscripts]
|
||||||
|
y1 <- y1[subscripts]
|
||||||
|
larrows(x, y, x1, y1, type = "closed", length = 0.1, fill = "black",
|
||||||
|
angle = 15, ...)
|
||||||
|
lpoints(x, y,
|
||||||
|
pch = 16,
|
||||||
|
col = trellis.par.get("superpose.symbol")$col[2])
|
||||||
|
lpoints(x1, y1,
|
||||||
|
pch = 16,
|
||||||
|
col = trellis.par.get("superpose.symbol")$col[1])
|
||||||
|
lpoints(ff[2], ff[1],
|
||||||
|
pch = 16,
|
||||||
|
col = trellis.par.get("superpose.symbol")$col[3])
|
||||||
|
},
|
||||||
|
xlab = "week_c",
|
||||||
|
ylab = "(Intercept)",
|
||||||
|
key = list(space = "top", columns = 3,
|
||||||
|
text = list(c("Mixed model", "Within-subject", "Population")),
|
||||||
|
points = list(col = trellis.par.get("superpose.symbol")$col[1:3],
|
||||||
|
pch = 16))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
dev.off()
|
||||||
|
|
||||||
|
|
||||||
|
pdf("hdrs_shrinkage_int-weeksq.pdf", height = 6, width = 6, pointsize = 10)
|
||||||
|
|
||||||
|
with(df,
|
||||||
|
xyplot(`(Intercept)` ~ `I(week_c^2)`, aspect = 1,
|
||||||
|
x1 = C, y1 = A,
|
||||||
|
panel = function(x, y, x1, y1, subscripts, ...) {
|
||||||
|
panel.grid(h = -1, v = -1)
|
||||||
|
x1 <- x1[subscripts]
|
||||||
|
y1 <- y1[subscripts]
|
||||||
|
larrows(x, y, x1, y1, type = "closed", length = 0.1, fill = "black",
|
||||||
|
angle = 15, ...)
|
||||||
|
lpoints(x, y,
|
||||||
|
pch = 16,
|
||||||
|
col = trellis.par.get("superpose.symbol")$col[2])
|
||||||
|
lpoints(x1, y1,
|
||||||
|
pch = 16,
|
||||||
|
col = trellis.par.get("superpose.symbol")$col[1])
|
||||||
|
lpoints(ff[3], ff[1],
|
||||||
|
pch = 16,
|
||||||
|
col = trellis.par.get("superpose.symbol")$col[3])
|
||||||
|
},
|
||||||
|
xlab = expression(week_c^2),
|
||||||
|
ylab = "(Intercept)",
|
||||||
|
key = list(space = "top", columns = 3,
|
||||||
|
text = list(c("Mixed model", "Within-subject", "Population")),
|
||||||
|
points = list(col = trellis.par.get("superpose.symbol")$col[1:3],
|
||||||
|
pch = 16))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
dev.off()
|
||||||
|
|
||||||
|
|
||||||
|
pdf("hdrs_shrinkage_week-weeksq.pdf", height = 6, width = 6, pointsize = 10)
|
||||||
|
|
||||||
|
with(df,
|
||||||
|
xyplot(week_c ~ `I(week_c^2)`, aspect = 1,
|
||||||
|
x1 = C, y1 = B,
|
||||||
|
panel = function(x, y, x1, y1, subscripts, ...) {
|
||||||
|
panel.grid(h = -1, v = -1)
|
||||||
|
x1 <- x1[subscripts]
|
||||||
|
y1 <- y1[subscripts]
|
||||||
|
larrows(x, y, x1, y1, type = "closed", length = 0.1, fill = "black",
|
||||||
|
angle = 15, ...)
|
||||||
|
lpoints(x, y,
|
||||||
|
pch = 16,
|
||||||
|
col = trellis.par.get("superpose.symbol")$col[2])
|
||||||
|
lpoints(x1, y1,
|
||||||
|
pch = 16,
|
||||||
|
col = trellis.par.get("superpose.symbol")$col[1])
|
||||||
|
lpoints(ff[3], ff[2],
|
||||||
|
pch = 16,
|
||||||
|
col = trellis.par.get("superpose.symbol")$col[3])
|
||||||
|
},
|
||||||
|
xlab = expression(week_c^2),
|
||||||
|
ylab = "week_c",
|
||||||
|
key = list(space = "top", columns = 3,
|
||||||
|
text = list(c("Mixed model", "Within-subject", "Population")),
|
||||||
|
points = list(col = trellis.par.get("superpose.symbol")$col[1:3],
|
||||||
|
pch = 16))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
dev.off()
|
||||||
|
|
||||||
BIN
figures/hdrs-quad.pdf
Normal file
BIN
figures/hdrs-quad.pdf
Normal file
Binary file not shown.
BIN
figures/hdrs_shrinkage_int-week.pdf
Normal file
BIN
figures/hdrs_shrinkage_int-week.pdf
Normal file
Binary file not shown.
BIN
figures/hdrs_shrinkage_int-weeksq.pdf
Normal file
BIN
figures/hdrs_shrinkage_int-weeksq.pdf
Normal file
Binary file not shown.
BIN
figures/hdrs_shrinkage_week-weeksq.pdf
Normal file
BIN
figures/hdrs_shrinkage_week-weeksq.pdf
Normal file
Binary file not shown.
BIN
figures/iwm_logo_rgb.pdf
Normal file
BIN
figures/iwm_logo_rgb.pdf
Normal file
Binary file not shown.
BIN
figures/quad.pdf
Normal file
BIN
figures/quad.pdf
Normal file
Binary file not shown.
BIN
figures/sleep_box.pdf
Normal file
BIN
figures/sleep_box.pdf
Normal file
Binary file not shown.
BIN
figures/sleep_random_intercept.pdf
Normal file
BIN
figures/sleep_random_intercept.pdf
Normal file
Binary file not shown.
BIN
figures/sleep_random_slope.pdf
Normal file
BIN
figures/sleep_random_slope.pdf
Normal file
Binary file not shown.
BIN
figures/sleep_shrinkfit.pdf
Normal file
BIN
figures/sleep_shrinkfit.pdf
Normal file
Binary file not shown.
BIN
figures/sleep_subjects.pdf
Normal file
BIN
figures/sleep_subjects.pdf
Normal file
Binary file not shown.
BIN
slides/lead_longitudinal.pdf
Normal file
BIN
slides/lead_longitudinal.pdf
Normal file
Binary file not shown.
789
slides/lead_longitudinal.tex
Normal file
789
slides/lead_longitudinal.tex
Normal file
@ -0,0 +1,789 @@
|
|||||||
|
\documentclass[aspectratio=169]{beamer}
|
||||||
|
|
||||||
|
\usepackage{listings}
|
||||||
|
\usepackage[utf8,latin1]{inputenc}
|
||||||
|
\usepackage[style = apa, backend = biber, natbib = true]{biblatex}
|
||||||
|
\usepackage{tikz}
|
||||||
|
\addbibresource{lit.bib}
|
||||||
|
|
||||||
|
\makeatletter \def\newblock{\beamer@newblock} \makeatother
|
||||||
|
|
||||||
|
\beamertemplatenavigationsymbolsempty
|
||||||
|
\setbeamertemplate{itemize items}[circle]
|
||||||
|
\setbeamertemplate{section in toc}[circle]
|
||||||
|
\mode<beamer>{\setbeamercolor{math text displayed}{fg=iwmgray}}
|
||||||
|
\setbeamercolor{block body}{bg=iwmorange!50!white}
|
||||||
|
\setbeamercolor{block title}{fg=white, bg=iwmorange}
|
||||||
|
|
||||||
|
% Definitions for biblatex
|
||||||
|
\setbeamercolor{bibliography entry note}{fg=iwmgray}
|
||||||
|
\setbeamercolor{bibliography entry author}{fg=iwmgray}
|
||||||
|
\setbeamertemplate{bibliography item}{}
|
||||||
|
|
||||||
|
\definecolor{iwmorange}{RGB}{255,105,0}
|
||||||
|
\definecolor{iwmgray}{RGB}{67,79,79}
|
||||||
|
\definecolor{iwmblue}{RGB}{60,180,220}
|
||||||
|
\definecolor{iwmgreen}{RGB}{145,200,110}
|
||||||
|
\definecolor{iwmpurple}{RGB}{120,0,75}
|
||||||
|
|
||||||
|
\setbeamercolor{title}{fg=iwmorange}
|
||||||
|
\setbeamercolor{frametitle}{fg=iwmorange}
|
||||||
|
\setbeamercolor{structure}{fg=iwmorange}
|
||||||
|
\setbeamercolor{normal text}{fg=iwmgray}
|
||||||
|
\setbeamercolor{author}{fg=iwmgray}
|
||||||
|
\setbeamercolor{date}{fg=iwmgray}
|
||||||
|
|
||||||
|
\lstset{language = R,%
|
||||||
|
basicstyle = \ttfamily\color{iwmgray},
|
||||||
|
frame = single,
|
||||||
|
rulecolor = \color{iwmgray},
|
||||||
|
commentstyle = \slshape\color{iwmgreen},
|
||||||
|
keywordstyle = \bfseries\color{iwmgray},
|
||||||
|
identifierstyle = \color{iwmpurple},
|
||||||
|
stringstyle = \color{iwmblue},
|
||||||
|
numbers = none,%left,numberstyle = \tiny,
|
||||||
|
basewidth = {.5em, .4em},
|
||||||
|
showstringspaces = false,
|
||||||
|
emphstyle = \color{red!50!white}}
|
||||||
|
|
||||||
|
\title{Introduction to mixed-effects models}
|
||||||
|
\subtitle{(for longitudinal data)}
|
||||||
|
\author{Nora Wickelmaier}
|
||||||
|
\institute{\includegraphics[scale=.2]{../figures/iwm_logo_rgb}}
|
||||||
|
\date{2026-04-28}
|
||||||
|
|
||||||
|
\AtBeginSection[]{
|
||||||
|
\frame{
|
||||||
|
\tableofcontents[sectionstyle=show/hide, subsectionstyle=show/show/hide]}}
|
||||||
|
|
||||||
|
\setbeamertemplate{headline}{
|
||||||
|
\begin{beamercolorbox}{section in head}
|
||||||
|
\vskip5pt\insertsectionnavigationhorizontal{\paperwidth}{}{}\vskip2pt
|
||||||
|
\end{beamercolorbox}
|
||||||
|
}
|
||||||
|
|
||||||
|
\setbeamertemplate{footline}{\vskip-2pt\hfill\insertframenumber$\;$\vskip2pt}
|
||||||
|
|
||||||
|
\begin{document}
|
||||||
|
|
||||||
|
\begin{frame}{}
|
||||||
|
\thispagestyle{empty}
|
||||||
|
\titlepage
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}{Plan for today}
|
||||||
|
\begin{itemize}[<+->]
|
||||||
|
\item We will walk through an example for a longitudinal data set (6 time
|
||||||
|
points per subject)
|
||||||
|
\item I will explain the general concepts with the slides
|
||||||
|
\item We will switch to R and use the lme4 package to fit the models
|
||||||
|
\item You will use R to fit the model and try out different specifications
|
||||||
|
\item We will discuss the results
|
||||||
|
\item All the materials are here:
|
||||||
|
\url{https://gitea.iwm-tuebingen.de/nwickelmaier/lead_longitudinal}
|
||||||
|
\\~\\
|
||||||
|
\item[$\to$] Try to code along in R! Ask as many questions as possible
|
||||||
|
\end{itemize}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}{Outline}
|
||||||
|
\tableofcontents
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\section[Introduction]{Introduction to longitudinal data}
|
||||||
|
|
||||||
|
\begin{frame}[fragile]{Longitudinal data}
|
||||||
|
\begin{itemize}
|
||||||
|
\item Consist of repeated measurements on the same subject taken over time
|
||||||
|
\item Are a frequent use case for mixed-effects models
|
||||||
|
\item Contain time as a predictor:
|
||||||
|
time trends within and between subjects are of interest
|
||||||
|
\end{itemize}
|
||||||
|
\begin{lstlisting}
|
||||||
|
library("lme4")
|
||||||
|
data("sleepstudy")
|
||||||
|
?sleepstudy
|
||||||
|
str(sleepstudy)
|
||||||
|
summary(sleepstudy)
|
||||||
|
head(sleepstudy)
|
||||||
|
\end{lstlisting}
|
||||||
|
\nocite{Bates10lme4, Bates15}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}{Sleep study}
|
||||||
|
\begin{itemize}
|
||||||
|
\item Average reaction time per day for subjects in a sleep deprivation
|
||||||
|
study
|
||||||
|
\item On day 0, the subjects had their normal amount of sleep
|
||||||
|
\item Starting that night they were restricted to 3 hours of sleep per
|
||||||
|
night
|
||||||
|
\item Observations represent the average reaction time on a series of
|
||||||
|
tests given each day to each subject
|
||||||
|
\end{itemize}
|
||||||
|
|
||||||
|
\vfill
|
||||||
|
A data frame with 180 observations on the following 3 variables\\~\\
|
||||||
|
|
||||||
|
\begin{tabular}{ll}
|
||||||
|
\hline
|
||||||
|
\texttt{Reaction} & Average reaction time (ms) \\
|
||||||
|
\texttt{Days} & Number of days of sleep deprivation \\
|
||||||
|
\texttt{Subject} & Subject number on which the observation was made \\
|
||||||
|
\hline
|
||||||
|
\end{tabular}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
|
||||||
|
\begin{frame}[fragile]{Visualization of data}
|
||||||
|
\begin{columns}
|
||||||
|
\begin{column}{.53\textwidth}
|
||||||
|
\includegraphics[scale=.8]{../figures/sleep_box}
|
||||||
|
\end{column}
|
||||||
|
\begin{column}{.55\textwidth}
|
||||||
|
\begin{lstlisting}
|
||||||
|
boxplot(Reaction ~ Days, sleepstudy)
|
||||||
|
\end{lstlisting}
|
||||||
|
\end{column}
|
||||||
|
\end{columns}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}[fragile]{Visualization of individual data}
|
||||||
|
\begin{columns}
|
||||||
|
\begin{column}{.53\textwidth}
|
||||||
|
\includegraphics[scale=.5]{../figures/sleep_subjects}
|
||||||
|
\end{column}
|
||||||
|
\begin{column}{.55\textwidth}
|
||||||
|
\begin{lstlisting}
|
||||||
|
library("lattice")
|
||||||
|
|
||||||
|
xyplot(Reaction ~ Days | Subject,
|
||||||
|
data = sleepstudy,
|
||||||
|
type = c("g", "b"),
|
||||||
|
xlab = "Days of sleep deprivation",
|
||||||
|
ylab = "Average reaction time (ms)",
|
||||||
|
aspect = "xy")
|
||||||
|
\end{lstlisting}
|
||||||
|
\end{column}
|
||||||
|
\end{columns}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}{Random intercept model}
|
||||||
|
\begin{columns}
|
||||||
|
\begin{column}{.5\textwidth}
|
||||||
|
\begin{itemize}
|
||||||
|
\item The random intercept model adds a random intercept for each subject
|
||||||
|
\[
|
||||||
|
y_{ij} = \beta_0 + \beta_1\,Days_{ij} + \upsilon_{0i} + \varepsilon_i
|
||||||
|
\]
|
||||||
|
with $\upsilon_{0i} \overset{iid}{\sim} N(0, \sigma^2_{\upsilon})$,
|
||||||
|
$\varepsilon_{ij} \overset{iid}{\sim} N(0, \sigma^2_\varepsilon)$
|
||||||
|
\item The slope is identical for each subject (and the population)
|
||||||
|
\end{itemize}
|
||||||
|
\vspace{2.2cm}
|
||||||
|
\end{column}
|
||||||
|
\begin{column}{.5\textwidth}
|
||||||
|
\includegraphics[scale=.5]{../figures/sleep_random_intercept}
|
||||||
|
\end{column}
|
||||||
|
\end{columns}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
|
||||||
|
\begin{frame}{Fixed and random effects for random intercept model}
|
||||||
|
% m1 <- lmer(Reaction ~ Days + (1 | Subject), sleepstudy)
|
||||||
|
% tab <- cbind(
|
||||||
|
% sleepstudy[, c("Subject", "Days", "Reaction")],
|
||||||
|
% yhat = fixef(m1)["(Intercept)"] +
|
||||||
|
% fixef(m1)["Days"] * sleepstudy[["Days"]] +
|
||||||
|
% ranef(m1)[["Subject"]][["(Intercept)"]][sleepstudy[["Subject"]]] +
|
||||||
|
% resid(m1),
|
||||||
|
% Intercept = fixef(m1)["(Intercept)"],
|
||||||
|
% Slope = fixef(m1)["Days"] * sleepstudy[["Days"]],
|
||||||
|
% SubInt = ranef(m1)[["Subject"]][["(Intercept)"]][sleepstudy[["Subject"]]],
|
||||||
|
% Resid = resid(m1)
|
||||||
|
% )
|
||||||
|
% tab <- cbind(
|
||||||
|
% sleepstudy[, c("Subject", "Days", "Reaction")],
|
||||||
|
% Intercept = fixef(m1)["(Intercept)"] |> unname(),
|
||||||
|
% Slope = fixef(m1)["Days"] |> unname(),
|
||||||
|
% SubInt = ranef(m1)[["Subject"]][["(Intercept)"]][sleepstudy[["Subject"]]],
|
||||||
|
% Resid = resid(m1)
|
||||||
|
% )
|
||||||
|
% print(
|
||||||
|
% xtable(tab[tab$Subject %in% c("308", "309", "310"), ],
|
||||||
|
% digits = c(NA, 0, 0, 0, 1, 1, 1, 1)),
|
||||||
|
% include.rownames = FALSE, math.style.negative = TRUE)
|
||||||
|
% )
|
||||||
|
% latex table generated in R 4.5.2 by xtable 1.8-8 package
|
||||||
|
% Thu Mar 5 11:18:32 2026
|
||||||
|
\centering\footnotesize
|
||||||
|
\begin{tabular}{ccccccc}
|
||||||
|
\hline
|
||||||
|
Subject & Days & Reaction & Fixed & & Random & \\
|
||||||
|
\cline{4-5}
|
||||||
|
\cline{6-7}
|
||||||
|
& & & Intercept & Slope & SubInt & Resid \\
|
||||||
|
\hline
|
||||||
|
308 & 0 & 250 & 251.4 & 10.5 & 40.8 & $-$42.6 \\
|
||||||
|
308 & 1 & 259 & 251.4 & 10.5 & 40.8 & $-$44.0 \\
|
||||||
|
308 & 2 & 251 & 251.4 & 10.5 & 40.8 & $-$62.3 \\
|
||||||
|
\vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots\\
|
||||||
|
308 & 8 & 431 & 251.4 & 10.5 & 40.8 & 54.7 \\
|
||||||
|
308 & 9 & 466 & 251.4 & 10.5 & 40.8 & 80.0 \\
|
||||||
|
\hline
|
||||||
|
309 & 0 & 223 & 251.4 & 10.5 & $-$77.8 & 49.2 \\
|
||||||
|
309 & 1 & 205 & 251.4 & 10.5 & $-$77.8 & 21.2 \\
|
||||||
|
309 & 2 & 203 & 251.4 & 10.5 & $-$77.8 & 8.5 \\
|
||||||
|
\vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots\\
|
||||||
|
\hline
|
||||||
|
310 & 0 & 199 & 251.4 & 10.5 & $-$63.1 & 10.8 \\
|
||||||
|
310 & 1 & 194 & 251.4 & 10.5 & $-$63.1 & $-$4.4 \\
|
||||||
|
310 & 2 & 234 & 251.4 & 10.5 & $-$63.1 & 25.1 \\
|
||||||
|
\vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots\\
|
||||||
|
\hline
|
||||||
|
& & & & & $\sigma^2_{\upsilon}$ & $\sigma^2_\varepsilon$\\
|
||||||
|
\end{tabular}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}[fragile]{Random slope model}
|
||||||
|
\begin{columns}
|
||||||
|
\begin{column}{.5\textwidth}
|
||||||
|
\begin{itemize}
|
||||||
|
\item The random slope model adds a random intercept and a random slope for each
|
||||||
|
subject
|
||||||
|
\[
|
||||||
|
y_{ij} = \beta_0 + \beta_1\,Days_{ij} + \upsilon_{0i} +
|
||||||
|
\upsilon_{1i}\,Days_{ij} + \varepsilon_{ij}
|
||||||
|
\]
|
||||||
|
with
|
||||||
|
\begin{align*}
|
||||||
|
\begin{pmatrix} \upsilon_{0i}\\ \upsilon_{1i} \end{pmatrix} &\overset{iid}{\sim}
|
||||||
|
N \left(\begin{pmatrix} 0\\ 0 \end{pmatrix}, \, \mathbf{\Sigma}_\upsilon =
|
||||||
|
\begin{pmatrix}
|
||||||
|
\sigma^2_{\upsilon_0} & \sigma_{\upsilon_0 \upsilon_1} \\
|
||||||
|
\sigma_{\upsilon_0 \upsilon_1} & \sigma^2_{\upsilon_1} \\
|
||||||
|
\end{pmatrix} \right)
|
||||||
|
\\
|
||||||
|
\varepsilon_{ij} & \overset{iid}{\sim} N(0, \sigma^2_\varepsilon)
|
||||||
|
\end{align*}
|
||||||
|
\vspace{-.5cm}
|
||||||
|
\item Individual slopes for each subject
|
||||||
|
\end{itemize}
|
||||||
|
\end{column}
|
||||||
|
\begin{column}{.5\textwidth}
|
||||||
|
\includegraphics[scale=.5]{../figures/sleep_random_slope}
|
||||||
|
\end{column}
|
||||||
|
\end{columns}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
|
||||||
|
\begin{frame}{Fixed and random effects for random slope model}
|
||||||
|
% m2 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
|
||||||
|
% tab <- cbind(
|
||||||
|
% sleepstudy[, c("Subject", "Days", "Reaction")],
|
||||||
|
% yhat = fixef(m2)["(Intercept)"] +
|
||||||
|
% fixef(m2)["Days"] * sleepstudy[["Days"]] +
|
||||||
|
% ranef(m2)[["Subject"]][["(Intercept)"]][sleepstudy[["Subject"]]] +
|
||||||
|
% ranef(m2)[["Subject"]][["Days"]][sleepstudy[["Subject"]]] *
|
||||||
|
% sleepstudy[["Days"]] +
|
||||||
|
% resid(m2),
|
||||||
|
% Intercept = fixef(m2)["(Intercept)"],
|
||||||
|
% Slope = fixef(m2)["Days"] * sleepstudy[["Days"]],
|
||||||
|
% SubInt = ranef(m2)[["Subject"]][["(Intercept)"]][sleepstudy[["Subject"]]],
|
||||||
|
% SubSlp = ranef(m2)[["Subject"]][["Days"]][sleepstudy[["Subject"]]] *
|
||||||
|
% sleepstudy[["Days"]],
|
||||||
|
% Resid = resid(m2)
|
||||||
|
% )
|
||||||
|
% tab <- cbind(
|
||||||
|
% sleepstudy[, c("Subject", "Days", "Reaction")],
|
||||||
|
% Intercept = fixef(m2)["(Intercept)"] |> unname(),
|
||||||
|
% Slope = fixef(m2)["Days"] |> unname(),
|
||||||
|
% SubInt = ranef(m2)[["Subject"]][["(Intercept)"]][sleepstudy[["Subject"]]],
|
||||||
|
% SubSlp = ranef(m2)[["Subject"]][["Days"]][sleepstudy[["Subject"]]],
|
||||||
|
% Resid = resid(m2)
|
||||||
|
% )
|
||||||
|
% print(
|
||||||
|
% xtable(tab[tab$Subject %in% c("308", "309", "310"), ],
|
||||||
|
% digits = c(NA, 0, 0, 0, 1, 1, 1, 1, 1)),
|
||||||
|
% include.rownames = FALSE, math.style.negative = TRUE)
|
||||||
|
% )
|
||||||
|
% latex table generated in R 4.5.2 by xtable 1.8-8 package
|
||||||
|
% Thu Mar 5 11:18:32 2026
|
||||||
|
\centering\footnotesize
|
||||||
|
\begin{tabular}{cccccccc}
|
||||||
|
\hline
|
||||||
|
Subject & Days & Reaction & Fixed & & Random & & \\
|
||||||
|
\cline{4-5}
|
||||||
|
\cline{6-8}
|
||||||
|
& & & Intercept & Slope & SubInt & SubSlp & Resid \\
|
||||||
|
\hline
|
||||||
|
308 & 0 & 250 & 251.4 & 10.5 & 2.3 & 9.2 & $-$4.1 \\
|
||||||
|
308 & 1 & 259 & 251.4 & 10.5 & 2.3 & 9.2 & $-$14.6 \\
|
||||||
|
308 & 2 & 251 & 251.4 & 10.5 & 2.3 & 9.2 & $-$42.2 \\
|
||||||
|
\vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots \\
|
||||||
|
308 & 8 & 431 & 251.4 & 10.5 & 2.3 & 9.2 & 19.6 \\
|
||||||
|
308 & 9 & 466 & 251.4 & 10.5 & 2.3 & 9.2 & 35.7 \\
|
||||||
|
\hline
|
||||||
|
309 & 0 & 223 & 251.4 & 10.5 & $-$40.4 & $-$8.6 & 11.7 \\
|
||||||
|
309 & 1 & 205 & 251.4 & 10.5 & $-$40.4 & $-$8.6 & $-$7.6 \\
|
||||||
|
309 & 2 & 203 & 251.4 & 10.5 & $-$40.4 & $-$8.6 & $-$11.7 \\
|
||||||
|
\vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots \\
|
||||||
|
\hline
|
||||||
|
310 & 0 & 199 & 251.4 & 10.5 & $-$39.0 & $-$5.4 & $-$13.4 \\
|
||||||
|
310 & 1 & 194 & 251.4 & 10.5 & $-$39.0 & $-$5.4 & $-$23.1 \\
|
||||||
|
310 & 2 & 234 & 251.4 & 10.5 & $-$39.0 & $-$5.4 & 11.8 \\
|
||||||
|
\vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots \\
|
||||||
|
\hline
|
||||||
|
&&&&& $\sigma^2_{\upsilon_0}$ & $\sigma^2_{\upsilon_1}$ &
|
||||||
|
$\sigma^2_\varepsilon$\\[-6pt]
|
||||||
|
&&&&& \multicolumn{2}{c}{$\sigma_{\upsilon_0\upsilon_1}$} & \\
|
||||||
|
\end{tabular}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}{Partial pooling}
|
||||||
|
\begin{columns}
|
||||||
|
\begin{column}{.5\textwidth}
|
||||||
|
\includegraphics[scale=.5]{../figures/sleep_shrinkfit}
|
||||||
|
\end{column}
|
||||||
|
\begin{column}{.5\textwidth}
|
||||||
|
\begin{itemize}
|
||||||
|
\item Within-subject regression line shows regression line fitted to
|
||||||
|
data for each individual
|
||||||
|
\item Population regression line shows fixed effects for mixed-effects
|
||||||
|
model
|
||||||
|
\item Mixed model regression line shows individual regression lines as
|
||||||
|
predicted by mixed-effects models
|
||||||
|
\end{itemize}
|
||||||
|
\end{column}
|
||||||
|
\end{columns}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\section[Growth curve model]{Example: Depression and Imipramin}
|
||||||
|
|
||||||
|
|
||||||
|
\begin{frame}{Quadratic time trends}
|
||||||
|
\begin{columns}
|
||||||
|
\begin{column}{.35\textwidth}
|
||||||
|
\begin{itemize}
|
||||||
|
\item A lot of times the assumption of a linear time trend is too
|
||||||
|
simple
|
||||||
|
\item Change is not happening unbraked linearly but flattens out
|
||||||
|
\end{itemize}
|
||||||
|
\end{column}
|
||||||
|
\begin{column}{.65\textwidth}
|
||||||
|
\includegraphics[width=9cm]{../figures/quad}
|
||||||
|
\end{column}
|
||||||
|
\end{columns}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}{Quadratic time trends}
|
||||||
|
\begin{itemize}
|
||||||
|
\item Quadratic regression model
|
||||||
|
\begin{align*}
|
||||||
|
y_{ij} &= b_{0i} + b_{1i}\,t_{ij} + b_{2i}\,t^2_{ij} + \varepsilon_{ij}\\
|
||||||
|
&= b_{0i} + (b_{1i} + b_{2i}\,t_{ij}) t_{ij} + \varepsilon_{ij}
|
||||||
|
\end{align*}
|
||||||
|
\item The linear change depends on time $t$
|
||||||
|
\[
|
||||||
|
\frac{\partial y}{\partial t} = b_{1i} + 2b_{2i} \, t
|
||||||
|
\]
|
||||||
|
\item The intercept $t = -b_{1i}/(2 b_{2i})$ is the point in time when a
|
||||||
|
positive (negative) trend becomes negative (postive)
|
||||||
|
\end{itemize}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}{Depression and Imipramin \citep{ReisbyGram77}}
|
||||||
|
\begin{itemize}
|
||||||
|
\item \citet{ReisbyGram77} studied the effect of Imipramin on 66
|
||||||
|
inpatients treated for depression
|
||||||
|
\item Depression was measured with the Hamilton depression rating scale
|
||||||
|
(HDRS)
|
||||||
|
\item Additionally, the concentration of Imipramin and its metabolite
|
||||||
|
Desipramin was measured in their blood plasma
|
||||||
|
\item Patients were classified into endogenous and non-endogenous
|
||||||
|
depressed
|
||||||
|
\item Depression was measured weekly for 6 time points; the effect of
|
||||||
|
the antidepressant was observed starting at week 2 for four weeks
|
||||||
|
\end{itemize}
|
||||||
|
\vfill
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}{Descriptive statistics}
|
||||||
|
\begin{columns}
|
||||||
|
\begin{column}{.55\textwidth}
|
||||||
|
\includegraphics[scale=.8]{../figures/hdrs-box}
|
||||||
|
\end{column}
|
||||||
|
\begin{column}{.6\textwidth}
|
||||||
|
HDRS score\\[1ex]
|
||||||
|
{\footnotesize
|
||||||
|
\begin{tabular}{rrrrrrr}
|
||||||
|
\hline
|
||||||
|
$t$ & W0 & W1 & W2 & W3 & W4 & W5 \\
|
||||||
|
\hline
|
||||||
|
$M$ & 23.44 & 21.84 & 18.31 & 16.42 & 13.62 & 11.95 \\
|
||||||
|
$SD$ & 4.53 & 4.70 & 5.49 & 6.42 & 6.97 & 7.22 \\
|
||||||
|
$n$ & 61 & 63 & 65 & 65 & 63 & 58 \\
|
||||||
|
\hline
|
||||||
|
\end{tabular}
|
||||||
|
}
|
||||||
|
|
||||||
|
\vspace{.5cm}
|
||||||
|
Empirical correlation matrix of HDRS score\\[1ex]
|
||||||
|
{\footnotesize
|
||||||
|
\begin{tabular}{rrrrrrr}
|
||||||
|
\hline
|
||||||
|
& W0 & W1 & W2 & W3 & W4 & W5 \\
|
||||||
|
\hline
|
||||||
|
Week 0 & 1 & .49 & .41 & .33 & .23 & .18 \\
|
||||||
|
Week 1 & .49 & 1 & .49 & .41 & .31 & .22 \\
|
||||||
|
Week 2 & .41 & .49 & 1 & .74 & .67 & .46 \\
|
||||||
|
Week 3 & .33 & .41 & .74 & 1 & .82 & .57 \\
|
||||||
|
Week 4 & .23 & .31 & .67 & .82 & 1 & .65 \\
|
||||||
|
Week 5 & .18 & .22 & .46 & .57 & .65 & 1 \\
|
||||||
|
\hline
|
||||||
|
\end{tabular}
|
||||||
|
}
|
||||||
|
|
||||||
|
\vspace{1cm}
|
||||||
|
\end{column}
|
||||||
|
\end{columns}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}{Predictions random slope model}
|
||||||
|
\begin{columns}
|
||||||
|
\begin{column}{.6\textwidth}
|
||||||
|
\includegraphics[scale=.5]{../figures/hdrs-ind_pred-randomslope}
|
||||||
|
\end{column}
|
||||||
|
\begin{column}{.4\textwidth}
|
||||||
|
\[
|
||||||
|
y_{ij} = \beta_0 + \beta_1 time + \upsilon_{0i} + \upsilon_{1i} time +
|
||||||
|
\varepsilon_{ij}
|
||||||
|
\]
|
||||||
|
with
|
||||||
|
\begin{align*}
|
||||||
|
\begin{pmatrix} \upsilon_{0i}\\ \upsilon_{1i} \end{pmatrix} &\overset{iid}\sim
|
||||||
|
N \left(\begin{pmatrix} 0\\ 0 \end{pmatrix}, \, \boldsymbol{\Sigma}_\upsilon =
|
||||||
|
\begin{pmatrix}
|
||||||
|
\sigma^2_{\upsilon_0} & \sigma_{\upsilon_0 \upsilon_1} \\
|
||||||
|
\sigma_{\upsilon_0 \upsilon_1} & \sigma^2_{\upsilon_1} \\
|
||||||
|
\end{pmatrix} \right)\\
|
||||||
|
\boldsymbol{\varepsilon}_i &\overset{iid}\sim N(\mathbf{0}, \, \sigma^2
|
||||||
|
\mathbf{I}_{n_i})
|
||||||
|
\end{align*}
|
||||||
|
\end{column}
|
||||||
|
\end{columns}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}[fragile]{}
|
||||||
|
\begin{block}{Exercise}
|
||||||
|
\begin{itemize}
|
||||||
|
\item What would be a possible extension of this model?\pause
|
||||||
|
\item Write down the model equations
|
||||||
|
\begin{itemize}
|
||||||
|
\item What changes for the fixed effects?
|
||||||
|
\item How do the variance components for the random effects change?
|
||||||
|
\end{itemize}\pause
|
||||||
|
\item How can we interpret the random quadratic effects for this model?\pause
|
||||||
|
\item How do we add (random) quadratic effects to a random slope model
|
||||||
|
using \texttt{lme4::lmer()}?\pause
|
||||||
|
\item Fit this growth curve model in R\pause
|
||||||
|
\end{itemize}
|
||||||
|
\end{block}
|
||||||
|
\begin{lstlisting}
|
||||||
|
dat <- read.table("data/reisby.txt", header = TRUE)
|
||||||
|
dat$id <- factor(dat$id)
|
||||||
|
dat$diag <- factor(dat$diag, levels = c("nonen", "endog"))
|
||||||
|
dat <- na.omit(dat) # drop missing values
|
||||||
|
\end{lstlisting}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}[fragile]{Model with quadratic trend}
|
||||||
|
\begin{itemize}
|
||||||
|
\item Model with quadratic individual and quadratic group trend
|
||||||
|
\[
|
||||||
|
y_{ij} = \beta_0 + \beta_1\,t_{ij} + \beta_2\,t^2_{ij} + \upsilon_{0i} +
|
||||||
|
\upsilon_1\,t_{ij} + \upsilon_2\,t^2_{ij} + \varepsilon_{ij}
|
||||||
|
\]
|
||||||
|
with
|
||||||
|
\begin{align*}
|
||||||
|
\begin{pmatrix}
|
||||||
|
\upsilon_{0i}\\
|
||||||
|
\upsilon_{1i}\\
|
||||||
|
\upsilon_{2i}
|
||||||
|
\end{pmatrix} &\overset{iid}{\sim}
|
||||||
|
N \left(\begin{pmatrix}
|
||||||
|
0\\ 0\\ 0
|
||||||
|
\end{pmatrix}, \,
|
||||||
|
\begin{pmatrix}
|
||||||
|
\sigma^2_{\upsilon_0} & \sigma_{\upsilon_0 \upsilon_1} & \sigma_{\upsilon_0 \upsilon_2}\\
|
||||||
|
\sigma_{\upsilon_0 \upsilon_1} & \sigma^2_{\upsilon_1} & \sigma_{\upsilon_1 \upsilon_2}\\
|
||||||
|
\sigma_{\upsilon_0 \upsilon_2} & \sigma_{\upsilon_1 \upsilon_2} & \sigma^2_{\upsilon_2}\\
|
||||||
|
\end{pmatrix} \right) \\
|
||||||
|
\boldsymbol{\varepsilon}_i &\overset{iid}{\sim} N(\mathbf{0}, \, \sigma^2 \mathbf{I}_{n_i})
|
||||||
|
\end{align*}
|
||||||
|
\end{itemize}
|
||||||
|
\vspace{-.5cm}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}[fragile]{Model with quadratic trend}
|
||||||
|
\begin{lstlisting}
|
||||||
|
library("lme4")
|
||||||
|
|
||||||
|
# random intercept model
|
||||||
|
lme1 <- lmer(hamd ~ week + (1 | id), data = dat, REML = FALSE)
|
||||||
|
|
||||||
|
# random slope model
|
||||||
|
lme2 <- lmer(hamd ~ week + (week | id), data = dat, REML = FALSE)
|
||||||
|
|
||||||
|
# model with quadratic time trend
|
||||||
|
lme3 <- lmer(hamd ~ week + I(week^2) + (week + I(week^2) | id),
|
||||||
|
data = dat, REML = FALSE)
|
||||||
|
\end{lstlisting}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}{Model predictions}
|
||||||
|
\begin{columns}
|
||||||
|
\begin{column}{6cm}
|
||||||
|
\includegraphics[width=6cm]{../figures/hdrs-quad}
|
||||||
|
\end{column}
|
||||||
|
%
|
||||||
|
\begin{column}{5cm}
|
||||||
|
\begin{itemize}
|
||||||
|
\item Averaged over persons an approximately linear trend is obtained,
|
||||||
|
$\hat{\beta}_1 = -2.63$, $\hat{\beta}_2 = 0.05$
|
||||||
|
\item Some of the predicted individual trends are strongly nonlinear
|
||||||
|
\end{itemize}
|
||||||
|
\end{column}
|
||||||
|
\end{columns}
|
||||||
|
\begin{itemize}
|
||||||
|
\item Test against a model without individual quadratic trends\\[2ex]
|
||||||
|
|
||||||
|
H$_0$: $\sigma^2_{\upsilon_2} = \sigma_{\upsilon_0 \upsilon_2} =
|
||||||
|
\sigma_{\upsilon_1 \upsilon_2} = 0$ \qquad
|
||||||
|
$G^2(3) = 10.98$, $p = .012$
|
||||||
|
\end{itemize}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}[fragile]{Model predictions}
|
||||||
|
\begin{columns}
|
||||||
|
\begin{column}{8cm}
|
||||||
|
\includegraphics[scale=.5]{../figures/hdrs-ind_pred-quad}
|
||||||
|
\end{column}
|
||||||
|
\begin{column}{6cm}
|
||||||
|
\begin{lstlisting}
|
||||||
|
xyplot(
|
||||||
|
hamd + predict(lme3)
|
||||||
|
~ week | id,
|
||||||
|
data = dat,
|
||||||
|
type = c("p", "l", "g"),
|
||||||
|
pch = 16,
|
||||||
|
distribute.type = TRUE,
|
||||||
|
ylab = "HDRS score",
|
||||||
|
xlab = "Time (Week)")
|
||||||
|
\end{lstlisting}
|
||||||
|
\end{column}
|
||||||
|
\end{columns}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
% \begin{frame}[fragile]{Implied marginal covariance matrix}
|
||||||
|
% Predicted
|
||||||
|
% \begin{align*}
|
||||||
|
% \mathbf{Z}_i \boldsymbol{\hat\Sigma}_\upsilon \mathbf{Z}'_i +
|
||||||
|
% \hat\sigma^2 \mathbf{I}_{n_i} &=
|
||||||
|
% \begin{pmatrix}
|
||||||
|
% 20.96 & 9.41 & 8.16 & 6.68 & 4.98 & 3.06 \\
|
||||||
|
% 9.41 & 23.86 & 15.57 & 16.08 & 14.88 & 11.97 \\
|
||||||
|
% 8.16 & 15.57 & 31.07 & 23.11 & 23.26 & 20.98 \\
|
||||||
|
% 6.68 & 16.08 & 23.11 & 38.31 & 30.12 & 30.09 \\
|
||||||
|
% 4.98 & 14.88 & 23.26 & 30.12 & 45.98 & 39.29 \\
|
||||||
|
% 3.06 & 11.97 & 20.98 & 30.09 & 39.29 & 59.11
|
||||||
|
% \end{pmatrix}\\
|
||||||
|
% %
|
||||||
|
% \intertext{Observed}
|
||||||
|
% %
|
||||||
|
% \widehat{Cov}(\mathbf{y}_i) &=
|
||||||
|
% \begin{pmatrix}
|
||||||
|
% 20.55 & 10.11 & 10.14 & 10.09 & 7.19 & 6.28 \\
|
||||||
|
% 10.11 & 22.07 & 12.28 & 12.55 & 10.26 & 7.72 \\
|
||||||
|
% 10.14 & 12.28 & 30.09 & 25.13 & 24.63 & 18.38 \\
|
||||||
|
% 10.09 & 12.55 & 25.13 & 41.15 & 37.34 & 23.99 \\
|
||||||
|
% 7.19 & 10.26 & 24.63 & 37.34 & 48.59 & 30.51 \\
|
||||||
|
% 6.28 & 7.72 & 18.38 & 23.99 & 30.51 & 52.12 \\
|
||||||
|
% \end{pmatrix}\\
|
||||||
|
% \end{align*}
|
||||||
|
% % \vspace{-1cm}
|
||||||
|
% % \begin{lstlisting}
|
||||||
|
% % getVarCov(lme3, type="marginal") # predicted cov. matrix
|
||||||
|
% %
|
||||||
|
% % var.d <- crossprod(getME(lme3,"Lambdat"))
|
||||||
|
% % Zt <- getME(lme3, "Zt")
|
||||||
|
% % vr <- sigma(lme3)^2
|
||||||
|
% %
|
||||||
|
% % var.b <- vr*(t(Zt) %*% var.d %*% Zt)
|
||||||
|
% % sI <- vr * Diagonal(nrow(dat2))
|
||||||
|
% % var.y <- var.b + sI
|
||||||
|
% %
|
||||||
|
% % \end{lstlisting}
|
||||||
|
% \end{frame}
|
||||||
|
|
||||||
|
\begin{frame}{Centering variables}
|
||||||
|
\begin{itemize}
|
||||||
|
\item If multiples of the time variables ($t$, $t^2$, $t^3$, etc.) are
|
||||||
|
entered into the regression equation, multicollinearity can become a
|
||||||
|
problem
|
||||||
|
\item For example, $t = 0, 1, 2, 3$ and $t^2 = 0, 1, 4, 9$ correlate almost
|
||||||
|
perfectly
|
||||||
|
\item By centering the variables, this problem can be diminished: $(t -
|
||||||
|
\bar{t}) = -1.5, -0.5, 0.5, 1.5$ and $(t - \bar{t})^2 = 2.25, 0.25, 0.25,
|
||||||
|
2.25$ are uncorrelated
|
||||||
|
\item By centering variables the interpretation of the intercept in a linear
|
||||||
|
model changes:
|
||||||
|
\begin{itemize}
|
||||||
|
\item Uncentered intercepts represent the difference to the first time
|
||||||
|
point ($t = 0$)
|
||||||
|
\item Centered intercepts represent the difference after half of the
|
||||||
|
time
|
||||||
|
\end{itemize}
|
||||||
|
\end{itemize}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}{}
|
||||||
|
\begin{block}{Exercise}
|
||||||
|
\begin{itemize}
|
||||||
|
\item Center time\pause
|
||||||
|
\item Compare the correlation between time and time$^2$ with and without
|
||||||
|
centering\pause
|
||||||
|
\item Refit the model with quadratic effects when time is centered\pause
|
||||||
|
\item Which parameters change?
|
||||||
|
\begin{itemize}
|
||||||
|
\item What changes for the fixed effects?
|
||||||
|
\item How do the variance components for the random effects change?
|
||||||
|
\end{itemize}\pause
|
||||||
|
\item \emph{Why} do the variance components for the random effects change?
|
||||||
|
\end{itemize}
|
||||||
|
\end{block}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}[fragile]{Analysis with centered time variable}
|
||||||
|
\begin{lstlisting}
|
||||||
|
dat$week_c <- dat$week - mean(dat$week)
|
||||||
|
cor(dat$week, dat$week^2) # 0.96
|
||||||
|
cor(dat$week_c, dat$week_c^2) # 0.01
|
||||||
|
|
||||||
|
# random slope model
|
||||||
|
lme2c <- lmer(hamd ~ week_c + (week_c | id), data = dat, REML = FALSE)
|
||||||
|
|
||||||
|
# model with quadratic time trend
|
||||||
|
lme3c <- lmer(hamd ~ week_c + I(week_c^2) + (week_c + I(week_c^2)|id),
|
||||||
|
data = dat, REML = FALSE)
|
||||||
|
\end{lstlisting}
|
||||||
|
% \begin{itemize}
|
||||||
|
% \item When comparing the estimated parameters, it becomes obvious that not
|
||||||
|
% only the intercept changes but the estimates for the (co)variances do as
|
||||||
|
% well
|
||||||
|
% \item Why?\pause
|
||||||
|
% ~Be sure to make an informed choice when centering your
|
||||||
|
% variables!
|
||||||
|
% \end{itemize}
|
||||||
|
\nocite{Alday2025, Hedeker2006}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}[fragile]{Investigating random effects structure}
|
||||||
|
\begin{itemize}
|
||||||
|
\item In order to get a better understanding of the necessary random effects
|
||||||
|
it might be a good idea to take a closer look at them
|
||||||
|
\item Two plots often used are the so-called caterpillar and shrinkage plots
|
||||||
|
\item Play around with different models and compare how, e.\,g., the
|
||||||
|
caterpillar plots change with and without covariances in the model!
|
||||||
|
\end{itemize}
|
||||||
|
\vfill
|
||||||
|
\begin{lstlisting}
|
||||||
|
library("lattice")
|
||||||
|
dotplot(ranef(lme3), scales = list( x = list(relation = "free")))$id
|
||||||
|
\end{lstlisting}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
|
||||||
|
\begin{frame}[fragile]%{Investigating random effects structure}
|
||||||
|
{Caterpillar plot}
|
||||||
|
\begin{columns}
|
||||||
|
\begin{column}{1.13\textwidth}
|
||||||
|
\includegraphics[scale=.31]{../figures/hdrs-caterpillar}
|
||||||
|
\end{column}
|
||||||
|
\end{columns}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}[fragile]%{Investigating random effects structure}
|
||||||
|
{Shrinkage plots}
|
||||||
|
\begin{columns}
|
||||||
|
\begin{column}{.35\textwidth}
|
||||||
|
\includegraphics[scale=.35]{../figures/hdrs_shrinkage_int-week}
|
||||||
|
\end{column}
|
||||||
|
\begin{column}{.35\textwidth}
|
||||||
|
\includegraphics[scale=.35]{../figures/hdrs_shrinkage_int-weeksq}
|
||||||
|
\end{column}
|
||||||
|
\begin{column}{.35\textwidth}
|
||||||
|
\includegraphics[scale=.35]{../figures/hdrs_shrinkage_week-weeksq}
|
||||||
|
\end{column}
|
||||||
|
\end{columns}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
% \begin{frame}{Higher-order polynomials}
|
||||||
|
% \begin{itemize}
|
||||||
|
% \item Nonlinear time trends can be modelled in a flexibel and parsimonious
|
||||||
|
% way by using higher-order polynomials
|
||||||
|
% \item For example, saddle or reversal points in a time trend can be
|
||||||
|
% described
|
||||||
|
% \item Polynomials have the advantage that the regression model stays linear
|
||||||
|
% in its parameters
|
||||||
|
% \item They have the disadvantage that extrapolated values can quickly be
|
||||||
|
% outside of a range that can still be interpreted in a meaningful way
|
||||||
|
% \end{itemize}
|
||||||
|
% \end{frame}
|
||||||
|
%
|
||||||
|
% \begin{frame}{Cubic time trends}
|
||||||
|
% \begin{center}
|
||||||
|
% \includegraphics[width=9cm]{../figures/cubic}
|
||||||
|
% \end{center}
|
||||||
|
% \end{frame}
|
||||||
|
%
|
||||||
|
% \begin{frame}{Polynomial regression: Extrapolation}
|
||||||
|
% \begin{center}
|
||||||
|
% \includegraphics[width=9cm]{../figures/cubic-gone-bad}
|
||||||
|
% \end{center}
|
||||||
|
% \end{frame}
|
||||||
|
|
||||||
|
\begin{frame}{What we learned today\dots}
|
||||||
|
{\dots and how to go on}
|
||||||
|
\pause
|
||||||
|
\begin{enumerate}[<+->]
|
||||||
|
\item We learned
|
||||||
|
\begin{itemize}
|
||||||
|
\item The basic concept of random effects and why to include them in a
|
||||||
|
model
|
||||||
|
\item How to model data collected over several time points
|
||||||
|
\item How to compute mixed-effects model with quadratic time trends in R
|
||||||
|
using \texttt{lmer()} from the lme4 package
|
||||||
|
\item How to interpret parameters in mixed-effects model with quadratic
|
||||||
|
effects
|
||||||
|
\end{itemize}
|
||||||
|
\item Next steps
|
||||||
|
\begin{itemize}
|
||||||
|
\item Do this exercise
|
||||||
|
\url{https://gitea.iwm-tuebingen.de/nwickelmaier/lead_longitudinal/src/branch/master/exercises/schizo.md}
|
||||||
|
\item It has a very similar structure than the depression dataset and
|
||||||
|
this will help you to generalize the concepts we learned today
|
||||||
|
\item You can send questions to me and even make an appointment with me
|
||||||
|
to go over your solution
|
||||||
|
\end{itemize}
|
||||||
|
\end{enumerate}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\appendix
|
||||||
|
%\begin{frame}[allowframebreaks]{References}
|
||||||
|
\begin{frame}{References}
|
||||||
|
\printbibliography
|
||||||
|
\vfill
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\end{document}
|
||||||
|
|
||||||
55
slides/lit.bib
Normal file
55
slides/lit.bib
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
@book{Alday2025,
|
||||||
|
title = {Embrace Uncertainty -- {M}ixed-effects models with {J}ulia},
|
||||||
|
author = {Phillip Alday and Reinhold Kliegl and Douglas Bates},
|
||||||
|
year = {2025},
|
||||||
|
url = {https://embraceuncertaintybook.com/}
|
||||||
|
}
|
||||||
|
|
||||||
|
@misc{Bates10lme4,
|
||||||
|
title = {{lme4}: Mixed-effects modeling with {R} (book draft)},
|
||||||
|
author = {Bates, D.},
|
||||||
|
year = {2010},
|
||||||
|
url = {https://lme4.r-forge.r-project.org/}
|
||||||
|
}
|
||||||
|
|
||||||
|
@article{Bates15,
|
||||||
|
title = {Fitting Linear Mixed-Effects Models Using lme4},
|
||||||
|
volume = {67},
|
||||||
|
url = {https://CRAN.R-project.org/package=lme4/vignettes/lmer.pdf},
|
||||||
|
doi = {10.18637/jss.v067.i01},
|
||||||
|
number = {1},
|
||||||
|
journal = {Journal of Statistical Software},
|
||||||
|
author = {Bates, D. and M\"achler, M. and Bolker, B. and Walker, S.},
|
||||||
|
year = {2015},
|
||||||
|
pages = {1--48}
|
||||||
|
}
|
||||||
|
|
||||||
|
@book{Hedeker2006,
|
||||||
|
author = {Hedeker, D. R. and Gibbons, R. D.},
|
||||||
|
title = {Longitudinal data analysis},
|
||||||
|
year = {2006},
|
||||||
|
address = {Hoboken},
|
||||||
|
publisher = {John Wiley}
|
||||||
|
}
|
||||||
|
|
||||||
|
@article{Moeller08,
|
||||||
|
title = {Short-term treatment with risperidone or haloperidol in first-episode schizophrenia: 8-week results of a randomized controlled trial within the German Research Network on Schizophrenia},
|
||||||
|
author = {M{\"o}ller, H.-J. and Riedel, M. and J{\"a}ger, M. and Wickelmaier, F. and Maier, W. and K{\"u}hn, K.-U. and Buchkremer, G. and Heuser, I. and Klosterk{\"o}tter, J. and Gastpar, M. and others},
|
||||||
|
journal = {International Journal of Neuropsychopharmacology},
|
||||||
|
volume = {11},
|
||||||
|
number = {7},
|
||||||
|
pages = {985--997},
|
||||||
|
year = {2008},
|
||||||
|
doi = {10.1017/S1461145708008791}
|
||||||
|
}
|
||||||
|
|
||||||
|
@article{ReisbyGram77,
|
||||||
|
author = {Reisby, N. and Gram, L. F. and Bech, P. and Nagy, A. and Petersen, G. O. and Ortmann, J. and Ibsen, I. and Dencker, S. J. and Jacobsen, O. and Krautwald, O. and Sondergaard, I. and Christiansen, J.},
|
||||||
|
title = {Imipramine: Clinical effects and pharmacokinetic variability},
|
||||||
|
journal = {Psychopharmacology},
|
||||||
|
year = {1977},
|
||||||
|
volume = {54},
|
||||||
|
pages = {263--272},
|
||||||
|
doi = {10.1007/BF00426574}
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user