-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathWeek9_LMM2.qmd
More file actions
1578 lines (1292 loc) · 65.4 KB
/
Week9_LMM2.qmd
File metadata and controls
1578 lines (1292 loc) · 65.4 KB
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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "PSY4210: Linear Mixed Models (LMMs) 2"
author:
- name: Pei Hwa Goh
- name: Joshua F. Wiley
email: joshua.wiley@monash.edu
- name: Michelle Byrne
date: "`r Sys.Date()`"
format:
html:
toc: true
number-sections: true
embed-resources: true
anchor-sections: true
smooth-scroll: true
---
These are the `R` packages we will use.
```{r setup}
options(digits = 4)
## new packages are lme4, lmerTest, and multilevelTools
library(data.table)
library(JWileymisc)
library(extraoperators)
library(lme4)
library(lmerTest)
library(multilevelTools)
library(visreg)
library(ggplot2)
library(ggpubr)
library(haven)
## Set your working directory to the folder that has the datafiles.
## "Merged" is a a merged long dataset of both baseline and daily diary
dm <- as.data.table(read_sav("[2021] PSY4210 merged.sav"))
dm[, sex := factor(
sex,
levels = c(1,2),
labels = c("male", "female"))]
## While we are at it, let's also tell R that relationship
## status is a factor too
dm[, relsta := factor(
relsta, levels = c(1,2,3),
labels = c("single", "in a committed exclusive relationship", "in a committed nonexclusive relationship"))]
dm[, c("Bstress", "Wstress") := meanDeviations(dStress), by = ID]
```
# Linear Mixed Models
## ML and REML
Two common estimators used with linear mixed mdoels are
**M**aximum **L**ikelihood (ML) and
**Re**stricted **M**aximum **L**ikelihood (REML). You can think of
this somewhat the same as the formula for calculating a population's
variance versus estimating the population variance from a
sample.
Also see: https://stats.stackexchange.com/questions/16008/what-does-unbiasedness-mean/16009#16009.
$$
\sigma^2_{pop} = \frac{\sum_{i=1}^{n} (X_i - \bar{X})^{2}}{n}
$$
$$
\sigma^2_{sample} = \frac{\sum_{i=1}^{n} (X_i - \bar{X})^{2}}{n - 1}
$$
Generally speaking, REML, is less biased so we prefer it as an
estimator and it is the usual default in LMMs. However, for some
comparisons between models to be valid and some statistical tests to
be valid, we need true maximum likelihood (ML) estimates so you will
sometimes see the default REML option "turned off" to get pure ML
estimates or a model be refit using ML instaed of REML. This is done
in `R` using `REML = FALSE` to get ML.
## Random Intercept Model
There are two main uses of intercept only models:
- To calculate the intraclass correlation coefficient (ICC)
- As a comparison to see how much better a more complex model
fits.
To calculate the ICC, we use this equation:
$$ICC = \frac{\sigma^{2}_{intercept}}{\sigma^{2}_{intercept} +
\sigma^{2}_{residual}}$$
Following is an example of an intercept only model, where there is
both a fixed effects intercept and a random intercept.
The outcome variable is `stress`. All predictors come after the
tilde, `~`. In this case, the only "predictors" are the fixed and
random intercept, represented by `1`. The random intercept is random
by `ID`. The function to fit linear mixed models is `lmer()` and
comes from the `lme4` package. It also requires a dataset be
specified, here `dm`. We can get a summary using `summary()`.
```{r}
ri.m <- lmer(dStress ~ 1 + (1 | ID),
data = dm,
REML = TRUE)
summary(ri.m)
iccMixed("dStress", id = "ID", data = dm)
```
There are four main "blocks" of output from the summary.
1. A repetition of the model options, formula we used, and dataset
used. This is for records so you know exactly what the model was.
In *this* model, it shows use that we fit a LMM using restricted
maximum likelihood (REML) and that the degrees of freedom were
approximated using Satterthwaite's method. The outcome variable is
stress (`dStress`) and there are only intercept predictors,
`1`. The REML criterion at convergence is kind of like the log
likelihood (LL), but unfortunately cannot be readily used to
compare across models as easily as the actual LL (e.g., in AIC or
BIC, which we'll talk about more later).
2. Scaled Pearson residuals. These are raw residuals divided by the
estimated standard deviation, so that they can be roughly
interpretted as z-scores. The minimum and maximum are useful for
identifying whether there are outliers present in the model
residuals.
In *this* model, we can see that the lowest residual is
`r min(residuals(ri.m, type = "pearson", scaled=TRUE))`
and the maximum residual is
`r max(residuals(ri.m, type = "pearson", scaled=TRUE))`
which are not too large. Large residuals imply that they are
extremely unlikely by chance alone and likely represent outliers.
3. Random effects. These show a summary of the random effects in the
model. Random effects are basically always also fixed effects, so
the random effects only shows the standard deviation and variance
of random effects, plus, if applicable, their correlations. The
means are shown in the fixed effects section. In the case of a
random intercept only model like this one, there are only two
random effects: (1) the random intercept and (2) the random
residual. We have both the standard deviation and variance of
both. We will use the variances to calculate ICCs.
In *this* model, the standard deviation of the random intercept,
tells us that the average or typical difference between an
individual's average stress, and the population average
stress is
`r as.data.frame(VarCorr(ri.m))[1, "sdcor"]`.
The standard deviation of the residuals
tells us that the average or typical difference between an
individual stress score and the predicted stress
score is
`r as.data.frame(VarCorr(ri.m))[2, "sdcor"]`.
The random effects section also tells us how many observations and
unique people/groups went into the analysis.
In *this* model we can see that we had `r as.integer(ngrps(ri.m))`
people providing `r nobs(ri.m)` unique observations.
4. Fixed effects. This section shows the fixed effects. It is a
table, where each row is for a different effect / predictor and
each column gives a different piece of information.
The "Estimate" is the actual parameter estimate (i.e., THE fixed
effect, the regression coefficient, etc.). The "Std. Error" is the
standard error of the estimate, which captures uncertainty in the
coefficient due to sampling variation. The "df" is the
Satterthwaite estimated degrees of freedom. As an estimate, it may
have decimals. The "t value" is the ratio of the coefficient to
its standard error, that is: $t = \frac{Estimate}{StdError}$.
The "Pr(>|t|)" is the p-value, the probability that by chance
alone one would obtain as or a larger absolute t-value. The
vertical bars indicate absolute values and the "Pr" stands for
probability value. Note that `R` uses
[scientific E notation](https://en.wikipedia.org/wiki/Scientific_notation).
The number following the "e" indicates how many places to the
right (if positive) or left (if negative) the decimal point should
be moved. For example, 0.001 could be written 1e-3. 0.00052 could
be written 5.2e-4. These often are used for p-values which may be
numbers very close to zero.
In *this* model, we can see that the fixed effect for the
intercept is `r fixef(ri.m)[["(Intercept)"]]` which is like the
the mean of the random intercept and tells us the average
level of stress, in this instance since there are no
other predictors in the model.
Profile likelihood confidence intervals can be obtained using the
`confint()` function. These confidence intervals capture the
uncertainty in parameter estimates for both the fixed and random
effects due to sampling variation. They do not capture individual
differences directly. Note that you only get confidence intervals for
random effects when using the profile method, not when
`method = "Wald"` although the Wald method is much faster.
Profile likelihood confidence intervals are the default and are
generally more accurate than Wald based confidence intervals, but also
can be slower to calculate. Here both ways are shown.
```{r}
## Profile Confidence Intervals
ri.ci <- confint(ri.m, method = "profile", oldNames = FALSE)
ri.ci
## Wald Confidence Intervals
confint(ri.m, method = "Wald", oldNames = FALSE)
```
### Diagnostics
Typical diagnostics and checks include checking for outliers,
assessing whether the distributional assumptions are met, checking for
homogeneity of variance and checking whether there is a linear
association between predictors and outcome. With only an intercept,
there is no need for checking whether a linear association is
appropriate.
As for linear regressions, we can use `modelDiagnostics()` to
calculate diagnostics and `plot()` to get a plot of the diagnostics.
The top left plot of the residuals helps check for outliers on the
residuals.
These plots show one relatively extreme value on the residuals. In
this case, using the scaled pearson residuals, which are roughly like
z scores, the size of the residual outliers are unlikely to be an
issue (-3.13 is not that large in z scores). The bottom left plot
shows the distribution of the random intercept. No outliers are
observed.
The density plots (and QQ deviates plot for residuals) indicate
mild non-normality, but it is not too extreme and close enough for
inference.
Finally, on the top right, we check the homogeneity of variance.
There is no clear trend in the residuals indicating that the
homogeneity of variance assumption is reasonably met.
The residuals show a characteristic banding when dealing with
"continuous" variable that have a handful of possible values.
This is not a problem per se, although if too extreme may indicate
that treating the data as continuous is not a great idea.
```{r}
## When I initially tried to run the model diagnostics,
## an error about haven_labelled class appeared, so let's
## get rid of the haven_labelled class type for stress
## before running the model and model diagnostics
dm[, dStress := as.numeric(dStress)]
## Now we re-run the model
ri.m <- lmer(dStress ~ 1 + (1 | ID),
data = dm,
REML = TRUE)
## And re-run the model diagnostics
plot(modelDiagnostics(ri.m), ncol = 2, nrow = 2, ask = FALSE)
```
With diagnostics reasonably met, we proceed with a write up.
### Sample Write Up
An intercept only linear mixed model was fit to
`r nobs(ri.m)` stress scores from
`r as.integer(ngrps(ri.m))` people. The intraclass correlation
coefficient was
`r as.data.frame(VarCorr(ri.m))[1, "vcov"] / sum(as.data.frame(VarCorr(ri.m))[, "vcov"])`
indicating that about 40% of the total variance in stress
was between people and the other 60% is within person due to
fluctuations across days. The fixed effect intercept revealed that the
average [95% CI] stress was
`r fixef(ri.m)[["(Intercept)"]]`
`r sprintf("[%0.2f, %0.2f]", ri.ci[3, 1], ri.ci[3, 2])`.
However, there were individual differences, with the standard
deviation for the random intercept being
`r as.data.frame(VarCorr(ri.m))[1, "sdcor"]`
indicating that there are individual differences in the mean
stress. Assuming the random intercepts follow a normal distribution,
we expect most people to fall within one standard deviation of the
mean, which in these data would be somewhere between:
`r fixef(ri.m)[["(Intercept)"]] + c(-1, 1) * as.data.frame(VarCorr(ri.m))[1, "sdcor"]`.
## Random Intercept and Fixed Effects Models
Following is an example of a LMM with fixed effects and a random
intercept (no random slopes). Although we did not explicitly add a
fixed effects intercept by adding `1` to the equation, it is there by
default. We still have a random intercept.
```{r}
fp.m <- lmer(dStress ~ dEnergy + (1 | ID),
data = dm,
REML = TRUE)
summary(fp.m)
```
There are four main "blocks" of output from the summary.
1. A repetition of the model options, formula we used, and dataset
used. This is for records so you know exactly what the model was.
In *this* model, it shows use that we fit a LMM using restricted
maximum likelihood (REML) and that the degrees of freedom were
approximated using Satterthwaite's method. The outcome variable is
stress (`stress`) and energy is a predictor.
The REML criterion at convergence is kind of like the log
likelihood (LL), but unfortunately cannot be readily used to
compare across models as easily as the actual LL (e.g., in AIC or
BIC).
2. Scaled Pearson residuals. These are raw residuals divided by the
estimated standard deviation, so that they can be roughly
interpretted as z-scores. The minimum and maximum are useful for
identifying whether there are outliers present in the model
residuals.
In *this* model, we can see that the lowest residual is
`r min(residuals(fp.m, type = "pearson", scaled=TRUE))`
and the maximum residual is
`r max(residuals(fp.m, type = "pearson", scaled=TRUE))`
which are acceptable. Absolute residuals of 10, for example, would be
large enough that they are extremely unlikely by chance alone and likely
represent outliers. We can see there are some more extreme negative
than positive residuals. That means that predictions are sometimes
too (extremely) high rather than too (extremely) low.
3. Random effects. These show a summary of the random effects in the
model. Random effects are basically always also fixed effects, so
the random effects only shows the standard deviation and variance
of random effects, plus, if applicable, their correlations. The
means are shown in the fixed effects section. In the case of a
model where the only random effect is the intercept, the
random effects show: (1) the random intercept and (2) the random
residual. We have both the standard deviation and variance of
both.
In *this* model, the standard deviation of the random intercept,
tells us that the average or typical difference between an
individual's estimated stress when energy is 0,
and the population average estimated stress when energy is
0 is
`r as.data.frame(VarCorr(fp.m))[1, "sdcor"]`.
The standard deviation of the residuals
tells us that the average or typical difference between an
individual stress score and the predicted stress
score is
`r as.data.frame(VarCorr(fp.m))[2, "sdcor"]`.
The random effects section also tells us how many observations and
unique people/groups went into the analysis.
In *this* model we can see that we had `r as.integer(ngrps(fp.m))`
people providing `r nobs(fp.m)` unique observations.
4. Fixed effects. This section shows the fixed effects. It is a
table, where each row is for a different effect / predictor and
each column gives a different piece of information.
The "Estimate" is the actual parameter estimate (i.e., THE fixed
effect, the regression coefficient, etc.). The "Std. Error" is the
standard error of the estimate, which captures uncertainty in the
coefficient due to sampling variation. The "df" is the
Satterthwaite estimated degrees of freedom. As an estimate, it may
have decimals. The "t value" is the ratio of the coefficient to
its standard error, that is: $t = \frac{Estimate}{StdError}$.
The "Pr(>|t|)" is the p-value, the probability that by chance
alone one would obtain as or a larger absolute t-value. The
vertical bars indicate absolute values and the "Pr" stands for
probability value. Note that `R` uses
[scientific E notation](https://en.wikipedia.org/wiki/Scientific_notation).
The number following the "e" indicates how many places to the
right (if positive) or left (if negative) the decimal point should
be moved. For example, 0.001 could be written 1e-3. 0.00052 could
be written 5.2e-4. These often are used for p-values which may be
numbers very close to zero.
In *this* model, we can see that the fixed effect for the
intercept is `r fixef(fp.m)[["(Intercept)"]]` which is like
the mean of the random intercept and tells us the average
estimated stress score when energy = 0.
The fixed effect (regression coefficient) for energy is
`r fixef(fp.m)[["dEnergy"]]` which tells us how much on average
(fixed effect) lower stress is expected to be when energy
is one unit higher.
Profile likelihood confidence intervals can be obtained using the
`confint()` function. These confidence intervals capture the
uncertainty in parameter estimates for both the fixed and random
effects due to sampling variation. They do not capture individual
differences directly. Note that you only get confidence intervals for
random effects when using the profile method, not when
`method = "Wald"` although the Wald method is much faster.
```{r}
fp.ci <- confint(fp.m, method = "profile", oldNames = FALSE)
fp.ci
```
### Diagnostics and Checks
Typical diagnostics and checks include checking for outliers,
assessing whether the distributional assumptions are met, checking for
homogeneity of variance and checking whether there is a linear
association between predictors and outcome.
Results look about the same as for the intercept only model.
```{r}
plot(modelDiagnostics(fp.m),
ncol = 2, nrow = 2, ask = FALSE)
```
### Sample Write Up
To examine the association of stress and energy, a linear
mixed model was fit. The final model included `r nobs(fp.m)` stress
scores from `r as.integer(ngrps(fp.m))` people.
The fixed effect intercept revealed that the
average [95% CI] stress when energy is 0 was
`r fixef(fp.m)[["(Intercept)"]]`
`r sprintf("[%0.2f, %0.2f]", fp.ci[3, 1], fp.ci[3, 2])`.
However, there were individual differences, with the standard
deviation for the random intercept being
`r as.data.frame(VarCorr(fp.m))[1, "sdcor"]`
indicating that there are individual differences in the mean
stress. Assuming the random intercepts follow a normal distribution,
we expect most people to fall within one standard deviation of the
mean, which in these data would be somewhere between:
`r fixef(fp.m)[["(Intercept)"]] + c(-1, 1) * as.data.frame(VarCorr(fp.m))[1, "sdcor"]`.
Using Satterthwaite's approximation for degrees of freedom revealed
that energy was statistically significantly associated with stress
(p = .005). On average across people, a one unit higher energy score
was associated with 0.16 lower stress scores.
## Between and Within Effects
When we have a time-varying predictor variable, we can separate it
into a between and within portion and both of these new variables can
be included in a LMM as predictors. After creating a between and
within version of energy, `Benergy` and `Wenergy` we just enter both
as fixed effects predictors.
```{r}
dm[, c("Benergy", "Wenergy") := meanDeviations(dEnergy), by = ID]
fp.m2 <- lmer(dStress ~ Benergy + Wenergy + (1 | ID),
data = dm)
summary(fp.m2)
```
There are four main "blocks" of output from the summary.
1. A repetition of the model options, formula we used, and dataset
used. This is for records so you know exactly what the model was.
In *this* model, it shows use that we fit a LMM using restricted
maximum likelihood (REML) and that the degrees of freedom were
approximated using Satterthwaite's method. The outcome variable is
stress (`dStress`) and `Benergy` and `Wenergy` are fixed effects
predictors.
The REML criterion at convergence is kind of like the log
likelihood (LL), but unfortunately cannot be readily used to
compare across models as easily as the actual LL (e.g., in AIC or
BIC).
2. Scaled Pearson residuals. These are raw residuals divided by the
estimated standard deviation, so that they can be roughly
interpretted as z-scores. The minimum and maximum are useful for
identifying whether there are outliers present in the model
residuals.
In *this* model, we can see that the lowest residual is
`r min(residuals(fp.m2, type = "pearson", scaled=TRUE))`
and the maximum residual is
`r max(residuals(fp.m2, type = "pearson", scaled=TRUE))`
which while a bit large,
are not so extreme if interpretted as z-scores as to be
concerning. Absolute residuals of 10, for example, would be large enough
that they are extremely unlikely by chance alone and likely
represent outliers. We can see there are slightly higher negative
than positive residuals. That means that predictions are sometimes
too (extremely) high rather than too (extremely) low relative to
actual values.
3. Random effects. These show a summary of the random effects in the
model. Random effects are basically always also fixed effects, so
the random effects only shows the standard deviation and variance
of random effects, plus, if applicable, their correlations. The
means are showon in the fixed effects section. In the case of a
model where the only random effect is the intercept, the
random effects show: (1) the random intercept and (2) the random
residual. We have both the standard deviation and variance of
both.
In *this* model, the standard deviation of the random intercept,
tells us that the average or typical difference between an
individual's estimated stress when `Benergy` and `Wenergy` are 0,
and the population average estimated stress when the predictors are
0 is
`r as.data.frame(VarCorr(fp.m2))[1, "sdcor"]`.
The standard deviation of the residuals
tells us that the average or typical difference between an
individual stress score and the predicted stress
score is
`r as.data.frame(VarCorr(fp.m2))[2, "sdcor"]`.
The random effects section also tells us how many observations and
unique people/groups went into the analysis.
In *this* model we can see that we had `r as.integer(ngrps(fp.m2))`
people providing `r nobs(fp.m2)` unique observations.
4. Fixed effects. This section shows the fixed effects. It is a
table, where each row is for a different effect / predictor and
each column gives a different piece of information.
The "Estimate" is the actual parameter estimate (i.e., THE fixed
effect, the regression coefficient, etc.). The "Std. Error" is the
standard error of the estimate, which captures uncertainty in the
coefficient due to sampling variation. The "df" is the
Satterthwaite estimated degrees of freedom. As an estimate, it may
have decimals. The "t value" is the ratio of the coefficient to
its standard error, that is: $t = \frac{Estimate}{StdError}$.
The "Pr(>|t|)" is the p-value, the probability that by chance
alone one would obtain as or a larger absolute t-value. The
vertical bars indicate absolute values and the "Pr" stands for
probability value. Note that `R` uses
[scientific E notation](https://en.wikipedia.org/wiki/Scientific_notation).
The number following the "e" indicates how many places to the
right (if positive) or left (if negative) the decimal point should
be moved. For example, 0.001 could be written 1e-3. 0.00052 could
be written 5.2e-4. These often are used for p-values which may be
numbers very close to zero.
In *this* model, we can see that the fixed effect for the
intercept is `r fixef(fp.m2)[["(Intercept)"]]` which is like
the mean of the random intercept and tells us the average
estimated stress score when both the between and within energy
= 0. The fixed effect (regression coefficient) for `Benergy` is
`r fixef(fp.m2)[["Benergy"]]` which tells us how different on average
(fixed effect) stress is expected to be when average energy
is one unit higher. That is, for people who, in general (on
average) have higher energy, how much lower stress do they have on
average across days?
The fixed effect (regression coefficient) for `Wenergy` is
`r fixef(fp.m2)[["Wenergy"]]` which tells us how much on average
(fixed effect) stress is expected to change when energy
is one unit higher than an individuals' own average.
That is, on days when someone has one more unit higher energy than
usual (than their own mean), how much lower stress do they have on
that same day?
The approximate p-values indicate that the average energy scores are
statistically significant, indicating that people who have more
energy in general have significantly lower stress in general.
The daily energy scores are NOT significant, but if they were, would suggest
that within people, higher energy days would also be associated with
lower stress days.
Profile likelihood confidence intervals can be obtained using the
`confint()` function. These confidence intervals capture the
uncertainty in parameter estimates for both the fixed and random
effects due to sampling variation. They do not capture indivdiual
differences directly. Note that you only get confidence intervals for
random effects when using the profile method, not when
`method = "Wald"` although the Wald method is much faster.
```{r}
fp.ci2 <- confint(fp.m2, method = "profile", oldNames = FALSE)
fp.ci2
```
### Diagnostics and Checks
Typical diagnostics and checks include checking for outliers,
assessing whether the distributional assumptions are met, checking for
homogeneity of variance and checking whether there is a linear
association between predictors and outcome.
Results look about the same as for the intercept only model.
Homogeneity of variance still appears approximately met.
```{r}
plot(modelDiagnostics(fp.m2),
ncol = 2, nrow = 2, ask = FALSE)
```
### Sample Write Up
To examine the association of stress and energy, a linear
mixed model was fit. To understand the association of energy and
stress at both the between person and within person level, daily
energy ratings were separated into average energy across the five days
of the study (between energy) and daily deviations of energy from
individuals' own mean energy level (within energy).
The final model included `r nobs(fp.m2)` stress
scores from `r as.integer(ngrps(fp.m2))` people.
The fixed effect intercept revealed that the
average [95% CI] stress when between and within energy are 0 was
`r fixef(fp.m2)[["(Intercept)"]]`
`r sprintf("[%0.2f, %0.2f]", fp.ci2[3, 1], fp.ci2[3, 2])`.
However, there were individual differences, with the standard
deviation for the random intercept being
`r as.data.frame(VarCorr(fp.m2))[1, "sdcor"]`
indicating that there are individual differences in the mean
stress. Assuming the random intercepts follow a normal distribution,
we expect most people to fall within one standard deviation of the
mean, which in these data would be somewhere between:
`r fixef(fp.m2)[["(Intercept)"]] + c(-1, 1) * as.data.frame(VarCorr(fp.m2))[1, "sdcor"]`.
Using Satterthwaite's approximation for degrees of freedom revealed
that only between person energy was statistically significantly
associated with stress. On average across people, at the between person level,
a one unit higher average energy score was associated with 0.30
lower stress scores. Although not significant, on average across people,
at the within person level, a one unit higher than usual daily energy score was
associated with 0.12 lower stress scores that same day. The magnitude
suggests that a one unit higher average energy level has about
twice the magnitude of association with average stress as does
a one unit higher daily energy level.
## Statistical Inference
An extra note about how we got the df's above.
There is ambiguity in terms of how best to calculate degrees of
freedom (df) for LMMs. By default `R` does not calculate the df and so
does not provide p-values for the regression coefficients (fixed
effects) from LMMs.
One easy, albeit imperfect, solution is to use the `lmerTest`
package. `lmerTest` use Satterthwaite's method to calculate
approximate degrees of freedom and use these for the t-tests and
p-values for each regression coefficient. To use `lmerTest` simply
make sure that **both** `lme4` and `lmerTest` packages are installed
and that you load the `lmerTest` package after `lme4`, by using:
`library(lmerTest)`. This is shown in the examples above.
Once that is done, all regular calls to `lmer()` function used to fit
LMMs will automatically have df estimated and p-values.
Although this is a relatively common approach, it is not without
limitations or debate. Other approaches include:
- assuming the sample size is large enough that the degrees of freedom
are large enough that we can assume the parameters follow a normal
distribution instead of a t-distribution.
- Relying on things like profile-based confidence intervals
- Bootstrapping, which is time intensive but probably the most robust
of these options.
Here is a short example bootstrapping. Here we bootstrap the 95%
confidence intervals, and if the interval does not include 0, we can
conclude that $p < .05$, it is statistically significant.
Although bootstrapping is robust, we often do not use it as it can be
quite time intensive for more complex models, especially those with
more random effects.
```{r}
confint(fp.m, method = "boot")
```
# Random Slopes
## Theory / Conceptual
Beyond random intercepts, we can allow
allow regression slopes to differ by person. In LMMs, we always
include a random intercept. That means if we also include a random
slope, we will have two random effects. With two (or more) random
effects, because they are assumed to be a random variable with a
normal distribution, we can also look at how the random effects
correlate with each other. That is, just as how you could look at how
two random variables, say age and stress, correlate with each other,
you can look at how any two random effects correlate with each other
(e.g., how a random intercept and random slope correlate).
When working with random slopes, we also need to slightly modify our
understanding of the assumptions and notation for LMMs.
Let $\mathbf{G}$ be a square, $q$ x $q$ covariance matrix, where $q$
is the number of random effects in the model. In our simplest of
examples, there is only one random effect, the random intercept, so $q
= 1$ and the random effect covariance matrix is:
$$
\mathbf{G} =
\begin{bmatrix}
\sigma^{2}_{int} \\
\end{bmatrix}
$$
If we had a random intercept only, then we assume:
$$ u_{0j} \sim \mathcal{N}(0, \mathbf{G}) $$
This is the same assumption we covered in the introduction to LMMs,
however the subtle change in notation sets us up for more complexity.
By using $\mathbf{G}$ we are replacing a single standard
deviation/variance with a covariance matrix. The benefit of the matrix
is that a covariance matrix can be small, like a 1 x 1 matrix or
bigger like a 2 x 2 or $q$ x $q$ matrix for any number of random
effects.
So what is the implication of this change? We do not *just* assume
that each random effect follows a normal distribution. In fact, for
LMMs, the assumption is that the random effects follow a
**multivariate** normal distribution^[for more info, see here:
https://en.wikipedia.org/wiki/Multivariate_normal_distribution].
The multivariate normal distribution (MVN) is a distribution for
multiple variables. If $q$ variables follow a MVN, then each $q_i$
variable will itself follow a univariate normal distribution.
However, just because each $q_i$ variable individually follows a
univariate normal distribution does not mean that the $q$ variables
together follow a MVN. In other words, MVN implies univariate normal,
but univariate normal does not imply MVN.
Like the univariate normal distribution, the MVN has two basic
parameters, the mean and variance. However, unlike a univariate normal
distribution, the MVN the means will be a vector of means, one for
each variable, and the variances, will be a $q$ x $q$ covariance
matrix.
Back to LMMs, what this means is that if we have a random intercept
and a random slope, $q = 2$ and we'd have:
$$
\mathbf{G} =
\begin{bmatrix}
\sigma^{2}_{int} & \sigma_{int,slope} \\
\sigma_{int,slope} & \sigma^{2}_{slope} \\
\end{bmatrix}
$$
The variances are on the diagonal and the covariance (the
unstandardized correlation between the intercept and the slope) is on
the off diagonal.
The usual practice in LMMs is to freely estimate both the variances
and covariances (correlations) for any random effects. We can,
however, also assume that the random effects are uncorrelated, that
is, assume that the random effects follow a multivariate normal
distribution like this:
$$
\mathbf{G} =
\begin{bmatrix}
\sigma^{2}_{int} & 0 \\
0 & \sigma^{2}_{slope} \\
\end{bmatrix}
$$
Although random slopes are different in some respects from a random
intercept, in most ways they are comparable. In both cases we assume a
normal distribution, with a mean (the "fixed effect" part, the average
slope across people) and a standard deviation (how much variability
there is in the slope across people). We estimate the mean and
standard deviation, but we *can* get BLUPs for the individual slopes
as well, which are predictions about what the slope is for any
specific individual. While a random intercept only impacts the level
of lines, a random slope will impact both the level and the slope of
the lines, shown graphically in the following figure.
```{r, fig.width = 7, fig.height = 10, fig.cap = "Figure showing a random intercept or random intercept and slope"}
ggarrange(
ggplot(expand.grid(x = c(0, 10), y = c(0, 10)), aes(x, y)) +
geom_point(colour = "white") +
geom_abline(intercept = 1, slope = 1, colour = "black", linewidth = 1) +
geom_abline(intercept = 2, slope = 1, colour = "yellow", linewidth = 1) +
geom_abline(intercept = 3, slope = 1, colour = "blue", linewidth = 1) +
geom_abline(intercept = 4, slope = 1, colour = "purple", linewidth = 1) +
theme_pubr() +
coord_cartesian(xlim = c(0, 9), ylim = c(0, 10), expand=FALSE) +
ggtitle("Random Intercepts"),
ggplot(expand.grid(x = c(0, 10), y = c(0, 10)), aes(x, y)) +
geom_point(colour = "white") +
geom_abline(intercept = 1, slope = 1, colour = "black", linewidth = 1) +
geom_abline(intercept = 2, slope = .5, colour = "yellow", linewidth = 1) +
geom_abline(intercept = 3, slope = 1.5, colour = "blue", linewidth = 1) +
geom_abline(intercept = 4, slope = .5, colour = "purple", linewidth = 1) +
theme_pubr() +
coord_cartesian(xlim = c(0, 9), ylim = c(0, 10), expand = FALSE) +
ggtitle("Random Slopes"),
ncol = 1, nrow = 2)
```
To evaluate the distributional assumptions of LMMs with multiple
random effects, the ideal test is to evaluate whether the random
effects come from a MVN. Visualizing MVN distributions is not easy,
especially with more than 2 dimensions, so instead we use another
approach.
The Mahalanobis distance measures the distance between a point and a
space defined by a vector of means and a covariance matrix. The
"point" can be unidimensional or multidimensional and if multidimensional
is not limited to only two dimensions. That is, the Mahalanobis
distance can compute the distance between a multidimensional point and
a multidimensional distribution. This is very convenient as it scales
arbitrarily to however complex (many random effects) or simple (one
random effect) we may have. If for each row of data, we calculate its
Mahalanobis distance, those squared distances will follow a $\chi^{2}$
(chi-squared) distribution with degrees of freedom equal to the number
of dimensions ($p$), that is a $\chi^2$ distribution with $df =
p$ if the raw data were MVN. This allows us to compare our observed
Mahalanobis distances to a chi-squared distribution and if they are a
close match, it indicates the data on which the Mahalanobis distances
were calculated were MVN. Thus, we can use Mahalanobis distances to
evaluate whether a set of variables have a MVN distribution.
They also can help identify multivariate outliers. We will see
examples of this when we look at diagnostics for LMMs with multiple
random effects.
## Random Slopes in `R`
Random slopes can only be estimated for predictors that vary within
units. Using the merged daily data collection dataset, we can
look at energy predicting stress. First, we will take a look at a model
with a random intercept and `dEnergy` as a fixed effect only.
```{r}
## get rid of the haven_labeled class type for stress
dm[, dStress := as.numeric(dStress)]
m0 <- lmer(dStress ~ dEnergy + (1 | ID),
data = dm)
summary(m0)
plot(modelDiagnostics(m0), ncol = 2, nrow = 2, ask = FALSE)
```
From the results, we can see that on average, higher energy is
associated with lower stress. Energy has not been separated into a
between or within component, so we cannot say whether its driven by
people who have higher energy on average having lower stress or days
with higher than usual energy having lower than usual stress, or both.
We can see that the residuals (top left graph panel) and random
intercept by ID (bottom left graph panel) are about normally
distributed with only modest if any outliers and we can see that the
homogeneity of variance assumption is approximately met at least in
the residuals vs fitted/predicted values (top right).
Next, we add dEnergy as a random slope by adding it inside the
parentheses before ID, `(dEnergy | ID)`.
```{r}
m1 <- lmer(dStress ~ dEnergy + (dEnergy | ID),
data = dm)
summary(m1)
```
The output is fairly familiar to the fixed effect slope and random
intercept only model but there is another random effect under the
groups ID for `dEnergy`. We get the variance and standard deviation of
`dEnergy` as well as the correlation between the random intercept and the
random slope. The correlation is
`r as.data.table(VarCorr(m1))[3, sdcor]` indicating that people who
have a higher level of stress when energy is 0 (the intercept) also tend
to have a more negative slope of the association between stress and energy. Note that although
the fixed effect slope estimate for energy is about the same between the
two models, the standard error is larger so the p-value is higher
(further away from 0) for the model with a fixed and random slope of
`dEnergy` than the model with only a fixed slope of `dEnergy`. This is a
fairly common pattern.
Now we can look at model diagnostic plots. We use 2 columns and 3 rows
as there are more plots now, but otherwise we use the usual
`modelDiagnostics()` function.
```{r, fig.width = 7, fig.height = 10}
plot(modelDiagnostics(m1), ncol = 2, nrow = 3, ask = FALSE)
```
In the figure, we see our familiar density and QQ deviates plot for
the residuals (top left). Univariate density plots (black lines) and
univariate normal distributions (blue dashed lines) for the random
intercept alone (`ID : (Intercept)`) and the random slope of energy
alone (`ID : dEnergy`). The naming convention is to include the
coefficient name, intercept or energy for the energy slope, prefixed by
what the variable name is for the random effect, here ID. Both of the
univariate random effect distribution plots can be interpreted as
usual and are shown on the middle row, left and right panels. What is
technically graphed are the BLUPs and at the bottom we can get a five
number summary to help see the minimum, 25th percentile, 50th
percentile (median), 75th percentile, and maximum, which can give us
some sense of the spread of the random intercept and slope. For
example, for the random slope of energy, the maximum BLUP is 0.07,
indicating that the highest predicted individual slope for anyone is
0.07. This tells us that not many people are expected to have *higher* stress if
they have higher energy. We can also see that 50% of people have BLUPs
between -0.20 and -0.07, giving us a sense of the spread of the random
slopes.
The new graph is on the bottom row, left side and it evaluates whether
the random effects by ID follow a MVN or not by using the Mahalanobis
distances. The observed density is the black line. The theoretical
density again is in dashed blue line. Here the theoretical density
**is not** a normal density, but a chi-squared density with $df = p =
2$. In this case, we can see that its not a terrible fit between the
observed and theoretical chi-squared density, suggesting the MVN
assumption is reasonably well met. We also can see, however, that
there are some relatively extreme distances, with the maximum at 13.72
being quite a bit higher than the next nearest. Although its not
flagged as an extreme value, partly because there are only about 88
people.
In practice, I may not actually make any changes here. For the sake of
illustration, however, and to show how to do it, we will use a more
stringent criteria for extreme values. Rather than defining an extreme
value as the top and bottom 0.5% of the theoretical distribution,
let's use the top and bottom 1% of the theoretical distribution.
Because I know this will yield some extreme values to remove, I am
saving the results of `modelDiagnostics()` in an object, `m1.diag`
which I can plot but I can also use to identify which rows / IDs in
the dataset are extreme and should / could be dropped.
The new figure shows a few extreme values in the random effect
BLUPs. Maybe a low value on the intercept, definitely a couple too low
slopes, and maybe one MVN extreme value, the 13.72 Mahalanobis
distance.
```{r, fig.width = 7, fig.height = 10}
m1.diag <- modelDiagnostics(m1, ev.perc = .01)
plot(m1.diag, ncol = 2, nrow = 3, ask=FALSE)
```
To consider removing these cases, we need to identify them.
The `m1.diag` object in `R` has several subparts, but we are
particularly interested here in the `extremeValues` subpart, which is
itself a little data table, shown in the following.
```{r}
m1.diag$extremeValues
```
In this extreme values data table, we can see a few columns. The most
important columns are:
- `ID` this is the ID variable from the dataset and will be helpful
later.
- `Index` this is the row number in the dataset used in the LMM and
can be used to identify specific rows of data that are extreme.
- `EffectType` this indicates what type of effect the extreme value
was identified on.
The first column, `dstress` just shows the outcome variable score for
reference, which may help but is not necessarily that useful always.
The name of the first column will depend on the name of the outcome
variable.
In this dataset, we can see that there are three extreme values
identified on the residuals. For all the random effects, because the
BLUPs are calculated per person, not per observation if a person is
classified as an extreme value, then all observations belonging to
that specific ID will be shown. That is because at the random effect
level, a person as a whole unit is either extreme or not and if
extreme the only "choice" would be to exclude / modify that entire person.
Because there are multiple types of extreme values, we could have some
choice in how to address them. We could remove any extreme values, or
remove one at a time and re-run the model to see if anything else
remained extreme or not. For example, we can see that ID 23 is an
extreme value on the random effect of dEnergy as well as the random
intercept. Dropping ID 23 might change the rest enough that say some
of the other residuals are no longer extreme. Conversely, we could
drop some of the extreme residuals, specific 'weird' days and see if
that happens to address any of the random effects. In this case, it is
not too likely as the extreme residuals come from different
participants (IDs 29, 64, 110) than do the extreme values on the random
effects.
A relatively intense approach would be to drop all of these extreme
values and re-estimate the LMM in the dataset with these rows / IDs
excluded. Here are different ways that we could do that.
The first approach uses the `Index` column from the extreme values
data table and then we pass that to our dataset, `dm` and use the
minus sign to indicate we want to drop those rows of data.
Note that because the same rows are extreme on a few different
measures, we use the `unique()` function so that we only drop each row
once. This never hurts to use, as if all rows are already unique its
fine, but it helps if there are duplicate extreme values (e.g., Index
29 is extreme on the random intercept and random effect of dEnergy).
```{r}
dm.noev1 <- dm[-unique(m1.diag$extremeValues$Index)]
```
Another approach, supposing we only wanted to drop selected IDs. For
example, we could decide that we want to only get rid of ID 23.