-
Notifications
You must be signed in to change notification settings - Fork 18
/
17-linear_mixed_effects_models1.Rmd
294 lines (237 loc) · 8.45 KB
/
17-linear_mixed_effects_models1.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# Linear mixed effects models 1
## Learning goals
- Understanding sources of dependence in data.
- fixed effects vs. random effects.
- `lmer()` syntax in R.
- Understanding the `lmer()` summary.
- Simulating data from an `lmer()`.
## Load packages and set plotting theme
```{r, message=FALSE}
library("knitr") # for knitting RMarkdown
library("kableExtra") # for making nice tables
library("janitor") # for cleaning column names
library("broom.mixed") # for tidying up linear models
library("patchwork") # for making figure panels
library("lme4") # for linear mixed effects models
library("tidyverse") # for wrangling, plotting, etc.
```
```{r}
theme_set(theme_classic() + #set the theme
theme(text = element_text(size = 20))) #set the default text size
opts_chunk$set(comment = "",
fig.show = "hold")
```
## Dependence
Let's generate a data set in which two observations from the same participants are dependent, and then let's also shuffle this data set to see whether taking into account the dependence in the data matters.
```{r}
# make example reproducible
set.seed(1)
df.dependence = tibble(participant = 1:20,
condition1 = rnorm(20),
condition2 = condition1 + rnorm(20, mean = 0.2, sd = 0.1)) %>%
mutate(condition2shuffled = sample(condition2)) # shuffles the condition label
```
Let's visualize the original and shuffled data set:
```{r}
df.plot = df.dependence %>%
pivot_longer(cols = -participant,
names_to = "condition",
values_to = "value") %>%
mutate(condition = str_replace(condition, "condition", ""))
p1 = ggplot(data = df.plot %>%
filter(condition != "2shuffled"),
mapping = aes(x = condition, y = value)) +
geom_line(aes(group = participant), alpha = 0.3) +
geom_point() +
stat_summary(fun = "mean",
geom = "point",
shape = 21,
fill = "red",
size = 4) +
labs(title = "original",
tag = "a)")
p2 = ggplot(data = df.plot %>%
filter(condition != "2"),
mapping = aes(x = condition, y = value)) +
geom_line(aes(group = participant), alpha = 0.3) +
geom_point() +
stat_summary(fun = "mean",
geom = "point",
shape = 21,
fill = "red",
size = 4) +
labs(title = "shuffled",
tag = "b)")
p1 + p2
```
Let's save the two original and shuffled data set as two separate data sets.
```{r}
# separate the data sets
df.original = df.dependence %>%
pivot_longer(cols = -participant,
names_to = "condition",
values_to = "value") %>%
mutate(condition = str_replace(condition, "condition", "")) %>%
filter(condition != "2shuffled")
df.shuffled = df.dependence %>%
pivot_longer(cols = -participant,
names_to = "condition",
values_to = "value") %>%
mutate(condition = str_replace(condition, "condition", "")) %>%
filter(condition != "2")
```
Let's run a linear model, and independent samples t-test on the original data set.
```{r}
# linear model (assuming independent samples)
lm(formula = value ~ condition,
data = df.original) %>%
summary()
t.test(df.original$value[df.original$condition == "1"],
df.original$value[df.original$condition == "2"],
alternative = "two.sided",
paired = F)
```
The mean difference between the conditions is extremely small, and non-significant (if we ignore the dependence in the data).
Let's fit a linear mixed effects model with a random intercept for each participant:
```{r}
# fit a linear mixed effects model
lmer(formula = value ~ condition + (1 | participant),
data = df.original) %>%
summary()
```
To test for whether condition is a significant predictor, we need to use our model comparison approach:
```{r}
# fit models
fit.compact = lmer(formula = value ~ 1 + (1 | participant),
data = df.original)
fit.augmented = lmer(formula = value ~ condition + (1 | participant),
data = df.original)
# compare via Chisq-test
anova(fit.compact, fit.augmented)
```
This result is identical to running a paired samples t-test:
```{r}
t.test(df.original$value[df.original$condition == "1"],
df.original$value[df.original$condition == "2"],
alternative = "two.sided",
paired = T)
```
But, unlike in the paired samples t-test, the linear mixed effects model explicitly models the variation between participants, and it's a much more flexible approach for modeling dependence in data.
Let's fit a linear model and a linear mixed effects model to the original (non-shuffled) data.
```{r}
# model assuming independence
fit.independent = lm(formula = value ~ 1 + condition,
data = df.original)
# model assuming dependence
fit.dependent = lmer(formula = value ~ 1 + condition + (1 | participant),
data = df.original)
```
Let's visualize the linear model's predictions:
```{r}
# plot with predictions by fit.independent
fit.independent %>%
augment() %>%
bind_cols(df.original %>%
select(participant)) %>%
clean_names() %>%
ggplot(data = .,
mapping = aes(x = condition,
y = value,
group = participant)) +
geom_point(alpha = 0.5) +
geom_line(alpha = 0.5) +
geom_point(aes(y = fitted),
color = "red") +
geom_line(aes(y = fitted),
color = "red")
```
And this is what the residuals look like:
```{r}
# make example reproducible
set.seed(1)
fit.independent %>%
augment() %>%
bind_cols(df.original %>%
select(participant)) %>%
clean_names() %>%
mutate(index = as.numeric(condition),
index = index + runif(n(), min = -0.3, max = 0.3)) %>%
ggplot(data = .,
mapping = aes(x = index,
y = value,
group = participant,
color = condition)) +
geom_point() +
geom_smooth(method = "lm",
se = F,
formula = "y ~ 1",
aes(group = condition)) +
geom_segment(aes(xend = index,
yend = fitted),
alpha = 0.5) +
scale_color_brewer(palette = "Set1") +
scale_x_continuous(breaks = 1:2,
labels = 1:2) +
labs(x = "condition") +
theme(legend.position = "none")
```
It's clear from this residual plot, that fitting two separate lines (or points) is not much better than just fitting one line (or point).
Let's visualize the predictions of the linear mixed effects model:
```{r}
# plot with predictions by fit.independent
fit.dependent %>%
augment() %>%
clean_names() %>%
ggplot(data = .,
mapping = aes(x = condition,
y = value,
group = participant)) +
geom_point(alpha = 0.5) +
geom_line(alpha = 0.5) +
geom_point(aes(y = fitted),
color = "red") +
geom_line(aes(y = fitted),
color = "red")
```
Let's compare the residuals of the linear model with that of the linear mixed effects model:
```{r}
# linear model
p1 = fit.independent %>%
augment() %>%
clean_names() %>%
ggplot(data = .,
mapping = aes(x = fitted,
y = resid)) +
geom_point() +
coord_cartesian(ylim = c(-2.5, 2.5))
# linear mixed effects model
p2 = fit.dependent %>%
augment() %>%
clean_names() %>%
ggplot(data = .,
mapping = aes(x = fitted,
y = resid)) +
geom_point() +
coord_cartesian(ylim = c(-2.5, 2.5))
p1 + p2
```
The residuals of the linear mixed effects model are much smaller. Let's test whether taking the individual variation into account is worth it (statistically speaking).
```{r}
# fit models (without and with dependence)
fit.compact = lm(formula = value ~ 1 + condition,
data = df.original)
fit.augmented = lmer(formula = value ~ 1 + condition + (1 | participant),
data = df.original)
# compare models
# note: the lmer model has to be entered as the first argument
anova(fit.augmented, fit.compact)
```
Yes, the linear mixed effects model explains the data better than the linear model.
## Additional resources
### Readings
- [Linear mixed effects models tutorial by Bodo Winter](https://arxiv.org/pdf/1308.5499.pdf)
## Session info
Information about this R session including which version of R was used, and what packages were loaded.
```{r}
sessionInfo()
```