forked from psych252/psych252book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
08-mixed-effects2.Rmd
799 lines (637 loc) · 23.5 KB
/
08-mixed-effects2.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
# Linear mixed effects models 2
## Learning goals
- An `lmer()` worked example
- complete pooling vs. no pooling vs. partial pooling
- getting p-values
- checking model assumptions
- Simulating mixed effects models
- effect of outliers
- non-homogeneity of variance
- Simpson's paradox
## Load packages and set plotting theme
```{r, message=FALSE, warning=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("ggeffects") # for plotting marginal effects
library("emmeans") # for the joint_tests() function
library("lme4") # for linear mixed effects models
library("performance") # for assessing model performance
library("see") # for assessing model performance
library("tidyverse") # for wrangling, plotting, etc.
```
```{r, warning=FALSE}
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")
```
## A worked example
Let's illustrate the concept of pooling and shrinkage via the sleep data set that comes with the lmer package.
```{r}
# load sleepstudy data set
df.sleep = sleepstudy %>%
as_tibble() %>%
clean_names() %>%
mutate(subject = as.character(subject)) %>%
select(subject, days, reaction)
```
```{r}
# add two fake participants (with missing data)
df.sleep = df.sleep %>%
bind_rows(tibble(subject = "374",
days = 0:1,
reaction = c(286, 288)),
tibble(subject = "373",
days = 0,
reaction = 245))
```
Let's start by visualizing the data
```{r}
# visualize the data
ggplot(data = df.sleep,
mapping = aes(x = days, y = reaction)) +
geom_point() +
facet_wrap(~subject, ncol = 5) +
labs(x = "Days of sleep deprivation",
y = "Average reaction time (ms)") +
scale_x_continuous(breaks = 0:4 * 2) +
theme(strip.text = element_text(size = 12),
axis.text.y = element_text(size = 12))
```
The plot shows the effect of the number of days of sleep deprivation on the average reaction time (presumably in an experiment). Note that for participant 373 and 374 we only have one and two data points respectively.
### Complete pooling
Let's first fit a model the simply combines all the data points. This model ignores the dependence structure in the data (i.e. the fact that we have repeated observations from the same participants).
```{r}
fit.complete = lm(formula = reaction ~ days,
data = df.sleep)
fit.params = tidy(fit.complete)
summary(fit.complete)
```
And let's visualize the predictions of this model.
```{r}
# visualization (aggregate)
ggplot(data = df.sleep,
mapping = aes(x = days, y = reaction)) +
geom_abline(intercept = fit.params$estimate[1],
slope = fit.params$estimate[2],
color = "blue") +
geom_point() +
labs(x = "Days of sleep deprivation",
y = "Average reaction time (ms)") +
scale_x_continuous(breaks = 0:4 * 2) +
theme(strip.text = element_text(size = 12),
axis.text.y = element_text(size = 12))
```
And here is what the model's predictions look like separated by participant.
```{r}
# visualization (separate participants)
ggplot(data = df.sleep,
mapping = aes(x = days, y = reaction)) +
geom_abline(intercept = fit.params$estimate[1],
slope = fit.params$estimate[2],
color = "blue") +
geom_point() +
facet_wrap(~subject, ncol = 5) +
labs(x = "Days of sleep deprivation",
y = "Average reaction time (ms)") +
scale_x_continuous(breaks = 0:4 * 2) +
theme(strip.text = element_text(size = 12),
axis.text.y = element_text(size = 12))
```
The model predicts the same relationship between sleep deprivation and reaction time for each participant (not surprising since we didn't even tell the model that this data is based on different participants).
### No pooling
We could also fit separate regressions for each participant. Let's do that.
```{r}
# fit regressions and extract parameter estimates
df.no_pooling = df.sleep %>%
group_by(subject) %>%
nest(data = c(days, reaction)) %>%
mutate(fit = map(data, ~ lm(reaction ~ days, data = .)),
params = map(fit, tidy)) %>%
ungroup() %>%
unnest(c(params)) %>%
select(subject, term, estimate) %>%
complete(subject, term, fill = list(estimate = 0)) %>%
pivot_wider(names_from = term,
values_from = estimate) %>%
clean_names()
```
And let's visualize what the predictions of these separate regressions would look like:
```{r}
ggplot(data = df.sleep,
mapping = aes(x = days,
y = reaction)) +
geom_abline(data = df.no_pooling %>%
filter(subject != 373),
aes(intercept = intercept,
slope = days),
color = "blue") +
geom_point() +
facet_wrap(~subject, ncol = 5) +
labs(x = "Days of sleep deprivation",
y = "Average reaction time (ms)") +
scale_x_continuous(breaks = 0:4 * 2) +
theme(strip.text = element_text(size = 12),
axis.text.y = element_text(size = 12))
```
When we fit separate regression, no information is shared between participants.
### Partial pooling
By usign linear mixed effects models, we are partially pooling information. That is, the estimates for one participant are influenced by the rest of the participants.
We'll fit a number of mixed effects models that differ in their random effects structure.
#### Random intercept and random slope
This model allows for random differences in the intercepts and slopes between subjects (and also models the correlation between intercepts and slopes).
Let's fit the model
```{r}
fit.random_intercept_slope = lmer(formula = reaction ~ 1 + days + (1 + days | subject),
data = df.sleep)
```
and take a look at the model's predictions:
```{r, warning=FALSE, message=F}
fit.random_intercept_slope %>%
augment() %>%
clean_names() %>%
ggplot(data = .,
mapping = aes(x = days,
y = reaction)) +
geom_line(aes(y = fitted),
color = "blue") +
geom_point() +
facet_wrap(~subject, ncol = 5) +
labs(x = "Days of sleep deprivation",
y = "Average reaction time (ms)") +
scale_x_continuous(breaks = 0:4 * 2) +
theme(strip.text = element_text(size = 12),
axis.text.y = element_text(size = 12))
```
As we can see, the lines for each participant are different. We've allowed for the intercept as well as the relationship between sleep deprivation and reaction time to be different between participants.
#### Only random intercepts
Let's fit a model that only allows for the intercepts to vary between participants.
```{r}
fit.random_intercept = lmer(formula = reaction ~ 1 + days + (1 | subject),
data = df.sleep)
```
And let's visualize what these predictions look like:
```{r, warning=FALSE, message=F}
fit.random_intercept %>%
augment() %>%
clean_names() %>%
ggplot(data = .,
mapping = aes(x = days,
y = reaction)) +
geom_line(aes(y = fitted),
color = "blue") +
geom_point() +
facet_wrap(~subject, ncol = 5) +
labs(x = "Days of sleep deprivation",
y = "Average reaction time (ms)") +
scale_x_continuous(breaks = 0:4 * 2) +
theme(strip.text = element_text(size = 12),
axis.text.y = element_text(size = 12))
```
Now, all the lines are parallel but the intercept differs between participants.
#### Only random slopes
Finally, let's compare a model that only allows for the slopes to differ but not the intercepts.
```{r}
fit.random_slope = lmer(formula = reaction ~ 1 + days + (0 + days | subject),
data = df.sleep)
```
And let's visualize the model fit:
```{r, warning=FALSE, message=F}
fit.random_slope %>%
augment() %>%
clean_names() %>%
ggplot(data = .,
mapping = aes(x = days,
y = reaction)) +
geom_line(aes(y = fitted),
color = "blue") +
geom_point() +
facet_wrap(vars(subject), ncol = 5) +
labs(x = "Days of sleep deprivation",
y = "Average reaction time (ms)") +
scale_x_continuous(breaks = 0:4 * 2) +
theme(strip.text = element_text(size = 12),
axis.text.y = element_text(size = 12))
```
Here, all the lines have the same starting point (i.e. the same intercept) but the slopes are different.
### Compare results
Let's compare the results of the different methods -- complete pooling, no pooling, and partial pooling (with random intercepts and slopes).
```{r, warning=F, message=F}
# complete pooling
fit.complete_pooling = lm(formula = reaction ~ days,
data = df.sleep)
df.complete_pooling = fit.complete_pooling %>%
augment() %>%
bind_rows(fit.complete_pooling %>%
augment(newdata = tibble(subject = c("373", "374"),
days = rep(10, 2)))) %>%
clean_names() %>%
select(reaction, days, complete_pooling = fitted)
# no pooling
df.no_pooling = df.sleep %>%
group_by(subject) %>%
nest(data = c(days, reaction)) %>%
mutate(fit = map(data, ~ lm(reaction ~ days, data = .)),
augment = map(fit, augment)) %>%
unnest(c(augment)) %>%
ungroup() %>%
clean_names() %>%
select(subject, reaction, days, no_pooling = fitted)
# partial pooling
fit.lmer = lmer(formula = reaction ~ 1 + days + (1 + days | subject),
data = df.sleep)
df.partial_pooling = fit.lmer %>%
augment() %>%
bind_rows(fit.lmer %>%
augment(newdata = tibble(subject = c("373", "374"),
days = rep(10, 2)))) %>%
clean_names() %>%
select(subject, reaction, days, partial_pooling = fitted)
# combine results
df.pooling = df.partial_pooling %>%
left_join(df.complete_pooling,
by = c("reaction", "days")) %>%
left_join(df.no_pooling,
by = c("subject", "reaction", "days"))
```
Let's compare the predictions of the different models visually:
```{r, warning=FALSE, message=F}
ggplot(data = df.pooling,
mapping = aes(x = days,
y = reaction)) +
geom_smooth(method = "lm",
se = F,
color = "orange",
fullrange = T) +
geom_line(aes(y = complete_pooling),
color = "green") +
geom_line(aes(y = partial_pooling),
color = "blue") +
geom_point() +
facet_wrap(~subject, ncol = 5) +
labs(x = "Days of sleep deprivation",
y = "Average reaction time (ms)") +
scale_x_continuous(breaks = 0:4 * 2) +
theme(strip.text = element_text(size = 12),
axis.text.y = element_text(size = 12))
```
To better see the differences between the approaches, let's focus on the predictions for the participants with incomplete data:
```{r, warning=FALSE, message=F}
# subselection
ggplot(data = df.pooling %>%
filter(subject %in% c("373", "374")),
mapping = aes(x = days,
y = reaction)) +
geom_smooth(method = "lm",
se = F,
color = "orange",
fullrange = T) +
geom_line(aes(y = complete_pooling),
color = "green") +
geom_line(aes(y = partial_pooling),
color = "blue") +
geom_point() +
facet_wrap(vars(subject)) +
labs(x = "Days of sleep deprivation",
y = "Average reaction time (ms)") +
scale_x_continuous(breaks = 0:4 * 2) +
theme(strip.text = element_text(size = 12),
axis.text.y = element_text(size = 12))
```
#### Coefficients
One good way to get a sense for what the different models are doing is by taking a look at the coefficients:
```{r}
coef(fit.complete_pooling)
```
```{r}
coef(fit.random_intercept)
```
```{r}
coef(fit.random_slope)
```
```{r}
coef(fit.random_intercept_slope)
```
#### Shrinkage
In mixed effects models, the variance of parameter estimates across participants shrinks compared to a no pooling model (where we fit a different regression to each participant). Expressed differently, individual parameter estimates are borrowing strength from the overall data set in mixed effects models.
```{r}
# get estimates from partial pooling model
df.partial_pooling = fit.random_intercept_slope %>%
coef() %>%
.$subject %>%
rownames_to_column("subject") %>%
clean_names()
# combine estimates from no pooling with partial pooling model
df.plot = df.sleep %>%
group_by(subject) %>%
nest(data = c(days, reaction)) %>%
mutate(fit = map(data, ~ lm(reaction ~ days, data = .)),
tidy = map(fit, tidy)) %>%
unnest(c(tidy)) %>%
select(subject, term, estimate) %>%
pivot_wider(names_from = term,
values_from = estimate) %>%
clean_names() %>%
mutate(method = "no pooling") %>%
bind_rows(df.partial_pooling %>%
mutate(method = "partial pooling")) %>%
pivot_longer(cols = -c(subject, method),
names_to = "index",
values_to = "value") %>%
mutate(index = factor(index, levels = c("intercept", "days")))
# visualize the results
ggplot(data = df.plot,
mapping = aes(x = value,
group = method,
fill = method)) +
stat_density(position = "identity",
geom = "area",
color = "black",
alpha = 0.3) +
facet_grid(cols = vars(index),
scales = "free")
```
### Getting p-values
To get p-values for mixed effects models, I recommend using the `joint_tests()` function from the `emmeans` package.
```{r}
lmer(formula = reaction ~ 1 + days + (1 + days | subject),
data = df.sleep) %>%
joint_tests()
```
Our good ol' model comparison approach produces a Likelihood ratio test in this case:
```{r}
fit1 = lmer(formula = reaction ~ 1 + days + (1 + days | subject),
data = df.sleep)
fit2 = lmer(formula = reaction ~ 1 + (1 + days | subject),
data = df.sleep)
anova(fit1, fit2)
```
### Reporting results
#### Plotting marginal effects
```{r}
# library("ggeffects")
# using the plot() function
ggpredict(model = fit.random_intercept_slope,
terms = "days",
type = "fe") %>%
plot()
# using our own ggplot magic
df.plot = ggpredict(model = fit.random_intercept_slope,
terms = "days",
type = "fe")
ggplot(data = df.plot,
mapping = aes(x = x,
y = predicted,
ymin = conf.low,
ymax = conf.high)) +
geom_ribbon(fill = "lightblue") +
geom_line(size = 1)
```
#### Checking model performance
```{r, message=FALSE, warning=FALSE}
lmer(formula = reaction ~ 1 + days + (1 + days | subject),
data = df.sleep) %>%
check_model()
```
## Simulating a linear mixed effects model
To generate some data for a linear mixed effects model with random intercepts, we do pretty much what we are used to doing when we generated data for a linear model. However, this time, we have an additional parameter that captures the variance in the intercepts between participants. So, we draw a separate (offset from the global) intercept for each participant from this distribution.
```{r}
# make example reproducible
set.seed(1)
# parameters
sample_size = 100
b0 = 1
b1 = 2
sd_residual = 1
sd_participant = 0.5
# generate the data
df.mixed = tibble(participant = rep(1:sample_size, 2),
condition = rep(0:1, each = sample_size)) %>%
group_by(participant) %>%
mutate(intercepts = rnorm(n = 1, sd = sd_participant)) %>%
ungroup() %>%
mutate(value = b0 + b1 * condition + intercepts + rnorm(n(), sd = sd_residual)) %>%
arrange(participant, condition)
df.mixed
```
Let's fit a model to this data now and take a look at the summary output:
```{r}
# fit model
fit.mixed = lmer(formula = value ~ 1 + condition + (1 | participant),
data = df.mixed)
summary(fit.mixed)
```
Let's visualize the model's predictions:
```{r}
fit.mixed %>%
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 simulate some data from this fitted model:
```{r}
# simulated data
fit.mixed %>%
simulate() %>%
bind_cols(df.mixed) %>%
ggplot(data = .,
mapping = aes(x = condition,
y = sim_1,
group = participant)) +
geom_line(alpha = 0.5) +
geom_point(alpha = 0.5)
```
Even though we only fitted random intercepts in this model, when we simulate from the model, we get different slopes since, when simulating new data, the model takes our uncertainty in the residuals into account as well.
Let's see whether fitting random intercepts was worth it in this case:
```{r}
# using chisq test
fit.compact = lm(formula = value ~ 1 + condition,
data = df.mixed)
fit.augmented = lmer(formula = value ~ 1 + condition + (1 | participant),
data = df.mixed)
anova(fit.augmented, fit.compact)
```
Nope, it's not worth it in this case. That said, even though having random intercepts does not increase the likelihood of the data given the model significantly, we should still include random intercepts to capture the dependence in the data.
### The effect of outliers
Let's take 20 participants from our `df.mixed` data set, and make one of the participants be an outlier:
```{r}
# let's make one outlier
df.outlier = df.mixed %>%
mutate(participant = participant %>% as.character() %>% as.numeric()) %>%
filter(participant <= 20) %>%
mutate(value = ifelse(participant == 20, value + 30, value),
participant = as.factor(participant))
```
Let's fit the model and look at the summary:
```{r}
# fit model
fit.outlier = lmer(formula = value ~ 1 + condition + (1 | participant),
data = df.outlier)
summary(fit.outlier)
```
The variance of the participants' intercepts has increased dramatically!
Let's visualize the data together with the model's predictions:
```{r}
fit.outlier %>%
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")
```
The model is still able to capture the participants quite well. But note what its simulated data looks like now:
```{r}
# simulated data from lmer with outlier
fit.outlier %>%
simulate() %>%
bind_cols(df.outlier) %>%
ggplot(data = .,
mapping = aes(x = condition,
y = sim_1,
group = participant)) +
geom_line(alpha = 0.5) +
geom_point(alpha = 0.5)
```
The simulated data doesn't look like our original data. This is because one normal distribution is used to model the variance in the intercepts between participants.
### Different slopes
Let's generate data where the effect of condition is different for participants:
```{r}
# make example reproducible
set.seed(1)
tmp = rnorm(n = 20)
df.slopes = tibble(
condition = rep(1:2, each = 20),
participant = rep(1:20, 2),
value = ifelse(condition == 1, tmp,
mean(tmp) + rnorm(n = 20, sd = 0.3)) # regression to the mean
) %>%
mutate(condition = as.factor(condition),
participant = as.factor(participant))
```
Let's fit a model with random intercepts.
```{r}
fit.slopes = lmer(formula = value ~ 1 + condition + (1 | participant),
data = df.slopes)
summary(fit.slopes)
```
Note how the summary says "singular fit", and how the variance for random intercepts is 0. Here, fitting random intercepts did not help the model fit at all, so the lmer gave up ...
How about fitting random slopes?
```{r, eval=F}
# fit model
lmer(formula = value ~ 1 + condition + (1 + condition | participant),
data = df.slopes)
```
This won't work because the model has more parameters than there are data points. To fit random slopes, we need more than 2 observations per participants.
### Simpson's paradox
Taking dependence in the data into account is extremely important. The Simpson's paradox is an instructive example for what can go wrong when we ignore the dependence in the data.
Let's start by simulating some data to demonstrate the paradox.
```{r}
# make example reproducible
set.seed(2)
n_participants = 20
n_observations = 10
slope = -10
sd_error = 0.4
sd_participant = 5
intercept = rnorm(n_participants, sd = sd_participant) %>% sort()
df.simpson = tibble(x = runif(n_participants * n_observations, min = 0, max = 1)) %>%
arrange(x) %>%
mutate(intercept = rep(intercept, each = n_observations),
y = intercept + x * slope + rnorm(n(), sd = sd_error),
participant = factor(intercept, labels = 1:n_participants))
```
Let's visualize the overall relationship between `x` and `y` with a simple linear model.
```{r, message=FALSE}
# overall effect
ggplot(data = df.simpson,
mapping = aes(x = x,
y = y)) +
geom_point() +
geom_smooth(method = "lm",
color = "black")
```
As we see, overall, there is a positive relationship between `x` and `y`.
```{r}
lm(formula = y ~ x,
data = df.simpson) %>%
summary()
```
And this relationship is significant.
Let's take another look at the data use different colors for the different participants.
```{r, message=FALSE}
# effect by participant
ggplot(data = df.simpson,
mapping = aes(x = x,
y = y,
color = participant)) +
geom_point() +
geom_smooth(method = "lm",
color = "black") +
theme(legend.position = "none")
```
And let's fit a different regression for each participant:
```{r, message=FALSE}
# effect by participant
ggplot(data = df.simpson,
mapping = aes(x = x,
y = y,
color = participant,
group = participant)) +
geom_point() +
geom_smooth(method = "lm",
color = "black") +
theme(legend.position = "none")
```
What this plot shows, is that for almost all individual participants, the relationship between `x` and `y` is negative. The different participants where along the `x` spectrum they are.
Let's fit a linear mixed effects model with random intercepts:
```{r}
fit.lmer = lmer(formula = y ~ 1 + x + (1 | participant),
data = df.simpson)
fit.lmer %>%
summary()
```
As we can see, the fixed effect for `x` is now negative!
```{r}
fit.lmer %>%
augment() %>%
clean_names() %>%
ggplot(data = .,
aes(x = x,
y = y,
group = participant,
color = participant)) +
geom_point() +
geom_line(aes(y = fitted),
size = 1,
color = "black") +
theme(legend.position = "none")
```
Lesson learned: taking dependence into account is critical for drawing correct inferences!
## Additional resources
### Readings
- [Linear mixed effects models tutorial by Bodo Winter](https://arxiv.org/pdf/1308.5499.pdf)
- [Simpson's paradox](https://paulvanderlaken.com/2017/09/27/simpsons-paradox-two-hr-examples-with-r-code/)
- [Tutorial on pooling](https://www.tjmahr.com/plotting-partial-pooling-in-mixed-effects-models/)
## Session info
Information about this R session including which version of R was used, and what packages were loaded.
```{r}
sessionInfo()
```