-
Notifications
You must be signed in to change notification settings - Fork 3
/
11-example_autism.Rmd
1089 lines (843 loc) · 38.1 KB
/
11-example_autism.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
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
# MLM, Longitudinal: Autism
```{r, message=FALSE, error=FALSE}
library(tidyverse) # all things tidy
library(pander) # nice looking general tabulations
library(furniture) # nice table1() descriptive
library(texreg) # Convert Regression Output to LaTeX or HTML Tables
library(psych) # contains some useful functions, like headTail
library(performance)
library(sjstats) # ICC calculations
library(sjPlot) # Quick predictive and diagnostic plots
library(effects) # Estimated Marginal Means
library(VIM) # Visualization and Imputation of Missing Values
library(naniar) # Summaries and Visualizations for Missing Data
library(lme4) # Linear, generalized linear, & nonlinear mixed models
library(optimx)
library(interactions)
library(ggResidpanel)
library(HLMdiag) # package with the dataset
```
## Background
The source: <http://www-personal.umich.edu/~kwelch/>
This data was collected by researchers at the University of Michigan [@anderson2007, [@anderson2009]] as part of a prospective longitudinal study of 214 children. The children were divided into three diagnostic groups (`bestest2`) when they were 2 years old: Autism (`autism`), Pervasive Developmental Disorder (`pdd`), and non-spectrum children (none in this sample). The study was designed to collect information on each child at approximately 2, 3, 5, 9, and 13 years of age, although not all children were measured for each age. One of the study objectives was to assess the relative influence of the initial diagnostic category, language proficiency at age 2, and other covariates on the developmental trajectories of the socialization (`vsae`) of these children.
Study participants were children who had had consecutive referrals to one of two autism clinics before the age of 3 years. Social development was assessed at each age using the Vineland Adaptive Behavior Interview survey form, a parent-reported measure of socialization. **VSAE** (Vineland Socialization Age Equivalent), was a combined score that included assessments of interpersonal relationships, play/leisure time activities, and coping skills. Initial language development was assessed using the Sequenced Inventory of Communication Development (SICD) scale; children were placed into one of three groups (`sicdegp`) based on their initial SICD scores on the expressive language subscale at age 2.
- `childid` Child's identification number for this study
- `sicdegp` Sequenced Inventory of Communication Development group *(an assessment of expressive language development)* - a factor. Levels are `low`, `med`, and `high`
- `age2` Age (in years) centered around age 2 (age at diagnosis)
- `vsae` Vineland Socialization Age Equivalent, Parent-reported measure of socialization, including measures of:
- interpersonal relationships
- play/leisure time activities
- coping skills
- `gender` Child's gender - a factor. Levels are `male` and `female`
- `race` Child's race - a factor. Levels are `white` and `nonwhite`
- `bestest2` Diagnosis at age 2 - a factor. Levels are `autism` and `pdd` (pervasive developmental disorder)
```{r}
data(autism, package = "HLMdiag") # make the dataset 'active' from this package
```
```{r}
tibble::glimpse(autism) # first look at the dataset and its varaibles
```
### Long Format
```{r}
data_long <- autism %>% # save the dataset as a new name
dplyr::mutate(childid = childid %>% factor) %>% # declare grouping var a factor
dplyr::mutate(age = 2 + age2) %>% # create the original age variable (unequally spaced)
dplyr::mutate(obs = age %>% factor %>% as.numeric) %>% # Observation Number = 1, 2, 3, 4, 5 (equally spaced)
dplyr::select(childid, # choose variables and order to keep
gender, race, bestest2, sicdegp,
obs, age, age2, vsae) %>%
dplyr::arrange(childid, age2) # sort observations
```
```{r}
data_long %>%
psych::headTail(top = 11, bottom = 6)
```
### Wide Format
```{r}
data_wide <- data_long %>% # save the dataset as a new name
dplyr::select(-age2, -obs) %>% # delete (by deselecting) this variable
tidyr::pivot_wider(names_from = age, # repeated indicator
values_from = vsae, # variable repeated
names_prefix = "vsae_") %>% # prefix in from of the
dplyr::arrange(childid) # sort observations
tibble::glimpse(data_wide)
```
Notice the missing values, displayed as `NA`.
```{r}
data_wide %>%
psych::headTail()
```
## Exploratory Data Analysis
### Demographic Summary
#### Using the WIDE formatted dataset
Each person's data is only stored on a single line
```{r}
data_wide %>%
dplyr::group_by("Initial Language Development" = sicdegp) %>% # how split into columns
furniture::table1("Diagnosis" = bestest2,
"Gender" = gender,
"Race" = race,
digits = 2,
na.rm = FALSE,
total = TRUE,
test = TRUE, # compare the groups
output = "markdown")
```
#### Using the LONG formatted dataset
Each person's data is stored on multiple lines, one for each time point. To ensure the summary table is correct, you must choose a single time point per person.
```{r}
# Note: One person is missing Age 2
data_long %>%
dplyr::filter(age == 2) %>% # restrict to one-line per person
dplyr::group_by("Initial Language Development" = sicdegp) %>% # how split into columns
furniture::table1("Diagnosis" = bestest2,
"Gender" = gender,
"Race" = race,
digits = 2,
na.rm = FALSE,
total = TRUE,
test = TRUE, # compare the groups
output = "markdown")
```
### Baseline Summary
#### Using the WIDE formatted dataset
Each person's data is only stored on a single line
```{r}
data_wide %>%
dplyr::group_by("Initial Language Development" = sicdegp) %>% # how split into columns
furniture::table1(vsae_2,
digits = 2,
na.rm = FALSE,
total = TRUE,
test = TRUE, # compare the groups
output = "markdown")
```
#### Using the LONG formatted dataset
Each person's data is stored on multiple lines, one for each time point. To ensure the summary table is correct, you must choose a single time point per person.
```{r, results="asis"}
data_long %>%
dplyr::filter(age == 2) %>%
dplyr::group_by("Initial Language Development" = sicdegp) %>% # how split into columns
furniture::table1(vsae,
test = TRUE,
output = "markdown")
```
### Missing Data & Attrition
#### `VIM` package
Plot the amount of missing vlaues and the amount of each patter of missing values.
```{r}
data_wide %>%
VIM::aggr(numbers = TRUE, # shows the number to the far right
prop = FALSE) # shows counts instead of proportions
```
#### `naniar` package
```{r}
data_wide %>%
naniar::vis_miss()
```
```{r}
data_wide %>%
naniar::gg_miss_var()
```
```{r}
data_wide %>%
naniar::gg_miss_var(show_pct = TRUE, # x-axis is PERCENT, not count
facet = sicdegp) + # create seperate panels
theme_bw() # add ggplot layers as normal
```
```{r}
data_wide %>%
naniar::gg_miss_upset()
```
```{r}
data_wide %>%
naniar::gg_miss_upset(sets = c("vsae_13_NA",
"vsae_9_NA",
"vsae_5_NA",
"vsae_3_NA",
"vsae_2_NA"),
keep.order = TRUE)
```
### Means Across Time
#### Using the WIDE formatted dataset
> Default = COMPLETE CASES ONLY!!!
Note - the sample size at each time point is the same, but subjects are only included if they have data at every time point (total n = 41)
```{r}
data_wide %>%
dplyr::group_by("Initial Language Development" = sicdegp) %>% # how split into columns
dplyr::select(starts_with("vsae_")) %>%
furniture::table1(digits = 2,
total = TRUE,
test = TRUE, # compare the groups
output = "markdown")
```
> Specify All data:
note - that the smaple sizes will be different for each time point (total n = 155)
```{r, results="asis"}
data_wide %>%
dplyr::group_by("Initial Language Development" = sicdegp) %>% # how split into columns
dplyr::select(starts_with("vsae_")) %>%
furniture::table1(digits = 2,
na.rm = FALSE,
total = TRUE,
test = TRUE, # compare the groups
output = "markdown")
```
#### Using the LONG formatted dataset
Each person's data is stored on multiple lines, one for each time point.
> FOR ALL DATA!
```{r}
data_sum_all <- data_long %>%
dplyr::group_by(sicdegp, age2) %>% # specify the groups
dplyr::summarise(vsae_n = n(), # count of valid scores
vsae_mean = mean(vsae), # mean score
vsae_sd = sd(vsae), # standard deviation of scores
vsae_sem = vsae_sd / sqrt(vsae_n)) # stadard error for the mean of scores
data_sum_all
```
> FOR COMPLETE CASES ONLY!!!
```{r}
data_sum_cc <- data_long %>%
dplyr::group_by(childid) %>% # group-by child
dplyr::mutate(child_vsae_n = n()) %>% # count the number of valid VSAE scores
dplyr::filter(child_vsae_n == 5) %>% # restrict to only thoes children with 5 valid scores
dplyr::group_by(sicdegp, age2) %>% # specify the groups
dplyr::summarise(vsae_n = n(), # count of valid scores
vsae_mean = mean(vsae), # mean score
vsae_sd = sd(vsae), # standard deviation of scores
vsae_sem = vsae_sd / sqrt(vsae_n)) # stadard error for the mean of scores
data_sum_cc
```
### Person Profile Plots
Use the data in LONG format
#### Unequally Spaced
```{r, fig.width=8, fig.height=6}
data_long %>%
dplyr::mutate(sicdegp = fct_recode(sicdegp,
"Low Communication" = "low",
"Medium Communication" = "med",
"High Communication" = "high")) %>%
ggplot(aes(x = age,
y = vsae)) +
geom_point(size = 0.75) +
geom_line(aes(group = childid),
alpha = .5,
size = 1) +
facet_grid(. ~ sicdegp) +
theme_bw() +
scale_x_continuous(breaks = c(2, 3, 5, 9, 13)) +
labs(x = "Age of Child, Years",
y = "Vineland Socialization Age Equivalent",
color = "Sequenced Inventory of Communication Development") +
geom_smooth(aes(color = "Flexible"),
method = "loess",
se = FALSE,) +
geom_smooth(aes(color = "Linear"),
method = "lm",
se = FALSE) +
scale_color_manual(name = "Smoother Type: ",
values = c("Flexible" = "blue",
"Linear" = "red")) +
theme(legend.position = "bottom",
legend.key.width = unit(2, "cm"))
```
#### Equally Spaced
```{r, fig.width=8, fig.height=6}
data_long %>%
dplyr::mutate(sicdegp = fct_recode(sicdegp,
"Low Communication" = "low",
"Medium Communication" = "med",
"High Communication" = "high")) %>%
ggplot(aes(x = obs,
y = vsae)) +
geom_point(size = 0.75) +
geom_line(aes(group = childid),
alpha = .5,
size = 1) +
facet_grid(. ~ sicdegp) +
theme_bw() +
labs(x = "Observation Number",
y = "Vineland Socialization Age Equivalent",
color = "Sequenced Inventory of Communication Development") +
geom_smooth(aes(color = "Flexible"),
method = "loess",
se = FALSE,) +
geom_smooth(aes(color = "Linear"),
method = "lm",
se = FALSE) +
scale_color_manual(name = "Smoother Type: ",
values = c("Flexible" = "blue",
"Linear" = "red")) +
theme(legend.position = "bottom",
legend.key.width = unit(2, "cm"))
```
### Side-by-side Boxplots
```{r, fig.width=8, fig.height=6}
data_long %>%
ggplot(aes(x = sicdegp,
y = vsae,
fill = sicdegp)) +
geom_boxplot() +
theme_bw() +
facet_grid(. ~ age,
labeller = "label_both") +
theme(legend.position = "none") +
labs(x = "Initial Language Development\nSequenced Inventory of Communication Development (SICD) at Age 2",
y = "Parent-Reported Measure of Socialization\nVineland Socialization Age Equivalent")
```
### Means Plots
#### Default `stat_summary`
It is nice that the `stat_summary()` layer computes the standard error for the mean for you using the data in LONG format
```{r, fig.width=8, fig.height=6}
data_long %>%
ggplot(aes(x = age,
y = vsae,
color = sicdegp)) +
stat_summary() + # default: points at MEAN and extend vertically 1 standard error for the mean
stat_summary(fun = "mean", # plot the means
geom = "line") + # ...and connect with lines
theme_bw() +
scale_x_continuous(breaks = c(2, 3, 5, 9, 13))
```
```{r, fig.width=8, fig.height=6}
data_long %>%
ggplot(aes(x = obs,
y = vsae,
color = sicdegp)) +
stat_summary() + # default: points at MEAN and extend vertically 1 standard error for the mean
stat_summary(fun = "mean", # plot the means
geom = "line") + # ...and connect with lines
theme_bw()
```
#### Manually Summarized
```{r, fig.width=8, fig.height=6}
data_sum_all %>%
dplyr::mutate(age = age2 + 2) %>%
ggplot() +
aes(x = age,
y = vsae_mean,
color = sicdegp) +
geom_errorbar(aes(ymin = vsae_mean - vsae_sem, # mean +/- one SE for the mean
ymax = vsae_mean + vsae_sem),
width = .25) +
geom_point(aes(shape = sicdegp),
size = 3) +
geom_line(aes(group = sicdegp)) +
theme_bw() +
scale_x_continuous(breaks = c(2, 3, 5, 9, 13)) +
labs(x = "Age of Child, Years",
y = "Vineland Socialization Age Equivalent",
color = "Sequenced Inventory of Communication Development:",
shape = "Sequenced Inventory of Communication Development:",
linetype = "Sequenced Inventory of Communication Development:") +
theme(legend.position = "bottom",
legend.key.width = unit(2, "cm"))
```
## Model 1: Full model with 'loaded' mean structure
Take top-down approach: Quadratic regression model, describing `vsae` as a function of `age2`
> Each child has a unique parabolic trajectory over time, with coefficients that vary randomly around fixed-effects defining a mean growth curve for each SICD group. Since there is no `age` = 0 in our data, we will use the `age2` variables, which is `age` -2, so that intercept has meaning (mean at baseline age).
> `I()` denotes an internal calculated variable
- Fixed-effects
- `I(age-2)` age
- `I((age-2)^2)` quadratic age or age-squared,
- `sicdegp` SICD group (reference group = low)
- SICD group x age/age-squared interactions
- Random-effects
- intercept
- age and age-squared
### Fit the Model
```{r}
fit_lmer_1_re <- lmerTest::lmer(vsae ~ I(age-2)*sicdegp + I(I(age-2)^2)*sicdegp +
(I(age-2) + I((age-2)^2) | childid),
data = data_long,
REML = TRUE,
control = lmerControl(optimizer = "optimx", # get it to converge
calc.derivs = FALSE,
optCtrl = list(method = "nlminb",
starttests = FALSE,
kkt = FALSE)))
```
### Table of Prameter Estimates
```{r, results='asis'}
texreg::knitreg(fit_lmer_1_re,
caption = "MLM: Full Model",
caption.above = TRUE,
single.row = TRUE)
```
### Plot of the Estimated Marginal Means
Note: the $x-axis$ is the `age` variable back on its original scale
```{r, fig.cap="Model 1: Loaded Means Structure"}
interactions::interact_plot(model = fit_lmer_1_re, # model name
pred = age, # x-axis variable (must be continuous)
modx = sicdegp, # seperate lines
interval = TRUE) + # shaded bands
scale_x_continuous(breaks = c(2, 3, 5, 9, 13))
```
## Model 2A: Drop Random Intercepts
Note: There seems to be relatively little variation in baseline measurements of VSAE across individuals in the same SICD group, so the variation at age 2 can be attributed to random error, rather than between-subject variation.
This indicates we may want to try removing the random intercepts, while retaining the same fixed- and other random-effects.
This new model implies that children have common initial VSAE value at age 2, given their SICD group.
### Fit the Model
```{r}
fit_lmer_2a_re <- lmerTest::lmer(vsae ~ I(age-2)*sicdegp + I((age-2)^2)*sicdegp +
(0 + I(age-2) + I((age-2)^2) | childid),
data = data_long,
REML = TRUE)
```
### Assess the Signifcance
```{r}
anova(fit_lmer_1_re, fit_lmer_2a_re, refit = FALSE)
```
The more complicated model (including random intercepts) does NOT fit better, thus the random intercepts may be removed from the model. Model 2a is better than Model 1
## Model 2B: Drop Random Quadratic Slope
We should formally test the necessity of quadratic age random-effect.
Comparison of nested models with REML-based LRT using a 50:50 mixture χ2-distribution with 1 and 2 df Difference of 2 covariance parameters
### Fit the Model
```{r}
fit_lmer_2b_re <- lmerTest::lmer(vsae ~ I(age-2)*sicdegp + I((age-2)^2)*sicdegp +
(0 + I(age-2) | childid),
REML = TRUE,
data = data_long)
```
### Assess the Signifcance
```{r}
anova(fit_lmer_2a_re, fit_lmer_2b_re, refit = FALSE)
```
The more complicated model *(including random intercepts)* **DOES** fit better, thus the random slopes for both the linear AND the quadratic effect of age should be retained in the model. Model 2a is better than model 2b
## Model 3: Drop Quadratic Time Fixed Effect
Fit the previous 'best' model via ML, not REML to compare nested model that differe in terms of fixed effects only
### Fit the Models
```{r}
fit_lmer_2a_ml <- lmerTest::lmer(vsae ~ I(age-2)*sicdegp + I((age-2)^2)*sicdegp +
(0 + I(age-2) + I((age-2)^2) | childid),
data = data_long,
REML = FALSE)
fit_lmer_3_ml <- lmerTest::lmer(vsae ~ I(age-2)*sicdegp +
(0 + I(age-2) + I((age-2)^2) | childid),
data = data_long,
REML = FALSE)
```
### Assess the Signifcance
```{r}
anova(fit_lmer_2a_ml, fit_lmer_3_ml)
```
The more complicated model *(including fixed interaction between quadratic time and SICD group)* **DOES** fit better, thus the higher level interaction should be retained in the model. Model 2a is better than model 3.
## Final Model
### Table of Parameter Esitmates
```{r, results='asis'}
texreg::knitreg(fit_lmer_2a_re,
caption = "MLM: Final Model",
caption.above = TRUE,
single.row = TRUE,
digits = 4)
```
### Interpretation of Fixed Effects
#### Reference Group: `low` SICD group
- $\gamma_{0}$ = `r fixef(fit_lmer_2a_re)["(Intercept)"] %>% round(3)` is the estimated marginal mean VSAE score for children in the `low` SICD, at 2 years of age
- $\gamma_{a}$ = `r fixef(fit_lmer_2a_re)["I(age - 2)"] %>% round(3)` and $\gamma_{a^2}$ = `r fixef(fit_lmer_2a_re)["I((age - 2)^2)"] %>% round(3)` are the fixed effects for age and age-squared on VSAE for children in the `low` SICD group (change over time)
Thus the equation for the estimated marginal mean VASE trajectory for the `low` SICD group is:
$$
\begin{align*}
VASE =& \gamma_{0} +
\gamma_{a} (AGE - 2) +
\gamma_{a^2} (AGE - 2)^2 \\
=& `r fixef(fit_lmer_2a_re)["(Intercept)"] %>% round(3)` +
`r fixef(fit_lmer_2a_re)["I(age - 2)"] %>% round(3)` (AGE - 2) +
`r fixef(fit_lmer_2a_re)["I((age - 2)^2)"] %>% round(3)` (AGE - 2)^2 \\
\end{align*}
$$
#### First Comparison Group: `medium` SICD group
- $\gamma_{med}$ = `r fixef(fit_lmer_2a_re)["sicdegpmed"] %>% round(3)` is the DIFFERENCE in the estimated marginal mean VSAE score for children in the `medium` vs. the `low` SICD, at 2 years of age
- $\gamma_{med:\;a}$ = `r fixef(fit_lmer_2a_re)["I(age - 2):sicdegpmed"] %>% round(3)` and $\gamma_{med:\;a^2}$ = `r fixef(fit_lmer_2a_re)["sicdegpmed:I((age - 2)^2)"] %>% round(3)` are the DIFFERENCE in the fixed effects for age and age-squared on VSAE for children in the `medium` vs. the `low` SICD group
Thus the equation for the estimated marginal mean VASE trajectory for the `medium` SICD group is:
$$
\begin{align*}
VASE =& (\gamma_{0} + \gamma_{med}) +
(\gamma_{a} + \gamma_{med:\;a}) (AGE - 2) +
(\gamma_{a^2} + \gamma_{med:\;a^2})(AGE - 2)^2 \\
=& (`r fixef(fit_lmer_2a_re)["(Intercept)"] %>% round(3)` +
`r fixef(fit_lmer_2a_re)["sicdegpmed"] %>% round(3)`) +
(`r fixef(fit_lmer_2a_re)["I(age - 2)"] %>% round(3)` +
`r fixef(fit_lmer_2a_re)["I(age - 2):sicdegpmed"] %>% round(3)`) (AGE - 2) +
(`r fixef(fit_lmer_2a_re)["I((age - 2)^2)"] %>% round(3)` +
`r fixef(fit_lmer_2a_re)["sicdegpmed:I((age - 2)^2)"] %>% round(3)`)(AGE - 2)^2 \\
=& `r (fixef(fit_lmer_2a_re)["(Intercept)"] +
fixef(fit_lmer_2a_re)["sicdegpmed"] ) %>% round(3)` +
`r (fixef(fit_lmer_2a_re)["I(age - 2)"] +
fixef(fit_lmer_2a_re)["I(age - 2):sicdegpmed"]) %>% round(3)` (AGE - 2) +
`r (fixef(fit_lmer_2a_re)["I((age - 2)^2)"] +
fixef(fit_lmer_2a_re)["sicdegpmed:I((age - 2)^2)"]) %>% round(3)` (AGE - 2)^2 \\
\end{align*}
$$
#### Second Comparison Group: `high` SICD group
- $\gamma_{hi}$ = `r fixef(fit_lmer_2a_re)["sicdegphigh"] %>% round(3)` is the DIFFERENCE in the estimated marginal mean VSAE score for children in the `high` vs. the `low` SICD, at 2 years of age
- $\gamma_{hi:\;a}$ = `r fixef(fit_lmer_2a_re)["I(age - 2):sicdegphigh"] %>% round(3)` and $\gamma_{hi:\;a^2}$ = `r fixef(fit_lmer_2a_re)["sicdegphigh:I((age - 2)^2)"] %>% round(3)` are the DIFFERENCE in the fixed effects for age and age-squared on VSAE for children in the `high` vs. the `low` SICD group
Thus the equation for the estimated marginal mean VASE trajectory for the `high` SICD group is:
$$
\begin{align*}
VASE =& (\gamma_{0} + \gamma_{hi}) +
(\gamma_{a} + \gamma_{hi:\;a}) (AGE - 2) +
(\gamma_{a^2} + \gamma_{hi:\;a^2})(AGE - 2)^2 \\
=& (`r fixef(fit_lmer_2a_re)["(Intercept)"] %>% round(3)` +
`r fixef(fit_lmer_2a_re)["sicdegphigh"] %>% round(3)`) +
(`r fixef(fit_lmer_2a_re)["I(age - 2)"] %>% round(3)` +
`r fixef(fit_lmer_2a_re)["I(age - 2):sicdegphigh"] %>% round(3)`) (AGE - 2) +
(`r fixef(fit_lmer_2a_re)["I((age - 2)^2)"] %>% round(3)` +
`r fixef(fit_lmer_2a_re)["sicdegphigh:I((age - 2)^2)"] %>% round(3)`)(AGE - 2)^2 \\
=& `r (fixef(fit_lmer_2a_re)["(Intercept)"] +
fixef(fit_lmer_2a_re)["sicdegphigh"] ) %>% round(3)` +
`r (fixef(fit_lmer_2a_re)["I(age - 2)"] +
fixef(fit_lmer_2a_re)["I(age - 2):sicdegphigh"]) %>% round(3)` (AGE - 2) +
`r (fixef(fit_lmer_2a_re)["I((age - 2)^2)"] +
fixef(fit_lmer_2a_re)["sicdegphigh:I((age - 2)^2)"]) %>% round(3)` (AGE - 2)^2 \\
\end{align*}
$$
### Interpretation of Random Effects
```{r}
lme4::VarCorr(fit_lmer_2a_re)%>%
print(comp = c("Variance", "Std.Dev"),
digits = 3)
```
```{r, include=FALSE}
lme4::VarCorr(fit_lmer_2a_re) %>%
data.frame()
fit_lmer_2a_re_sigmas <- lme4::VarCorr(fit_lmer_2a_re) %>%
data.frame()
fit_lmer_2a_re_sigmas
```
**Here a group of observations = a CHILD**
#### Residual Varaince
**Within-child-variance**
- $e_{ti}$ the residuals associated with observation at time $t$ on child $i$
```{r, echo=FALSE, results="hide"}
sigma2_e <- lme4::VarCorr(fit_lmer_2a_re) %>%
data.frame %>%
dplyr::filter(grp == "Residual") %>%
dplyr::select(vcov)
sigma2_e
```
```{r, echo=FALSE, results="hide"}
sigma_e <- fit_lmer_2a_re_sigmas %>%
dplyr::filter(grp == "Residual") %>%
dplyr::pull(vcov)
sigma_e
```
$$
\sigma^2 = \sigma^2_e
= VAR[e_{ti}]
= `r round(sigma2_e, 3)`
$$
#### 2 Variance Components
**Between-children slope variances**
Random LINEAR effect of age variance
- $u_{1i}$ the DIFFERENCE between child $i$'s specific linear component for age and the fixed linear component for age, given their SICD group
```{r, echo=FALSE, results="hide"}
sigma2_u1 <- lme4::VarCorr(fit_lmer_2a_re) %>%
data.frame %>%
dplyr::filter(var1 == "I(age - 2)", is.na(var2)) %>%
dplyr::select(vcov)
sigma2_u1
```
```{r, echo=FALSE, results="hide"}
tau_11 <- fit_lmer_2a_re_sigmas %>%
dplyr::filter(grp == "childid", var1 == "I(age - 2)", is.na(var2)) %>%
dplyr::pull(vcov)
tau_11
```
$$
\tau_{11} = \sigma^2_{u1}
= VAR[u_{1i}]
= `r tau_11 %>% round(2)`
$$
Random QUADRATIC effect of age variance
- $u_{2i}$ the DIFFERENCE between child $i$'s specific quadratic component for age and the fixed quadratic component for age, given their SICD group
```{r, echo=FALSE, results="hide"}
sigma2_u2 <- lme4::VarCorr(fit_lmer_2a_re) %>%
data.frame %>%
dplyr::filter(var1 == "I((age - 2)^2)", is.na(var2)) %>%
dplyr::select(vcov)
sigma2_u2
```
```{r, echo=FALSE, results="hide"}
tau_22 <- fit_lmer_2a_re_sigmas %>%
dplyr::filter(grp == "childid", var1 == "I((age - 2)^2)", is.na(var2)) %>%
dplyr::pull(vcov)
tau_22
```
$$
\tau_{22} = \sigma^2_{u2}
= VAR[u_{2i}]
= `r tau_22 %>% round(2)`
$$
#### 1 Covariance (or correlation) Components
**Slope-slope covariance**
Random LINEAR and Quadratic effect of age covariance:
```{r, echo=FALSE, results="hide"}
sigma2_t12 <- lme4::VarCorr(fit_lmer_2a_re) %>%
data.frame %>%
dplyr::filter(var1 == "I(age - 2)", var2 == "I((age - 2)^2)") %>%
dplyr::select(vcov)
sigma2_t12
```
```{r, echo=FALSE, results="hide"}
tau_12 <- fit_lmer_2a_re_sigmas %>%
dplyr::filter(grp == "childid", var1 == "I(age - 2)", var2 == "I((age - 2)^2)") %>%
dplyr::pull(vcov)
tau_12
```
$$
\tau_{12} = \sigma^2_{u12}
= COV[u_{1i}, u_{2i}]
= `r tau_12 %>% round(2)`
$$
$$
Correlation(u_{1i}, u_{2i}) = -0.324
$$
### Assumption Checking
The residuals are:
- Assumed to be normally, independently, and identically distributed (conditional on other random-effects)
- Assumed independent of random-effects
$$
e_{ti} \sim N(0, \sigma^2)
$$
#### The `ggResidpanel` package
```{r}
ggResidpanel::resid_panel(fit_lmer_2a_re) # default = pearson residuals
```
#### Manually with `HLMdiag` and `ggplot2`
```{r}
fit_lmer_2a_re %>%
HLMdiag::hlm_augment(level = 1,
include.ls = FALSE,
standardized = TRUE) %>%
ggplot(aes(x = .resid)) +
geom_histogram(bins = 40,
color = "gray20",
alpha = .2) +
theme_bw() +
labs(main = "Histogram")
```
```{r}
fit_lmer_2a_re %>%
ranef() %>%
data.frame() %>%
ggplot(aes(sample = condval)) +
geom_qq() +
geom_qq_line() +
facet_wrap(~ term, scale = "free_y")+
theme_bw() +
labs(main = "Random Slopes")
```
```{r}
fit_lmer_2a_re %>%
HLMdiag::hlm_augment(level = 1,
include.ls = FALSE,
standardized = TRUE) %>%
ggplot(aes(x = .fitted,
y = .resid)) +
geom_hline(yintercept = 0, color = "red") +
geom_point() +
geom_smooth() +
theme_bw() +
labs(main = "Residual Plot")
```
```{r}
fit_lmer_2a_re %>%
HLMdiag::hlm_augment(level = 1,
include.ls = FALSE,
standardized = TRUE) %>%
ggplot(aes(sample = .resid)) +
geom_qq()+
stat_qq_line() +
theme_bw()
```
#### The `sjPlot` package
```{r}
sjPlot::plot_model(fit_lmer_2a_re,
type = "diag")
```
### Plot of the Estimated Marginal Means
#### Quick and Default
Note: the $x-axis$ is the `age` variable, back on its original scale
```{r, fig.cap="Final Model (2a)"}
interactions::interact_plot(model = fit_lmer_2a_re, # model name
pred = age, # x-axis variable (must be continuous)
modx = sicdegp, # seperate lines
interval = TRUE) + # shaded bands
scale_x_continuous(breaks = c(2, 3, 5, 9, 13))
```
```{r}
interactions::interact_plot(model = fit_lmer_2a_re, # model name
pred = age, # x-axis variable (must be continuous)
modx = sicdegp, # separate lines
interval = TRUE,
x.label = "Age of Child, in years",
y.label = "Estimated Marginal Mean\nVineland Socialization Age Equivalent",
legend.main = "Initial Communication (SICD)",
modx.labels = c("Low", "Medium", "High"),
colors = c("black", "black", "black")) +
scale_x_continuous(breaks = c(2, 3, 5, 9, 13)) +
scale_y_continuous(breaks = seq(from = 0, to = 120, by = 20)) +
theme_bw() +
theme(legend.position = c(0, 1),
legend.justification = c(-0.1, 1.1),
legend.background = element_rect(color = "black"),
legend.key.width = unit(2, "cm"))
```
#### More Customized - Color
This version would look better on a poster or in a slide presentation.
```{r}
effects::Effect(focal.predictors = c("age","sicdegp"), # variables involved in interactions
mod = fit_lmer_2a_re,
xlevels = list(age2 = seq(from = 2, to = 13, by = .1))) %>% # add more values to smooth out the prediction lines and ribbons
data.frame() %>%
dplyr::mutate(sicdegp = factor(sicdegp,
levels = c("high", "med", "low"),
labels = c("High", "Medium", "Low"))) %>%
ggplot(aes(x = age,
y = fit,
group = sicdegp)) +
geom_ribbon(aes(ymin = lower, # 95% Confidence Intervals
ymax = upper,
fill = sicdegp),
alpha = .3) +
geom_line(aes(linetype = sicdegp,
color = sicdegp),
size = 1) +
scale_x_continuous(breaks = c(2, 3, 5, 9, 13)) + # mark values that were actually measured
scale_y_continuous(breaks = seq(from = 0, to = 120, by = 20)) +
scale_linetype_manual(values = c("solid", "longdash", "dotted")) +
theme_bw() +
theme(legend.position = c(0, 1),
legend.justification = c(-0.1, 1.1),
legend.background = element_rect(color = "black"),
legend.key.width = unit(2.5, "cm")) +
labs(x = "Age of Child, in years",
y = "Estimated Marginal Mean\nVineland Socialization Age Equivalent",
linetype = "Initial Communication (SICD)",
fill = "Initial Communication (SICD)",
color = "Initial Communication (SICD)")
```
#### More Customized - Black and White
This version would be better for a publication.
```{r}
effects::Effect(focal.predictors = c("age","sicdegp"),
mod = fit_lmer_2a_re,
xlevels = list(age2 = seq(from = 2, to = 13, by = .1))) %>%
data.frame() %>%
dplyr::mutate(sicdegp = factor(sicdegp,
levels = c("high", "med", "low"),
labels = c("High", "Medium", "Low"))) %>%
ggplot(aes(x = age,
y = fit,
group = sicdegp)) +
geom_ribbon(aes(ymin = lower,
ymax = upper,
fill = sicdegp),
alpha = .4) +
geom_line(aes(linetype = sicdegp),
size = .7) +
scale_x_continuous(breaks = c(2, 3, 5, 9, 13)) +
scale_y_continuous(breaks = seq(from = 0, to = 120, by = 20)) +
scale_linetype_manual(values = c("solid", "longdash", "dotted")) +
scale_fill_manual(values = c("gray10", "gray40", "gray60")) +
theme_bw() +
theme(legend.position = c(0, 1),
legend.justification = c(-0.1, 1.1),
legend.background = element_rect(color = "black"),
legend.key.width = unit(2, "cm")) +
labs(x = "Age of Child, in years",
y = "Estimated Marginal Mean\nVineland Socialization Age Equivalent",
linetype = "Initial Communication (SICD)",
fill = "Initial Communication (SICD)",
color = "Initial Communication (SICD)")
```
### Post Hoc Compairisons
```{r}
fit_lmer_2a_re %>%
emmeans::emmeans(pairwise ~ sicdegp,
at = list(age = 13))
```
```{r}
totalSD <- VarCorr(fit_lmer_2a_re) %>%
data.frame() %>%
dplyr::summarise(tot_var = sum(vcov)) %>%
dplyr::pull(tot_var) %>%
sqrt()
totalSD
```