-
Notifications
You must be signed in to change notification settings - Fork 1
/
selection.qmd
1369 lines (1168 loc) · 39.1 KB
/
selection.qmd
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: "Model Selection"
subtitle: "Using latent class mixed models with natural cubic splines."
author:
- name: "Nathan Contantine-Cooke"
url: https://scholar.google.com/citations?user=2emHWR0AAAAJ&hl=en&oi=ao
corresponding: true
affiliations:
- ref: HGU
- ref: CGEM
- name: "Karla Monterrubio-Gómez"
url: https://scholar.google.com/citations?user=YmyxSXAAAAAJ&hl=en
affiliations:
- ref: HGU
- name: "Riccardo E. Marioni"
url: https://scholar.google.com/citations?hl=en&user=gA3Ik3MAAAAJ
affiliations:
- ref: CGEM
- name: "Catalina A. Vallejos"
url: https://scholar.google.com/citations?user=lkdrwm0AAAAJ&hl=en&oi=ao
affiliations:
- ref: HGU
- ref: Turing
#comments:
# giscus:
# repo: quarto-dev/quarto-docs
---
## Introduction
```{r Setup}
#| message: false
set.seed(123)
# Fitting the LCMMs takes around three hours (when running single threaded)
# if cache.models is true then the saved model objects will be used instead of
# refitting the models
cache.models <- TRUE
##########################
#-- Packages --#
##########################
library(tidyverse)
## Modelling ##
library(lcmm)
library(splines)
## Presentation ##
library(htmltools)
library(patchwork)
library(ggdist)
library(grid)
library(ggalluvial)
library(qqplotr)
if (!require(DT)) {
install.packages("DT")
}
##########################
#-- Data read --#
##########################
FCcumulative <- readRDS(paste0("/Volumes/igmm/cvallejo-predicct/",
"cdi/processed/",
"FCcumulativeLongInc.RDS")
)
###########################################
#-- Create directories and readme files --#
###########################################
if (!dir.exists("plots")) {
dir.create("plots")
}
fileConn <- file("plots/README.md")
writeLines(c("# README",
"",
paste("This directory contains plots created by the analysis",
"but are not provided as figures (supplementary or",
"otherwise) in the paper")),
fileConn)
close(fileConn)
if (!dir.exists("cache")) dir.create("cache")
fileConn <- file("cache/README.md")
writeLines(c("# README",
"",
paste("This directory stores cached versions of R objects which",
"take a long time to create (such as LCMM fit objects).")),
fileConn)
close(fileConn)
if (!dir.exists("paper")) dir.create("paper")
fileConn <- file("paper/README.md")
writeLines(c("# README",
"",
paste("This directory contains all figures used in the paper.",
"The sup subdirectory contains supplementary figures")),
fileConn)
close(fileConn)
if (!dir.exists("paper/sup")) dir.create("paper/sup")
if (!dir.exists("plots/residuals")) {
dir.create("plots/residuals")
}
########################
#-- Custom Functions --#
########################
# Build DT::datatable objects from matrix of fit statistics
DTbuild <- function(hlme.metric, caption) {
hlme.metrics <- cbind(hlme.metrics, group = seq(1, nrow(hlme.metrics)))
hlme.metrics <- hlme.metrics[, c(4, 1, 2, 3)]
DT::datatable(round(as.data.frame(hlme.metrics), 2),
options = list(dom = 't'),
caption = tags$caption(
style = 'text-align: center;',
h3(caption)),
style ="bootstrap4",
rownames = FALSE,
colnames = c("Clusters",
"Maximum log-likelihood",
"AIC",
"BIC"),
escape = FALSE)
}
#' Spaghetti plots of each class
#' @param models list containing HLME objects
#' @param G How many classes does the model assume?
#' @param log Logical. Should plots be on log scale
#' @param indi Logical. Should separate plots for each class be generated?
#' @param multi Logical. Should all plots be plotted alongside each other?
#' @param tmax Maximum observation period
#' @param column Logical. Should all sub-plots be in a single column? Defaults
#' to false (two columns)
#' @param prob.cutoff Posterior probability cut-off for subjects to be included
#' as trajectories
#' @param mapping Numeric vector which gives reordering of plots in a
#' multiplot. Need to take into account plots are generated by column - not
#' row
#' @param sizes Output latent class sizes
#' @param save Logical. Should sub figure labels be generated?
spaghetti_plot <- function(FCcumulative,
models,
G,
log = TRUE,
indi = FALSE,
multi = TRUE,
tmax = 5,
column = FALSE,
pprob.cutoff = NA,
sizes = FALSE,
mapping = NULL,
save = FALSE){
if(!is.na(pprob.cutoff)) {
pprob.cutoffs <- c()
for (subject in unique(models[[G]]$pprob$id)){
temp <- subset(models[[G]]$pprob, id == subject)
pprob <- temp[, 2 + temp$class]
if (pprob > pprob.cutoff) {
pprob.cutoffs <- c(pprob.cutoffs, subject)
}
}
FCcumulative <- subset(FCcumulative, id %in% pprob.cutoffs)
}
if (indi){
spaghetti_plot_sub(FCcumulative = FCcumulative,
models = models,
G = G,
log = log,
multi = FALSE,
tmax = tmax,
column = column,
pprob = pprob,
sizes = sizes,
mapping = mapping)
}
if (multi){
spaghetti_plot_sub(FCcumulative = FCcumulative,
models = models,
G = G,
log = log,
multi = TRUE,
tmax = tmax,
column = column,
pprob = pprob,
sizes = sizes,
mapping = mapping,
save = save)
}
}
spaghetti_plot_sub <- function(FCcumulative,
models,
G,
multi = FALSE,
log,
unit,
tmax = 5,
column = column,
pprob = NA,
sizes = sizes,
mapping = mapping,
save = save){
labels <- c("A", "C", "B", "D")
time <- seq(0, tmax, by = 0.01)
if (column) {
# use single column layout for sub-plots
layout <- matrix(seq(1, G),
ncol = 1,
nrow = G)
} else {
# use two column layout for sub-plots
layout <- matrix(seq(1, 2 * ceiling(G / 2)),
ncol = 2,
nrow = ceiling(G / 2))
}
# Set up the page
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
data_pred <- data.frame(time = time)
pred <- predictY(models[[G]],
data_pred,
var.time = "time",
draws = TRUE)
lcmm_uit <- as.data.frame(pred$pred)
lcmm_uit$time <- time
if(is.null(mapping)) {
mapping <- 1:G
}
for (g in mapping) {
matchidx <- as.data.frame(which(layout == g, arr.ind = TRUE))
id.group <- models[[G]]$pprob[models[[G]]$pprob[, 2] == mapping[g], 1]
if (sizes) {
message("There are ",
length(id.group),
" subjects in cluster ",
g,
".")
}
if(!log){
p[[g]] <- ggplot(data = subset(FCcumulative, id %in% id.group),
aes(x = time, y = exp(value))) +
geom_line(aes(group = id), alpha = 0.1) +
theme_minimal() +
geom_line(data = lcmm_uit,
aes(x = time,
y = exp(lcmm_uit[, paste0("Ypred_class", mapping[g])])),
size = 1.5,
col = "red") +
geom_line(data = lcmm_uit,
aes(x = time,
y = exp(lcmm_uit[, paste0("lower.Ypred_class",
mapping[g])])),
col = "red",
lty = 2) +
geom_line(data = lcmm_uit,
aes(x = time,
y = exp(lcmm_uit[, paste0("upper.Ypred_class",
mapping[g])])),
col = "red",
lty = 2) +
xlab("Time (years)") +
ylab("FCAL (μg/g)") +
ylim(0, 2500)
} else{
p[[g]] <- ggplot(data = subset(FCcumulative, id %in% id.group),
aes(x = time, y = value)) +
geom_line(aes(group = id), alpha = 0.1) +
theme_minimal() +
geom_line(data = lcmm_uit,
aes(x = time, y = lcmm_uit[, paste0("Ypred_class",
mapping[g])]),
size = 1.5,
col = "red") +
geom_line(data = lcmm_uit,
aes(x = time, y = lcmm_uit[, paste0("lower.Ypred_class",
mapping[g])]),
col = "red",
lty = 2) +
geom_line(data = lcmm_uit,
aes(x = time, y = lcmm_uit[, paste0("upper.Ypred_class",
mapping[g])]),
col = "red",
lty = 2) +
geom_hline(yintercept = log(250),
color = "#007add",
lty = 3,
size = 1.5) +
xlab("Time (years)") +
ylab("Log (FCAL (μg/g))") +
ylim(2, log(2500))
}
if (multi) {
if (save) {
# Add subfigure labels
print(p[[g]] +
ggtitle(labels[g]) +
theme_classic() +
theme(axis.line = element_line(colour = "gray"),
plot.title = element_text(face = "bold",
size = 20)),
vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col)
)
} else {
print(p[[g]],
vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col)
)
}
} else {
print(p[[g]])
}
}
}
```
To achieve our aims of identifying clusters within the CD population with
similar FCAL profiles, we use latent class mixed models (LCMMs) with natural
cubic spline formulations for the fixed and random effects components. LCMMs
are an extension of linear mixed effects models with an added fixed
effect class-specific component. Cluster membership in a LCMM is given via
a multinomial logistic model.
Previously, we have investigated using polynomial regression models with an
I-splines link function, and models which use Gaussian radial basis functions
(GRBFs) in order to model the fixed and random components of an LCMM.
Unfortunately, the polynomial regression and I-splines link approaches
demonstrated inflexibility and, in the case of polynomial regression behaved
erratically near the ends of the time period.
The GRBF approach was very sensitive to $l$, a length scale parameter, and the
iterative Marquardt algorithm implemented in the `{lcmm}` package did not
converge in many cases- possibly due the number of parameters required to be
estimated and/or the Runge phenomenon [@Fornberg2007].
This has led us to consider an approach using natural cubic splines which has a
few notable advantages [@Elhakeem2022]:
1. Less parameters need to be estimated than either a Gaussian radial basis
function regression model or a polynomial regression model with the same
flexibility . This reduces the time complexity when fitting the model and in
the future may also make extensions more practically feasible.
2. Natural cubic splines enforce linearity between $t_0$ and the first knot and
between the last knot and $t_\text{max}$ which ensures the model does not
behave erratically in these sometimes problematic areas.
3. Natural cubics are not highly sensitive to a continuous parameter and instead
requires only $K$, the number of knots, to be tuned: being robust to where
the knots themselves are placed.
## Formal defintions
For formal definitions of the models and statistics we have used in the work,
please see the supplementary material for our paper.
## The Crohn's Disease Inception Cohort
The background for the data we will fit to the models and an explanation of
the data processing steps implemented can be found on a
[dedicated page](data-cleaning.qmd). Due to the distribution of the FCAL
values (@fig-dist), FCAL values have been log-transformed prior to model fitting
(@fig-dist-log).
```{R Log transform}
#| label: fig-dist
#| fig-cap: "Distribution of FCAL values when in measurement units."
FCcumulative %>%
ggplot(aes(x = value, y = NULL)) +
stat_slab(size = 0.8, alpha = 0.5, fill = "#235789") +
geom_dots(binwidth = 10, size = 1, side = "bottom", color = "#235789") +
theme_minimal() +
theme(axis.text.y = element_blank()) +
xlab("FCAL (µg/g)") +
ylab("") +
ggtitle("Distribution of FCAL Measurements",
"Crohn's disease inception cohort")
```
```{R}
#| label: fig-dist-log
#| fig-cap: "Distribution of FCAL values after a log transformation has been applied."
FCcumulative$value <- log(FCcumulative$value)
FCcumulative %>%
ggplot(aes(x = value, y = NULL)) +
stat_slab(size = 0.8, alpha = 0.5, fill = "#235789") +
geom_dots(binwidth = 0.02, size = 1, side = "bottom", color = "#235789") +
theme_minimal() +
theme(axis.text.y = element_blank()) +
xlab("Log (FCAL (µg/g))") +
ylab("") +
ggtitle("Distribution of Log-Transformed FCAL Measurements",
"Crohn's disease inception cohort")
```
## Model fitting
LCMMs with 2 - 6 assumed clusters are considered. As recommended by
@Proust-Lima2017, a model is initially fitted with one cluster (I.E a regular
linear mixed effects model) which is used to sample initial values in a grid
search approach which attempts to find optimal models for each assumed number of
clusters based upon maximum likelihood. The trajectory of this linear mixed
effects model is given by @fig-lme.
For the fixed and random components of each model, we will consider natural
cubic splines of time with three knots (I.E five fixed points including the
boundaries of the splines. The knots for the natural cubic splines are placed at
the 1<sup>st</sup> quantile, median, and 3<sup>rd</sup>
quartile of the FCAL measurement times for the study cohort. This corresponds to
[`r round(attr(ns(FCcumulative$time, df = 4), "knots"), 2)`] years from
diagnosis.
```{R Model fitting}
#| fig-width: 12
#| fig-height: 6.75
#| label: fig-lme
#| fig-cap: "Linear mixed effects (LME) model fitted to data and used to generate inital values for the grid search method used for LCMMs with $G > 2$"
ngroups <- c(2, 3, 4, 5, 6)
rep <- 50
maxiter <- 10
if (!file.exists("cache/cubicbf.fits.RDS") | !cache.models){
m1 <- hlme(fixed = value ~ ns(time, df = 4),
random = ~ ns(time, df = 4),
subject = "id",
data = FCcumulative,
verbose = FALSE,
var.time = "time",
maxiter = 8000)
print(summary(m1))
if (!m1$conv) stop("LME did not converge \n")
hlme.metrics <- matrix(nrow = 0, ncol = 3)
colnames(hlme.metrics) <- c("maximum log-likelihood", "AIC", "BIC")
temp <- matrix(c(m1$loglik, m1$AIC, m1$BIC), nrow = 1)
rownames(temp) <- "1"
hlme.metrics <- rbind(hlme.metrics, temp)
cubicbf.fits <- list()
cubicbf.fits[["group1"]] <- m1
for (ngroup in ngroups) {
ng <- ngroup
cl <- parallel::makeCluster(parallel::detectCores())
parallel::clusterExport(cl, "ng")
hlme.fit <- gridsearch(
rep = rep,
maxiter = maxiter,
minit = m1,
cl = cl,
hlme(fixed = value ~ ns(time, df = 4),
mixture = ~ ns(time, df = 4),
random = ~ ns(time, df = 4),
subject = "id",
ng = ng,
data = FCcumulative,
verbose = FALSE)
)
parallel::stopCluster(cl)
cubicbf.fits[[paste0("group", ngroup)]] <- hlme.fit
if (hlme.fit$conv) {
cat("Convergence achieved for ", ng, "subgroups ✅ \n")
} else {
cat("Convergence NOT achieved for ", ng, " subgroups ⚠️ \n")
}
temp <- matrix(c(hlme.fit$loglik, hlme.fit$AIC, hlme.fit$BIC), nrow = 1)
rownames(temp) <- ngroup
hlme.metrics <- rbind(hlme.metrics, temp)
}
saveRDS(cubicbf.fits, "cache/cubicbf.fits.RDS")
saveRDS(hlme.metrics, "cache/cubicbf.RDS")
} else{
cubicbf.fits <- readRDS("cache/cubicbf.fits.RDS")
hlme.metrics <- readRDS("cache/cubicbf.RDS" )
}
m1 <- cubicbf.fits[[1]]
x <- predictY(m1,
newdata = data.frame(time = seq(0, 5, by = 0.01)),
var.time = "time",
draws = TRUE)
par(mfrow = c(1, 2))
plot(FCcumulative$time,
FCcumulative$value,
xlab = "Time",
ylab = "Log(FCAL)",
main = "LME with Cubic Natural Splines (Log Scale)",
col = rgb(0,0,0, 0.3))
lines(x$times[,1], x$pred[,1], col = "red")
lines(x$times[,1], x$pred[,2], col = "red", lty = 2) # Conf intervals
lines(x$times[,1], x$pred[,3], col = "red", lty = 2)
# Plot knots as vertical lines
abline(v = as.numeric(attr(ns(FCcumulative$time, df = 4),
"knots")),
col = "blue",
lty = 3)
plot(FCcumulative$time,
exp(FCcumulative$value),
xlab = "Time",
ylab = "FCAL",
main = "LME with Cubic Natural Splines (Measurement Scale)",
col = rgb(0,0,0, 0.3))
lines(x$times[, 1], exp(x$pred[, 1]), col = "red")
lines(x$times[, 1], exp(x$pred[, 2]), col = "red", lty= 2)
lines(x$times[, 1], exp(x$pred[, 3]), col = "red", lty= 2)
abline(v = as.numeric(attr(ns(FCcumulative$time, df = 4), "knots")),
col = "blue", lty = 3)
par(mfrow = c(1,1))
```
```{R Save LME, include = FALSE}
png(file = "LME.experiment.png", width = 16, height = 9, units = "in", res = 300)
par(mfrow = c(1, 2))
plot(FCcumulative$time,
FCcumulative$value,
xlab = "Time",
ylab = "Log(FCAL)",
main = "LME with Cubic Natural Splines (Log Scale)",
col = rgb(0,0,0, 0.3))
lines(x$times[, 1], x$pred[, 1], col = "red")
lines(x$times[, 1], x$pred[, 2], col = "red", lty = 2) # Conf intervals
lines(x$times[, 1], x$pred[, 3], col = "red", lty = 2)
# Plot knots as vertical lines
abline(v = as.numeric(attr(ns(FCcumulative$time, df = 4), "knots")),
col = "blue",
lty = 3)
plot(FCcumulative$time,
exp(FCcumulative$value),
xlab = "Time",
ylab = "FCAL",
main = "LME with Cubic Natural Splines (Measurement Scale)",
col = rgb(0,0,0, 0.3))
lines(x$times[, 1], exp(x$pred[, 1]), col = "red")
lines(x$times[, 1], exp(x$pred[, 2]), col = "red", lty= 2)
lines(x$times[, 1], exp(x$pred[, 3]), col = "red", lty= 2)
abline(v = as.numeric(attr(ns(FCcumulative$time, df = 4), "knots")),
col = "blue",
lty = 3)
dev.off()
par(mfrow = c(1, 1))
```
## Model selection
### Model fit
We consider two metrics when considering model fit: AIC and BIC which penalises
model complexity.
```{R Fit statistics}
#| fig-width: 10
#| fig-height: 5.5
groups.1 <- cubicbf.fits[[1]]
groups.2 <- cubicbf.fits[[2]]
groups.3 <- cubicbf.fits[[3]]
groups.4 <- cubicbf.fits[[4]]
groups.5 <- cubicbf.fits[[5]]
groups.6 <- cubicbf.fits[[6]]
DTbuild(hlme.metrics,
caption = "Fit Metrics for Natural Cubic Splines Model")
```
Considering all of the models above, AIC is most optimal for the $G = 5$
model and BIC is optimal for the $G = 2$ model. However, considering an alluvial
plot (@fig-alluvial) suggests neither $G = 2$ nor $G = 5$ are suitable models.
The $G = 2$ model clearly has additional well defined cluster not properly
represented by just two clusters, whilst the $G=5$ model results in an
incredibly small new cluster. As such, $G = 4$ may be a more suitable
alternative.
### Cluster discrimination
#### Alluvial plots
```{R Alluvial plots}
#| warning: false
#| label: fig-alluvial
#| fig-cap: "Alluvial plot demonstrating how cluster membership changes as the assumed number of clusters increase. The height of each band indicates the size of each cluster."
re_label <- function(old.G, new.G, alluvial.df){
new.order <- rep(new.G, new.G)
old.clusters <- subset(alluvial.df, G == old.G)
new.clusters <- subset(alluvial.df, G == new.G)
for (g in old.G:1) {
ids <- subset(old.clusters, class == g)$id
for (new.g in 1:new.G) {
new.clusters.g <- subset(new.clusters, new.g == class)
if (nrow(subset(new.clusters.g, id %in% ids)) > 0.5 * length(ids)) {
new.order[new.g] <- g
}
}
}
alluvial.df[alluvial.df[, "G"] == new.G, "class"] <-
plyr::mapvalues(alluvial.df[alluvial.df[, "G"] == new.G, "class"],
from = seq(1, new.G),
new.order)
return(alluvial.df)
}
# convert to alluvial format
alluvial.df <-cbind(groups.2$pprob[, 1:2], G = 2)
alluvial.df <-rbind(alluvial.df, cbind(groups.3$pprob[, 1:2], G = 3))
alluvial.df <-rbind(alluvial.df, cbind(groups.4$pprob[, 1:2], G = 4))
alluvial.df <-rbind(alluvial.df, cbind(groups.5$pprob[, 1:2], G = 5))
alluvial.df <-rbind(alluvial.df, cbind(groups.6$pprob[, 1:2], G = 6))
alluvial.df$id <- as.character(alluvial.df$id)
alluvial.df$class <- as.factor(alluvial.df$class)
# eliminate label switching
alluvial.df <- re_label(2, 3, alluvial.df)
alluvial.df <- re_label(3, 4, alluvial.df)
alluvial.df <- re_label(4, 5, alluvial.df)
alluvial.df <- re_label(5, 6, alluvial.df)
p <- ggplot(alluvial.df,
aes(x = G,
stratum = class,
alluvium = id,
fill = class,
label = class)) +
scale_x_discrete(expand = c(.1, .1)) +
geom_flow() +
geom_stratum(alpha = 0.5) +
geom_text(stat = "stratum", size = 3) +
theme_classic() +
theme(axis.line = element_line(colour = "gray")) +
theme(legend.position = "none") +
ggtitle("Alluvial plot of cluster membership across G",
"Crohn's disease inception cohort") +
scale_fill_manual(values = c("#e3281f",
"#3aa534",
"#ff5885",
"#fbc926",
"#511e9d",
"black")) +
xlab("Assumed number of clusters") +
ylab("Frequency")
print(p)
p <- p + ggtitle("", "")
ggsave("paper/alluvial.png", p, width = 8, height = 4.5, units = "in")
ggsave("paper/alluvial.pdf", p, width = 8, height = 4.5, units = "in")
```
#### Posterior classifications
An alternative to co-clustering when considering cluster discrimination is to
consider posterior classification possibilities. From the below data, we can
see how these posterior probabilities change as the number of assumed clusters
increase
::: {.panel-tabset}
##### G = 2
```{R Pprob G2}
postprob(cubicbf.fits[[2]])
```
##### G = 3
```{R Pprob G3}
postprob(cubicbf.fits[[3]])
```
##### G = 4
```{R Pprob G4}
postprob(cubicbf.fits[[4]])
```
##### G = 5
```{R Pprob G5}
postprob(cubicbf.fits[[5]])
```
##### G = 6
```{R Pprob G6}
postprob(cubicbf.fits[[6]])
```
:::
```{R Pprob distributions}
#| fig-width: 8
#| fig-height: 4.5
pprobs.2 <- c()
pprobs.3 <- c()
pprobs.4 <- c()
pprobs.5 <- c()
pprobs.6 <- c()
for (i in 1:nrow(cubicbf.fits[[1]]$pprob)){
class.2 <- groups.2$pprob[i, 2]
pprobs.2 <- c(pprobs.2, groups.2$pprob[i, class.2 + 2 ])
class.3 <- groups.3$pprob[i, 2]
pprobs.3 <- c(pprobs.3, groups.3$pprob[i, class.3 + 2 ])
class.4 <- groups.4$pprob[i, 2]
pprobs.4 <- c(pprobs.4, groups.4$pprob[i, class.4 + 2 ])
class.5 <- groups.5$pprob[i, 2]
pprobs.5 <- c(pprobs.5, groups.5$pprob[i, class.5 + 2 ])
class.6 <- groups.6$pprob[i, 2]
pprobs.6 <- c(pprobs.6, groups.6$pprob[i, class.6 + 2 ])
}
pprobs.2 <- tibble(prob = pprobs.2)
pprobs.3 <- tibble(prob = pprobs.3)
pprobs.4 <- tibble(prob = pprobs.4)
pprobs.5 <- tibble(prob = pprobs.5)
pprobs.6 <- tibble(prob = pprobs.6)
pprobs.2$Model <- as.factor(rep("Two clusters", nrow(pprobs.2)))
pprobs.3$Model <- as.factor(rep("Three clusters", nrow(pprobs.3)))
pprobs.4$Model <- as.factor(rep("Four clusters", nrow(pprobs.4)))
pprobs.5$Model <- as.factor(rep("Five clusters", nrow(pprobs.5)))
pprobs.6$Model <- as.factor(rep("Six clusters", nrow(pprobs.6)))
pprobs <- rbind(pprobs.2, pprobs.3, pprobs.4, pprobs.5, pprobs.6)
p <- pprobs %>%
ggplot(aes(x = prob, y = Model)) +
#geom_histogram(bins = 40, fill = NA, position="identity")
stat_slab(aes(fill = Model),color = "gray",
size = 0.8,
alpha = 0.2) +
geom_dots(aes(fill = Model, color = Model), dotsize = 1) +
xlab("Posterior probability for cluster membership") +
ylab("") +
ggtitle("Distribution of Posterior Probabilities Across Models",
"Subject-specific posterior probabilities for assigned cluster") +
theme_minimal() +
scale_color_manual(values = c("#e3281f",
"#3aa534",
"#ff5885",
"#fbc926",
"#511e9d")
) +
scale_fill_manual(values = c("#e3281f",
"#3aa534",
"#ff5885",
"#fbc926",
"#511e9d")
) +
scale_y_discrete(limits = rev)
print(p)
ggsave("plots/Distributions.png", p, width = 8.5, height = 4.5, units = "in")
ggsave("plots/Distributions.pdf", p, width = 8.5, height = 4.5, units = "in")
```
### Residual plots
To ensure model assumptions are not violated, residual plots are also consulted.
The residual plots are very similar across all models considered and are
reassuring. As we later decide on the $G = 4$ model
([see the Spaghetti plots per cluster section](#spaghetti-plots-per-cluster)),
we have generated additional plots examining the normality of the residuals for
this model.
::: {.panel-tabset}
#### G = 1
```{R Save residual G1}
#| include: false
png("plots/residuals/g1.png",
width = 16,
height = 9,
units = "in",
res = 300)
plot(cubicbf.fits[[1]], shades = TRUE)
dev.off()
```
```{R Print residual G1}
#| fig-width: 11
#| fig-height: 8
plot(cubicbf.fits[[1]], shades = TRUE)
```
#### G = 2
```{R Save residual G2}
#| include: false
png("plots/residuals/g2.png",
width = 16,
height = 9,
units = "in",
res = 300)
plot(cubicbf.fits[[2]], shades = TRUE)
dev.off()
```
```{R Print residual G2}
#| fig-width: 11
#| fig-height: 8
plot(cubicbf.fits[[2]], shades = TRUE)
```
#### G = 3
```{R Save residual G3}
#| include: false
png("plots/residuals/g3.png",
width = 16,
height = 9,
units = "in",
res = 300)
plot(cubicbf.fits[[3]], shades = TRUE)
dev.off()
```
```{R Print residual G3}
#| fig-width: 11
#| fig-height: 8
plot(cubicbf.fits[[3]], shades = TRUE)
```
#### G = 4
```{R Save residual G4}
#| include: false
png("plots/residuals/g4.png",
width = 16,
height = 9,
units = "in",
res = 300)
plot(cubicbf.fits[[4]], shades = TRUE)
dev.off()
```
```{R Print residual G4}
#| fig-width: 11
#| fig-height: 8
plot(cubicbf.fits[[4]], shades = TRUE)
```
```{R}
#| results: "hold"
#| label: fig-qqnorm-pop
#| fig-cap: "(A) Density plot of residuals for the four-cluster model. (B) Quantile-quantile plot of residuals for the four-cluster model."
#| fig-width: 6.5
#| fig-height: 6.5
p1 <- data.frame(residuals = resid(cubicbf.fits[[4]])) %>%
ggplot(aes(x = residuals)) +
geom_histogram(aes(y = ..density..),
fill = "#D8829D",
color = "#AF6A80",
bins = 30) +
geom_density(color = "#023777", size = 1.2) +
theme_classic() +
theme(axis.line = element_line(colour = "gray")) +
ylab("Density") +
xlab("Residuals") +
ggtitle("A")
p2 <- data.frame(residuals = resid(cubicbf.fits[[4]])) %>%
ggplot(aes(sample = residuals)) +
stat_qq_band() +
stat_qq_line(color = "#D8829D") +
stat_qq_point(color = "#023777") +
theme_classic() +
theme(axis.line = element_line(colour = "gray")) +
ylab("Theoretical Quantiles") +
xlab("Sample Quantiles") +
ggtitle("B")
ggsave("paper/Residual-plot.pdf",
plot = p1 / p2,
width = 8,
height = 8,
units = "in")
print(p1 / p2)
```
```{R}
#| label: fig-qqnorm-cluster
#| fig-cap: "Quantile-quantile plots of residuals for the four-cluster latent class mixed model stratified by (A) cluster 1; (B) cluster 2; (C) cluster 3; and (D) cluster 4."
dict <- cubicbf.fits[[4]]$pprob[, c("id", "class")]
dict$class <- plyr::mapvalues(dict$class,
from = c(1, 2, 3, 4),
to = c(4, 3, 1, 2))
temp <- merge(cubicbf.fits[[4]]$pred, dict, by = "id")
par(mfrow = c(2,2))
labels <- c("A", "B", "C", "D")
p <- list()
for (i in 1:4) {
p[[i]] <- subset(temp, class == i) %>%
ggplot(aes(sample = resid_ss)) +
stat_qq_band() +
stat_qq_line(color = "#D8829D") +
stat_qq_point(color = "#023777") +
theme_classic() +
theme(axis.line = element_line(colour = "gray")) +
ylab("Theoretical Quantiles") +
xlab("Sample Quantiles") +
ggtitle(labels[i])
}
ggsave("paper/cluster-resids.pdf",
(p[[1]] + p[[2]]) / (p[[3]] + p[[4]]),
width = 8,
height = 8,
units = "in")
print((p[[1]] + p[[2]]) / (p[[3]] + p[[4]]))
```
#### G = 5
```{R Save residual G5}
#| include: false
png("plots/residuals/g5.png",
width = 16,
height = 9,
units = "in",
res = 300)
plot(cubicbf.fits[[5]], shades = TRUE)
dev.off()
```
```{R Print residual G5}
#| fig-width: 11
#| fig-height: 8
plot(cubicbf.fits[[5]], shades = TRUE)
```
#### G = 6
```{R Save residual G6}
#| include: false
png("plots/residuals/g5.png",
width = 16,
height = 9,
units = "in",
res = 300)
plot(cubicbf.fits[[5]], shades = TRUE)
dev.off()
```
```{R Print residual G6}
#| fig-width: 11
#| fig-height: 8
plot(cubicbf.fits[[6]], shades = TRUE)
```
:::
### Spaghetti plots per cluster
Plotting the mean cluster trajectories alongside spaghetti plots of all subject
trajectories provides evidence for the $G = 4$ model being the
most appropriate.
::: {.panel-tabset}
#### For G = 2
##### Log-scale, all subjects
```{R Spaghetti G2 log all subjects}
#| fig-width: 11
#| fig-height: 8
spaghetti_plot(FCcumulative, cubicbf.fits, G = 2, log = TRUE, sizes = TRUE)
```
```{R}
#| include: false
cairo_pdf("paper/sup/all_trajectories_2_log.pdf", width = 8.25, height = 4)